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 discord, asyncio
import os, sys, string import os, sys, string, random
import msgTemplates, utils import msgTemplates, utils
from smtplib import SMTP
def getPath(): def getPath():
return os.path.dirname(os.path.realpath(__file__)) return os.path.dirname(os.path.realpath(__file__))
cslibServerID = 763446305006682142
memberRoleID = 763872584408104960
version = "0.1" version = "0.1"
prefix = "!" prefix = "!"
client = discord.Client() client = discord.Client()
...@@ -21,14 +22,34 @@ async def on_message (message): ...@@ -21,14 +22,34 @@ async def on_message (message):
command[0] = command[0].lower() command[0] = command[0].lower()
# We are in a private chat # We are in a private chat
if (message.channel.guild == None): if (type(message.channel) is discord.DMChannel):
if (command[0] == "register"): if (command[0] == "register"):
email = command[1].trim() if (len(command) == 1):
if (not validateEmail (email)): return await message.channel.send(msgTemplates.usageRegister)
await message.channel.send(msgTemplates.invalidEmail.format(email))
return 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))
await message.channel.send(msgTemplates.registeringEmail.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 @client.event
async def on_member_join(member): async def on_member_join(member):
...@@ -54,17 +75,6 @@ if (not os.path.isfile(tokenFile)): ...@@ -54,17 +75,6 @@ if (not os.path.isfile(tokenFile)):
print("Please create a 'token' file with the discord bot token in it.") print("Please create a 'token' file with the discord bot token in it.")
sys.exit(1) sys.exit(1)
sender = 'cslman@soton.ac.uk' utils.setupMail()
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)
token = open(tokenFile, "r").read().strip() token = open(tokenFile, "r").read().strip()
client.run(token) client.run(token)
\ No newline at end of file
...@@ -2,5 +2,15 @@ verification = """:gear: Welcome to **CSLib** :gear: ...@@ -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 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 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" invalidMail = "`{}` is an invalid `@soton.ac.uk` e-mail"
registeringEmail = "Sending verification e-mail to: `{}`" validationMailSent = "Sent verification e-mail to: `{}`"
\ No newline at end of file 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 import string
from smtplib import SMTP
def validateEmail(email): def validateMail(email):
for c in email: for c in email:
if (c not in string.ascii_letters + string.digits + "._@+"): if (c not in string.ascii_letters + string.digits + "._@+"):
return False return False
...@@ -9,3 +10,15 @@ def validateEmail(email): ...@@ -9,3 +10,15 @@ def validateEmail(email):
return False return False
return True 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