college-bot-v1/cogs/verify.py

89 lines
3.4 KiB
Python
Raw Normal View History

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-10-06 09:48:43 +01:00
from utils import VerifyCode, Student, VerifyView, get_or_none
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()
2022-10-04 18:21:44 +01:00
@discord.guild_only()
async def verify(self, ctx: discord.ApplicationContext):
2022-09-13 20:50:02 +01:00
"""Verifies or generates a verification code"""
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
2022-10-04 18:21:44 +01:00
view = VerifyView(ctx)
return await ctx.respond(view=view, 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-10-04 15:06:43 +01:00
@commands.user_command(name="B Number")
@discord.guild_only()
async def get_b_number(self, ctx: discord.ApplicationContext, member: discord.Member):
try:
2022-10-04 15:12:40 +01:00
student: Student = await Student.objects.get(user_id=member.id)
2022-10-04 15:06:43 +01:00
return await ctx.respond(
f"{member.mention}'s B number is saved as {student.id!r}.",
2022-10-04 15:12:40 +01:00
ephemeral=True,
2022-10-04 15:06:43 +01:00
allowed_mentions=discord.AllowedMentions.none()
)
except orm.NoMatch:
2022-10-04 15:12:40 +01:00
return await ctx.respond(
f"{member.mention} has no saved B number.",
ephemeral=True,
allowed_mentions=discord.AllowedMentions.none()
)
2022-10-04 15:06:43 +01:00
2022-10-06 09:48:43 +01:00
@commands.command(name="rebind")
@commands.is_owner()
async def rebind_code(self, ctx: commands.Context, b_number: str, *, user: discord.Member):
# noinspection GrazieInspection
"""Changes which account a B number is bound to"""
student = await get_or_none(Student, id=b_number.upper())
if student:
await student.update(user_id=user.id)
return await ctx.message.add_reaction("\N{white heavy check mark}")
await ctx.message.add_reaction("\N{cross mark}")
2022-09-13 20:50:02 +01:00
def setup(bot):
bot.add_cog(VerifyCog(bot))