Skip to content
Snippets Groups Projects
Commit f6026f82 authored by amy's avatar amy
Browse files

First version is almost finished

parent 4899b539
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
import discord, asyncio
import os, sys, string
import os, sys, string, random
import msgTemplates, utils
from smtplib import SMTP
def getPath():
return os.path.dirname(os.path.realpath(__file__))
cslibServerID = 763446305006682142
memberRoleID = 763872584408104960
version = "0.1"
prefix = "!"
client = discord.Client()
......@@ -21,14 +22,34 @@ async def on_message (message):
command[0] = command[0].lower()
# We are in a private chat
if (message.channel.guild == None):
if (type(message.channel) is discord.DMChannel):
if (command[0] == "register"):
email = command[1].trim()
if (not validateEmail (email)):
await message.channel.send(msgTemplates.invalidEmail.format(email))
return
if (len(command) == 1):
return await message.channel.send(msgTemplates.usageRegister)
await message.channel.send(msgTemplates.registeringEmail.format(email))
email = command[1].strip()
if (not utils.validateMail (email)):
return await message.channel.send(msgTemplates.invalidMail.format(email))
verificationCode = ''.join(random.choices(string.ascii_lowercase + string.digits, k=16))
registrationDb[verificationCode] = message.author.id
try:
utils.sendMail(email, msgTemplates.validationMailTextBody.format(email, verificationCode))
except:
return await message.channel.send(msgTemplates.errorMail)
await message.channel.send(msgTemplates.validationMailSent.format(email))
if (command[0] == "verify"):
if (command[1] in registrationDb):
userId = registrationDb[command[1]]
guild = await client.fetch_guild(cslibServerID)
user = await guild.get_member(userId)
await user.edit(roles = [await guild.get_role(memberRoleID)])
else:
await message.channel.send(msgTemplates.errorVerificationCode.format(command[1]))
@client.event
async def on_member_join(member):
......@@ -46,7 +67,7 @@ async def on_member_join(member):
@client.event
async def on_ready():
print(client.user.name + "v" + version + " booted up.\n" + "\"Something needs tinkerin'?\"")
print(client.user.name + " v" + version + " booted up.\n" + "\"Something needs tinkerin'?\"")
await client.change_presence(activity = discord.Game(name = prefix + "help"))
tokenFile = getPath() + "/token"
......@@ -54,17 +75,6 @@ if (not os.path.isfile(tokenFile)):
print("Please create a 'token' file with the discord bot token in it.")
sys.exit(1)
sender = 'cslman@soton.ac.uk'
receivers = ['sf1u20@soton.ac.uk']
message = """From: CSLib Authmaster <cslman@soton.ac.uk>
To: CSLib Apprentice <sf1u20@soton.ac.uk>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
smtpObj = SMTP('smtp.soton.ac.uk')
smtpObj.sendmail(sender, receivers, message)
utils.setupMail()
token = open(tokenFile, "r").read().strip()
client.run(token)
\ No newline at end of file
......@@ -2,5 +2,15 @@ verification = """:gear: Welcome to **CSLib** :gear:
I will first need to verify your account! Send me your `@soton.ac.uk` university e-mail address by using: `!register YOUR-ID@soton.ac.uk`
I will then send you an e-mail with an authentication code. Send it to back to me as such: `!verify AUTH-CODE` and get verified!"""
invalidEmail = "`{}` is an invalid `@soton.ac.uk` e-mail"
registeringEmail = "Sending verification e-mail to: `{}`"
\ No newline at end of file
invalidMail = "`{}` is an invalid `@soton.ac.uk` e-mail"
validationMailSent = "Sent verification e-mail to: `{}`"
validationMailTextBody = """From: CSLib Authmaster <cslibdiscordbot@gmail.com>
To: CSLib Apprentice <{}>
Subject: CSLib Verification
Your CSLib verification code is: {}"""
errorMail = "There has been an error sending an e-mail. Please contact the CSLib Managers!"
errorVerificationCode = "Invalid verification code: `{}`"
usageRegister = "Usage: !register YOUR-ID@soton.ac.uk"
\ No newline at end of file
import string
from smtplib import SMTP
def validateEmail(email):
def validateMail(email):
for c in email:
if (c not in string.ascii_letters + string.digits + "._@+"):
return False
if (not email.endswith("@soton.ac.uk")):
return False
return True
\ No newline at end of file
return True
def setupMail():
global smtpObj
smtpObj = SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('cslibdiscordbot@gmail.com', '3RZp%5CvcStqGhByoTj5')
def sendMail(receiver, text):
receiver = [receiver] # Do we really need this?
smtpObj.sendmail('cslibdiscordbot@gmail.com', receiver, text)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment