From 3966d42d892598b23562c54e09b0b96ee8b16a2a Mon Sep 17 00:00:00 2001 From: amy <milkdrop2000@protonmail.com> Date: Tue, 20 Oct 2020 00:06:40 +0200 Subject: [PATCH] First prototype --- __pycache__/msgTemplates.cpython-37.pyc | Bin 965 -> 1441 bytes bot.py | 48 ++++++++++++++++++------ msgTemplates.py | 23 +++++++++--- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/__pycache__/msgTemplates.cpython-37.pyc b/__pycache__/msgTemplates.cpython-37.pyc index e8dfb79bff1f5b0cdcb5f54042960168b99fe70c..b380e66d1d4672873baaa24b62242b8c76280f9e 100644 GIT binary patch delta 751 zcmX@gzK}b|iI<m)fq{WRlD99OiIsujF^B`h%nS?+4h#$o#dZt~3@Hpz3@MCJj44bh z%qc8$n4*|cSX0<C8Kam}*rQldIHFinIHTB7xT4roxT82yc%nE{c%!&d_@cN|_=6cV z1rpUJ`dwpFC`&EMOq;CBXjbp+@8X)EkeHXEke*rs5zkCbQLIw8N>5EJvQp5}at`*% zOw!U)a4ap!$W1IRNi9++tFBdm$a7U$2dCzxC?rC)CMT9;=I1FS=clA9l;kT^=9d;J z1m~BQWF+Pml;r0rr0V7-X67g)rlb_578h4}`GtX8QVCI*l3ARbUzDPdn4FwnnpdJw zo>`KiP?C{~#q6Tg<kZZv)D(rX%tWZ6noL!)6CcRxRtYMCy;rH=7#iZC3-VwUt71lK zPC=Dma7KQ)LP<tuu|jTYadBdL>g1J-o2|?v^Gg*n63bE*l2TLipthx^C@O@f<|OCm zf_(yV1~?=XlJj$OOY<^ID)p+eCdV>q+j*veoDVWKFB?P_D`XZcBq|gX<tOE&=ISUE z<fJARrz#}p=anQTmnbAEK+N$?%u7s9Et*`%s55yrlUse2LMX^ZRthlZgME<+2~k~8 zi27;r+~PqNy~PFcmPcx0N@~$9HW1q-_ZA0;_D#&pyCs6G);BRTCpa~)<dy`wXh>>B ziBrC2O64t{(&E%2aExT8rrhF8Eh@?{0_nRY0A_mTl_ln6rc8EX))KqLT?%q7SaIb_ ph9X`D28JTu$)(H=G7=073@l76OiYX%%pA-ttSpQyOe~BL5&(3X@OJ<J delta 280 zcmZ3;eU#nLiI<m)fq{V``(tms5i<kBV-N?185tND92giFiWL|b7*ZIb7*ZIc7*m*{ zm{ORdm{VAySW;M{SX0=d*izVo88kT(^(N+A<IGb?EG@}M%`2Jg!Dz<n7#iZCJGqWg zV)9#Nt;y4vHZl56ZerF}3dl)KEKXHO&d)1JOfFF<$w*aj4))1RQt(a8OH5BKDps5< z#^N66r^$Ycr!2K7Gc7YYu_QA;?-qAvURh#JW{PiOX3i}kFcT~R;s>YZmE00T6$?qN xC~?Y9sl3IRT2z!@G?|rEi*qGI5eEYULlMVhS5^lB9tH*m7A6)(7FICH1ORMfO;!K^ diff --git a/bot.py b/bot.py index 967f0f1..91ea6e2 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 e7cc535..2e08c04 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 -- GitLab