diff --git a/__pycache__/msgTemplates.cpython-37.pyc b/__pycache__/msgTemplates.cpython-37.pyc index e8dfb79bff1f5b0cdcb5f54042960168b99fe70c..b380e66d1d4672873baaa24b62242b8c76280f9e 100644 Binary files a/__pycache__/msgTemplates.cpython-37.pyc and b/__pycache__/msgTemplates.cpython-37.pyc differ diff --git a/bot.py b/bot.py index 967f0f15fb3e1bf500a63e3fc42774d28acf1971..91ea6e29268053d228ecf4ee530f0e2d5e232eda 100644 --- a/bot.py +++ b/bot.py @@ -1,16 +1,19 @@ import discord, asyncio -import os, sys, string, random +import os, sys, string, random, time import msgTemplates, utils def getPath(): return os.path.dirname(os.path.realpath(__file__)) +# Maybe move these to a config? cslibServerID = 763446305006682142 memberRoleID = 763872584408104960 version = "0.1" prefix = "!" + client = discord.Client() registrationDb = {} +registrationUserTimeout = {} @client.event async def on_message (message): @@ -22,32 +25,55 @@ async def on_message (message): command[0] = command[0].lower() # We are in a private chat - if (type(message.channel) is discord.DMChannel): + if (type(message.channel) is not discord.DMChannel): + if (command[0] == "help"): + outMsg = msgTemplates.helpHeader.format(version) + + for cmd in msgTemplates.helpMain: + outMsg += "\t- **{}**: `{}`\n".format(cmd, msgTemplates.helpMain[cmd]) + + return await message.channel.send(outMsg) + + else: + if (command[0] == "help"): + outMsg = msgTemplates.helpHeader.format(version) + + for cmd in msgTemplates.helpDm: + outMsg += "\t- **{}**: `{}`\n".format(cmd, msgTemplates.helpDm[cmd]) + + return await message.channel.send(outMsg) + if (command[0] == "register"): if (len(command) == 1): return await message.channel.send(msgTemplates.usageRegister) email = command[1].strip() if (not utils.validateMail (email)): - return await message.channel.send(msgTemplates.invalidMail.format(email)) + return await message.channel.send(msgTemplates.errorInvalidMail.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)) + utils.sendMail(email, msgTemplates.verificationMailTextBody.format(email, verificationCode)) except: return await message.channel.send(msgTemplates.errorMail) - await message.channel.send(msgTemplates.validationMailSent.format(email)) + await message.channel.send(msgTemplates.verificationMailSent.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)]) - + if (len(command) == 1): + return await message.channel.send(msgTemplates.usageVerify) + + verificationCode = command[1] + if (verificationCode in registrationDb): + userId = registrationDb[verificationCode] + guild = client.get_guild(cslibServerID) + await guild.get_member(userId).edit(roles = [guild.get_role(memberRoleID)]) + registrationDb.pop(verificationCode, None) + + await message.channel.send(msgTemplates.userVerified) + else: await message.channel.send(msgTemplates.errorVerificationCode.format(command[1])) diff --git a/msgTemplates.py b/msgTemplates.py index e7cc535316d761cc7efbdbbcbe796769e2fc1f7b..2e08c0445d6f11006905d6a456844ec62735b8d8 100644 --- a/msgTemplates.py +++ b/msgTemplates.py @@ -1,16 +1,27 @@ 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!""" +I will then send you an e-mail with a verification code. Send it to back to me as such: `!verify verification-code` and get verified!""" -invalidMail = "`{}` is an invalid `@soton.ac.uk` e-mail" -validationMailSent = "Sent verification e-mail to: `{}`" -validationMailTextBody = """From: CSLib Authmaster <cslibdiscordbot@gmail.com> +helpHeader = ":gear: **CSLib** Authmaster v{} :gear:\n" +helpDm = { + "!register YOUR-ID@soton.ac.uk": "Send a verification code to your Southampton e-mail address", + "!verify AUTH-CODE": "Verify your discord account with the verification code received via e-mail" +} +helpMain = { + "!help": "Show this message" +} + +verificationMailSent = "Sent verification e-mail to: `{}`" +verificationMailTextBody = """From: CSLib Authmaster <cslibdiscordbot@gmail.com> To: CSLib Apprentice <{}> Subject: CSLib Verification Your CSLib verification code is: {}""" +userVerified = "You have been verified! Welcome to the CSLib community." -errorMail = "There has been an error sending an e-mail. Please contact the CSLib Managers!" +errorMail = "There has been an error sending an e-mail. If you think this is a problem, please contact a CSLib Manager!" +errorInvalidMail = "`{}` is an invalid `@soton.ac.uk` e-mail" errorVerificationCode = "Invalid verification code: `{}`" -usageRegister = "Usage: !register YOUR-ID@soton.ac.uk" \ No newline at end of file +usageRegister = "Usage: !register `YOUR-ID@soton.ac.uk`" +usageVerify = "Usage: !verify `verification-code`" \ No newline at end of file