diff --git a/__pycache__/msgTemplates.cpython-37.pyc b/__pycache__/msgTemplates.cpython-37.pyc index b7908bcc6380a484050a1c71c4377b4062213260..e8dfb79bff1f5b0cdcb5f54042960168b99fe70c 100644 Binary files a/__pycache__/msgTemplates.cpython-37.pyc and b/__pycache__/msgTemplates.cpython-37.pyc differ diff --git a/__pycache__/utils.cpython-37.pyc b/__pycache__/utils.cpython-37.pyc index 6e0ae6d828d37d1d90c575ea92d4b1d36424a629..276b69728cad34ea1f1b999200e3dcc600a3f1e7 100644 Binary files a/__pycache__/utils.cpython-37.pyc and b/__pycache__/utils.cpython-37.pyc differ diff --git a/bot.py b/bot.py index d9a03eb05a0ebe084e6f96a55bb8ebef89c2fad1..967f0f15fb3e1bf500a63e3fc42774d28acf1971 100644 --- a/bot.py +++ b/bot.py @@ -1,11 +1,12 @@ 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 diff --git a/msgTemplates.py b/msgTemplates.py index 3a59c8db4f47178f349859516149f4fb5cebb685..e7cc535316d761cc7efbdbbcbe796769e2fc1f7b 100644 --- a/msgTemplates.py +++ b/msgTemplates.py @@ -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 diff --git a/utils.py b/utils.py index 7c143f5a6f44e20f2c40d40def1467fd01100d27..f34299719df1f09b8f76e6fdf088c6e226e709df 100644 --- a/utils.py +++ b/utils.py @@ -1,11 +1,24 @@ 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