mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
Enforce CWD
This commit is contained in:
parent
131de5cae3
commit
8314b0605d
4 changed files with 34 additions and 78 deletions
|
@ -24,6 +24,7 @@ class VerifyCog(commands.Cog):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if code is None:
|
if code is None:
|
||||||
|
|
||||||
class Modal(discord.ui.Modal):
|
class Modal(discord.ui.Modal):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(
|
super().__init__(
|
||||||
|
@ -32,9 +33,9 @@ class VerifyCog(commands.Cog):
|
||||||
label="What is your student ID",
|
label="What is your student ID",
|
||||||
placeholder="B...",
|
placeholder="B...",
|
||||||
min_length=7,
|
min_length=7,
|
||||||
max_length=7
|
max_length=7,
|
||||||
),
|
),
|
||||||
title="Enter your student ID number"
|
title="Enter your student ID number",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def callback(self, interaction: discord.Interaction):
|
async def callback(self, interaction: discord.Interaction):
|
||||||
|
@ -43,24 +44,12 @@ class VerifyCog(commands.Cog):
|
||||||
if not st: # timed out
|
if not st: # timed out
|
||||||
return
|
return
|
||||||
|
|
||||||
if not re.match(
|
if not re.match(r"^B\d{6}$", st):
|
||||||
"^B\d{6}$",
|
return await interaction.response.send_message("\N{cross mark} Invalid student ID.")
|
||||||
st
|
|
||||||
):
|
|
||||||
return await interaction.response.send_message(
|
|
||||||
"\N{cross mark} Invalid student ID."
|
|
||||||
)
|
|
||||||
|
|
||||||
_code = await send_verification_code(
|
_code = await send_verification_code(ctx.author, st)
|
||||||
ctx.author,
|
|
||||||
st
|
|
||||||
)
|
|
||||||
console.log(f"Sending verification email to {ctx.author} ({ctx.author.id}/{st})...")
|
console.log(f"Sending verification email to {ctx.author} ({ctx.author.id}/{st})...")
|
||||||
__code = await VerifyCode.objects.create(
|
__code = await VerifyCode.objects.create(code=_code, bind=ctx.author.id, student_id=st)
|
||||||
code=_code,
|
|
||||||
bind=ctx.author.id,
|
|
||||||
student_id=st
|
|
||||||
)
|
|
||||||
console.log(f"[green]Sent verification email to {ctx.author} ({ctx.author.id}/{st}): {_code!r}")
|
console.log(f"[green]Sent verification email to {ctx.author} ({ctx.author.id}/{st}): {_code!r}")
|
||||||
await interaction.followup.send(
|
await interaction.followup.send(
|
||||||
"\N{white heavy check mark} Verification email sent to your college email "
|
"\N{white heavy check mark} Verification email sent to your college email "
|
||||||
|
@ -73,34 +62,26 @@ class VerifyCog(commands.Cog):
|
||||||
f"is your birthday, !, and the first three letters of your first or last name"
|
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" (for example, `John Doe`, born on the 1st of february 2006, would be either "
|
||||||
f"`01022006!Joh` or `01022006!Doe`).",
|
f"`01022006!Joh` or `01022006!Doe`).",
|
||||||
ephemeral=True
|
ephemeral=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
return await ctx.send_modal(Modal())
|
return await ctx.send_modal(Modal())
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
existing: VerifyCode = await VerifyCode.objects.get(
|
existing: VerifyCode = await VerifyCode.objects.get(code=code)
|
||||||
code=code
|
|
||||||
)
|
|
||||||
except orm.NoMatch:
|
except orm.NoMatch:
|
||||||
return await ctx.respond(
|
return await ctx.respond(
|
||||||
"\N{cross mark} Invalid or unknown verification code. Try again!",
|
"\N{cross mark} Invalid or unknown verification code. Try again!", ephemeral=True
|
||||||
ephemeral=True
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await Student.objects.create(
|
await Student.objects.create(id=existing.student_id, user_id=ctx.author.id)
|
||||||
id=existing.student_id,
|
|
||||||
user_id=ctx.author.id
|
|
||||||
)
|
|
||||||
await existing.delete()
|
await existing.delete()
|
||||||
role = discord.utils.find(lambda r: r.name.lower() == "verified", guild.roles)
|
role = discord.utils.find(lambda r: r.name.lower() == "verified", guild.roles)
|
||||||
if role and role < guild.me.top_role:
|
if role and role < guild.me.top_role:
|
||||||
member = await guild.fetch_member(ctx.author.id)
|
member = await guild.fetch_member(ctx.author.id)
|
||||||
await member.add_roles(role, reason="Verified")
|
await member.add_roles(role, reason="Verified")
|
||||||
console.log(f"[green]{ctx.author} verified ({ctx.author.id}/{existing.student_id})")
|
console.log(f"[green]{ctx.author} verified ({ctx.author.id}/{existing.student_id})")
|
||||||
return await ctx.respond(
|
return await ctx.respond("\N{white heavy check mark} Verification complete!", ephemeral=True)
|
||||||
"\N{white heavy check mark} Verification complete!",
|
|
||||||
ephemeral=True
|
|
||||||
)
|
|
||||||
|
|
||||||
@commands.command(name="de-verify")
|
@commands.command(name="de-verify")
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
|
@ -124,10 +105,7 @@ class VerifyCog(commands.Cog):
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def verification_force(self, ctx: commands.Context, user: discord.Member, _id: str):
|
async def verification_force(self, ctx: commands.Context, user: discord.Member, _id: str):
|
||||||
"""Manually verifies someone"""
|
"""Manually verifies someone"""
|
||||||
await Student.objects.create(
|
await Student.objects.create(id=_id, user_id=user.id)
|
||||||
id=_id,
|
|
||||||
user_id=user.id
|
|
||||||
)
|
|
||||||
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
|
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
|
||||||
if role and role < ctx.me.top_role:
|
if role and role < ctx.me.top_role:
|
||||||
member = await ctx.guild.fetch_member(ctx.author.id)
|
member = await ctx.guild.fetch_member(ctx.author.id)
|
||||||
|
|
8
main.py
8
main.py
|
@ -5,9 +5,7 @@ from utils import registry
|
||||||
|
|
||||||
|
|
||||||
bot = commands.Bot(
|
bot = commands.Bot(
|
||||||
commands.when_mentioned_or("h!"),
|
commands.when_mentioned_or("h!"), debug_guilds=config.guilds, allowed_mentions=discord.AllowedMentions.none()
|
||||||
debug_guilds=config.guilds,
|
|
||||||
allowed_mentions=discord.AllowedMentions.none()
|
|
||||||
)
|
)
|
||||||
bot.load_extension("jishaku")
|
bot.load_extension("jishaku")
|
||||||
bot.load_extension("cogs.verify")
|
bot.load_extension("cogs.verify")
|
||||||
|
@ -23,9 +21,7 @@ async def on_ready():
|
||||||
async def ping(ctx: discord.ApplicationContext):
|
async def ping(ctx: discord.ApplicationContext):
|
||||||
"""Checks the bot's response time"""
|
"""Checks the bot's response time"""
|
||||||
gateway = round(ctx.bot.latency * 1000, 2)
|
gateway = round(ctx.bot.latency * 1000, 2)
|
||||||
return await ctx.respond(
|
return await ctx.respond(f"\N{white heavy check mark} Pong! `{gateway}ms`.")
|
||||||
f"\N{white heavy check mark} Pong! `{gateway}ms`."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
bot.run(config.token)
|
bot.run(config.token)
|
||||||
|
|
|
@ -6,23 +6,16 @@ import config
|
||||||
import aiosmtplib as smtp
|
import aiosmtplib as smtp
|
||||||
from email.message import EmailMessage
|
from email.message import EmailMessage
|
||||||
|
|
||||||
gmail_cfg = {
|
gmail_cfg = {"addr": "smtp.gmail.com", "username": config.email, "password": config.email_password, "port": 465}
|
||||||
"addr": "smtp.gmail.com",
|
|
||||||
"username": config.email,
|
|
||||||
"password": config.email_password,
|
|
||||||
"port": 465
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async def send_verification_code(
|
async def send_verification_code(user: discord.User, student_number: str, **kwargs) -> str:
|
||||||
user: discord.User,
|
|
||||||
student_number: str,
|
|
||||||
**kwargs
|
|
||||||
) -> str:
|
|
||||||
"""Sends a verification code, returning said verification code, to the student."""
|
"""Sends a verification code, returning said verification code, to the student."""
|
||||||
code = secrets.token_hex(16)
|
code = secrets.token_hex(16)
|
||||||
text = f"Hey {user} ({student_number})! The code to join the hi^5 code is '{code}' - use " \
|
text = (
|
||||||
|
f"Hey {user} ({student_number})! The code to join the hi^5 code is '{code}' - use "
|
||||||
f"'/verify {code}' in the bot's DMs to continue \N{dancer}\n\n~nex"
|
f"'/verify {code}' in the bot's DMs to continue \N{dancer}\n\n~nex"
|
||||||
|
)
|
||||||
msg = EmailMessage()
|
msg = EmailMessage()
|
||||||
msg["From"] = gmail_cfg["username"]
|
msg["From"] = gmail_cfg["username"]
|
||||||
msg["To"] = f"{student_number}@my.leedscitycollege.ac.uk"
|
msg["To"] = f"{student_number}@my.leedscitycollege.ac.uk"
|
||||||
|
@ -30,29 +23,14 @@ async def send_verification_code(
|
||||||
msg["Subject"] = "Server Verification"
|
msg["Subject"] = "Server Verification"
|
||||||
msg.set_content(text)
|
msg.set_content(text)
|
||||||
|
|
||||||
kwargs.setdefault(
|
kwargs.setdefault("hostname", gmail_cfg["addr"])
|
||||||
"hostname", gmail_cfg["addr"]
|
kwargs.setdefault("port", gmail_cfg["port"])
|
||||||
)
|
kwargs.setdefault("use_tls", True)
|
||||||
kwargs.setdefault(
|
kwargs.setdefault("username", gmail_cfg["username"])
|
||||||
"port", gmail_cfg["port"]
|
kwargs.setdefault("password", gmail_cfg["password"])
|
||||||
)
|
kwargs.setdefault("start_tls", not kwargs["use_tls"])
|
||||||
kwargs.setdefault(
|
|
||||||
"use_tls", True
|
|
||||||
)
|
|
||||||
kwargs.setdefault(
|
|
||||||
"username", gmail_cfg["username"]
|
|
||||||
)
|
|
||||||
kwargs.setdefault(
|
|
||||||
"password", gmail_cfg["password"]
|
|
||||||
)
|
|
||||||
kwargs.setdefault(
|
|
||||||
"start_tls", not kwargs["use_tls"]
|
|
||||||
)
|
|
||||||
|
|
||||||
assert kwargs["start_tls"] != kwargs["use_tls"]
|
assert kwargs["start_tls"] != kwargs["use_tls"]
|
||||||
|
|
||||||
await smtp.send(
|
await smtp.send(msg, **kwargs)
|
||||||
msg,
|
|
||||||
**kwargs
|
|
||||||
)
|
|
||||||
return code
|
return code
|
||||||
|
|
|
@ -3,6 +3,10 @@ from typing import TYPE_CHECKING
|
||||||
|
|
||||||
import orm
|
import orm
|
||||||
from databases import Database
|
from databases import Database
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
os.chdir(Path(__file__).parent)
|
||||||
|
|
||||||
|
|
||||||
registry = orm.ModelRegistry(Database("sqlite:///main.db"))
|
registry = orm.ModelRegistry(Database("sqlite:///main.db"))
|
||||||
|
@ -15,7 +19,7 @@ class VerifyCode(orm.Model):
|
||||||
"id": orm.Integer(primary_key=True),
|
"id": orm.Integer(primary_key=True),
|
||||||
"code": orm.String(min_length=8, max_length=64, unique=True),
|
"code": orm.String(min_length=8, max_length=64, unique=True),
|
||||||
"bind": orm.BigInteger(),
|
"bind": orm.BigInteger(),
|
||||||
"student_id": orm.String(min_length=7, max_length=7)
|
"student_id": orm.String(min_length=7, max_length=7),
|
||||||
}
|
}
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
id: int
|
id: int
|
||||||
|
|
Loading…
Reference in a new issue