college-bot-v1/cogs/verify.py

113 lines
4.7 KiB
Python
Raw Normal View History

2022-09-13 20:50:02 +01:00
import discord
import orm
from discord.ext import commands
2022-12-18 14:59:08 +00:00
from utils import VerifyCode, Student, VerifyView, get_or_none, console
2022-09-13 20:50:02 +01:00
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:
2022-11-28 17:30:44 +00:00
student: Student = await Student.objects.get(user_id=ctx.user.id)
2022-09-13 20:50:02 +01:00
return await ctx.respond(f"\N{cross mark} You're already verified as {student.id}!", ephemeral=True)
except orm.NoMatch:
pass
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
channel = discord.utils.get(ctx.guild.text_channels, name="verify")
2022-11-28 17:30:44 +00:00
if role in ctx.user.roles:
2022-11-23 11:54:59 +00:00
if role and role < ctx.me.top_role:
2022-11-28 17:30:44 +00:00
await ctx.user.remove_roles(role, reason=f"Auto de-verified")
2022-11-23 11:54:59 +00:00
if channel:
try:
2022-11-28 17:30:44 +00:00
await ctx.user.send(
2022-11-23 14:34:58 +00:00
f"You have been automatically de-verified. Please re-verify by going to {channel.mention} "
f"and typing </verify:{ctx.command.id}>."
2022-11-23 11:54:59 +00:00
)
except discord.Forbidden:
pass
return
view = VerifyView(ctx)
return await ctx.respond(view=view, ephemeral=True)
2022-09-13 20:50:02 +01:00
@commands.command(name="de-verify")
2023-01-09 10:34:33 +00:00
@commands.guild_only()
2022-09-13 20:50:02 +01:00
async def verification_del(self, ctx: commands.Context, *, user: discord.Member):
"""Removes a user's verification status"""
2023-01-09 10:34:33 +00:00
if not await self.bot.is_owner(ctx.author):
if not ctx.author.guild_permissions.administrator:
return await ctx.reply(":x: Permission denied.")
2022-09-13 20:50:02 +01:00
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}")
2022-10-12 18:01:37 +01:00
await ctx.message.delete(delay=10)
2022-09-13 20:50:02 +01:00
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.guild_only()
2022-12-18 14:53:52 +00:00
async def verification_force(self, ctx: commands.Context, user: discord.Member, _id: str, name: str):
2022-09-13 21:19:23 +01:00
"""Manually verifies someone"""
2023-01-09 10:34:33 +00:00
if not await self.bot.is_owner(ctx.author):
if not ctx.author.guild_permissions.administrator:
return await ctx.reply(":x: Permission denied.")
2022-12-18 14:59:08 +00:00
existing = await Student.objects.create(id=_id, user_id=user.id, name=name)
2022-09-13 21:19:23 +01:00
role = discord.utils.find(lambda r: r.name.lower() == "verified", ctx.guild.roles)
2022-12-18 14:59:08 +00:00
if role and role < ctx.guild.me.top_role:
await user.add_roles(role, reason="Verified")
try:
await user.edit(nick=f"{existing.name}", reason="Verified")
except discord.HTTPException:
pass
2022-12-18 15:00:10 +00:00
console.log(f"[green]{ctx.author} verified {user} ({user.id})")
2022-10-12 18:01:37 +01:00
await ctx.message.delete(delay=10)
2022-09-13 21:19:23 +01:00
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-30 16:31:38 +00:00
allowed_mentions=discord.AllowedMentions.none(),
2022-10-04 15:06:43 +01:00
)
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,
2022-10-30 16:31:38 +00:00
allowed_mentions=discord.AllowedMentions.none(),
2022-10-04 15:12:40 +01:00
)
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-10-12 18:01:37 +01:00
await ctx.message.delete(delay=10)
2022-10-06 09:48:43 +01:00
2022-09-13 20:50:02 +01:00
def setup(bot):
bot.add_cog(VerifyCog(bot))