2022-09-13 20:50:02 +01:00
|
|
|
import discord
|
|
|
|
import orm
|
2022-09-13 21:19:23 +01:00
|
|
|
import re
|
2022-09-13 20:50:02 +01:00
|
|
|
from discord.ext import commands
|
2022-09-13 21:19:23 +01:00
|
|
|
from utils import send_verification_code, VerifyCode, Student, console
|
2022-09-13 20:50:02 +01:00
|
|
|
import config
|
|
|
|
|
|
|
|
|
|
|
|
class VerifyCog(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@commands.slash_command()
|
|
|
|
async def verify(self, ctx: discord.ApplicationContext, *, code: str = None):
|
|
|
|
"""Verifies or generates a verification code"""
|
|
|
|
guild = self.bot.get_guild(config.guilds[0])
|
|
|
|
# if ctx.guild is not None:
|
|
|
|
# return await ctx.respond("\N{cross mark} This command can only be run in my DMs!", ephemeral=True)
|
|
|
|
|
|
|
|
try:
|
|
|
|
student: Student = await Student.objects.get(user_id=ctx.author.id)
|
|
|
|
return await ctx.respond(f"\N{cross mark} You're already verified as {student.id}!", ephemeral=True)
|
|
|
|
except orm.NoMatch:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if code is None:
|
2022-09-13 21:27:14 +01:00
|
|
|
|
2022-09-13 20:50:02 +01:00
|
|
|
class Modal(discord.ui.Modal):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(
|
|
|
|
discord.ui.InputText(
|
|
|
|
custom_id="student_id",
|
|
|
|
label="What is your student ID",
|
|
|
|
placeholder="B...",
|
|
|
|
min_length=7,
|
2022-09-13 21:27:14 +01:00
|
|
|
max_length=7,
|
2022-09-13 20:50:02 +01:00
|
|
|
),
|
2022-09-13 21:27:14 +01:00
|
|
|
title="Enter your student ID number",
|
2022-09-13 20:50:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
async def callback(self, interaction: discord.Interaction):
|
|
|
|
await interaction.response.defer()
|
2022-09-13 21:19:23 +01:00
|
|
|
st = self.children[0].value
|
|
|
|
if not st: # timed out
|
2022-09-13 20:50:02 +01:00
|
|
|
return
|
2022-09-13 21:19:23 +01:00
|
|
|
|
2022-09-13 21:27:14 +01:00
|
|
|
if not re.match(r"^B\d{6}$", st):
|
|
|
|
return await interaction.response.send_message("\N{cross mark} Invalid student ID.")
|
|
|
|
|
|
|
|
_code = await send_verification_code(ctx.author, st)
|
2022-09-13 21:19:23 +01:00
|
|
|
console.log(f"Sending verification email to {ctx.author} ({ctx.author.id}/{st})...")
|
2022-09-13 21:27:14 +01:00
|
|
|
__code = await VerifyCode.objects.create(code=_code, bind=ctx.author.id, student_id=st)
|
2022-09-13 21:19:23 +01:00
|
|
|
console.log(f"[green]Sent verification email to {ctx.author} ({ctx.author.id}/{st}): {_code!r}")
|
2022-09-13 20:50:02 +01:00
|
|
|
await interaction.followup.send(
|
|
|
|
"\N{white heavy check mark} Verification email sent to your college email "
|
2022-09-13 21:19:23 +01:00
|
|
|
f"({st}@my.leedscitycollege.ac.uk)\n"
|
2022-09-13 20:50:02 +01:00
|
|
|
f"Once you get that email, run this command again, with the first option being the 16"
|
|
|
|
f" character code.\n\n"
|
|
|
|
f">>> If you don't know how to access your email, go to <https://gmail.com>, then "
|
2022-09-13 21:19:23 +01:00
|
|
|
f"sign in as `{st}@leedscitycollege.ac.uk` (notice there's no `my.` "
|
2022-09-13 20:50:02 +01:00
|
|
|
f"prefix to sign into gmail), and you should be greeted by your inbox. The default password "
|
|
|
|
f"is your birthday, !, and the first three letters of your first or last name"
|
|
|
|
f" (for example, `John Doe`, born on the 1st of february 2006, would be either "
|
|
|
|
f"`01022006!Joh` or `01022006!Doe`).",
|
2022-09-13 21:27:14 +01:00
|
|
|
ephemeral=True,
|
2022-09-13 20:50:02 +01:00
|
|
|
)
|
2022-09-13 21:27:14 +01:00
|
|
|
|
2022-09-13 20:50:02 +01:00
|
|
|
return await ctx.send_modal(Modal())
|
|
|
|
else:
|
|
|
|
try:
|
2022-09-13 21:27:14 +01:00
|
|
|
existing: VerifyCode = await VerifyCode.objects.get(code=code)
|
2022-09-13 20:50:02 +01:00
|
|
|
except orm.NoMatch:
|
|
|
|
return await ctx.respond(
|
2022-09-13 21:27:14 +01:00
|
|
|
"\N{cross mark} Invalid or unknown verification code. Try again!", ephemeral=True
|
2022-09-13 20:50:02 +01:00
|
|
|
)
|
|
|
|
else:
|
2022-09-13 21:27:14 +01:00
|
|
|
await Student.objects.create(id=existing.student_id, user_id=ctx.author.id)
|
2022-09-13 20:50:02 +01:00
|
|
|
await existing.delete()
|
|
|
|
role = discord.utils.find(lambda r: r.name.lower() == "verified", guild.roles)
|
|
|
|
if role and role < guild.me.top_role:
|
|
|
|
member = await guild.fetch_member(ctx.author.id)
|
|
|
|
await member.add_roles(role, reason="Verified")
|
2022-09-13 21:19:23 +01:00
|
|
|
console.log(f"[green]{ctx.author} verified ({ctx.author.id}/{existing.student_id})")
|
2022-09-13 21:27:14 +01:00
|
|
|
return await ctx.respond("\N{white heavy check mark} Verification complete!", ephemeral=True)
|
2022-09-13 20:50:02 +01:00
|
|
|
|
|
|
|
@commands.command(name="de-verify")
|
|
|
|
@commands.is_owner()
|
|
|
|
async def verification_del(self, ctx: commands.Context, *, user: discord.Member):
|
|
|
|
"""Removes a user's verification status"""
|
|
|
|
await ctx.trigger_typing()
|
|
|
|
for code in await VerifyCode.objects.all(bind=user.id):
|
|
|
|
await code.delete()
|
|
|
|
usr = await Student.objects.first(user_id=user.id)
|
|
|
|
if usr:
|
|
|
|
await usr.delete()
|
|
|
|
|
|
|
|
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
|
|
|
|
if role and role < ctx.me.top_role:
|
|
|
|
await user.remove_roles(role, reason=f"De-verified by {ctx.author}")
|
|
|
|
|
|
|
|
return await ctx.reply(f"\N{white heavy check mark} De-verified {user}.")
|
|
|
|
|
2022-09-13 21:19:23 +01:00
|
|
|
@commands.command(name="verify")
|
|
|
|
@commands.is_owner()
|
|
|
|
@commands.guild_only()
|
|
|
|
async def verification_force(self, ctx: commands.Context, user: discord.Member, _id: str):
|
|
|
|
"""Manually verifies someone"""
|
2022-09-13 21:27:14 +01:00
|
|
|
await Student.objects.create(id=_id, user_id=user.id)
|
2022-09-13 21:19:23 +01:00
|
|
|
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
|
|
|
|
if role and role < ctx.me.top_role:
|
|
|
|
member = await ctx.guild.fetch_member(ctx.author.id)
|
|
|
|
await member.add_roles(role, reason="Verified")
|
|
|
|
return await ctx.reply(
|
|
|
|
"\N{white heavy check mark} Verification complete!",
|
|
|
|
)
|
|
|
|
|
2022-09-13 20:50:02 +01:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(VerifyCog(bot))
|