2023-01-19 14:04:28 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2022-10-04 16:20:01 +01:00
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
2023-01-19 14:04:28 +00:00
|
|
|
from utils import Student, get_or_none, BannedStudentID, owner_or_admin, JimmyBans
|
2022-10-04 16:20:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Mod(commands.Cog):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@commands.user_command(name="Ban Account's B Number")
|
|
|
|
@discord.default_permissions(ban_members=True)
|
|
|
|
async def ban_student_id(self, ctx: discord.ApplicationContext, member: discord.Member):
|
|
|
|
"""Bans a student ID from registering. Also bans an account associated with it."""
|
|
|
|
await ctx.defer(ephemeral=True)
|
|
|
|
student_id = await get_or_none(Student, user_id=member.id)
|
|
|
|
if student_id is None:
|
|
|
|
return await ctx.respond("\N{cross mark} Unknown B number (is the user verified yet?)", ephemeral=True)
|
|
|
|
|
|
|
|
ban = await get_or_none(BannedStudentID, student_id=student_id.id)
|
|
|
|
if ban is None:
|
|
|
|
ban = await BannedStudentID.objects.create(
|
|
|
|
student_id=student_id.id,
|
|
|
|
associated_account=member.id,
|
|
|
|
)
|
|
|
|
|
2022-11-28 17:30:44 +00:00
|
|
|
await member.ban(reason=f"Banned ID {ban.student_id} by {ctx.user}")
|
2022-10-04 16:20:01 +01:00
|
|
|
return await ctx.respond(
|
2022-10-30 16:31:38 +00:00
|
|
|
f"\N{white heavy check mark} Banned {ban.student_id} (and {member.mention})", ephemeral=True
|
2022-10-04 16:20:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@commands.slash_command(name="unban-student-number")
|
|
|
|
@discord.default_permissions(ban_members=True)
|
|
|
|
async def unban_student_id(self, ctx: discord.ApplicationContext, student_id: str):
|
|
|
|
"""Unbans a student ID and the account associated with it."""
|
|
|
|
student_id = student_id.upper()
|
|
|
|
ban = await get_or_none(BannedStudentID, student_id=student_id)
|
|
|
|
if not ban:
|
|
|
|
return await ctx.respond("\N{cross mark} That student ID isn't banned.")
|
|
|
|
await ctx.defer()
|
|
|
|
user_id = ban.associated_account
|
|
|
|
await ban.delete()
|
|
|
|
if not user_id:
|
2022-10-04 16:21:50 +01:00
|
|
|
return await ctx.respond(f"\N{white heavy check mark} Unbanned {student_id}. No user to unban.")
|
2022-10-04 16:20:01 +01:00
|
|
|
else:
|
|
|
|
try:
|
2022-11-28 17:30:44 +00:00
|
|
|
await ctx.guild.unban(discord.Object(user_id), reason=f"Unbanned by {ctx.user}")
|
2022-10-04 16:20:01 +01:00
|
|
|
except discord.HTTPException as e:
|
|
|
|
return await ctx.respond(
|
|
|
|
f"\N{white heavy check mark} Unbanned {student_id}. Failed to unban {user_id} - HTTP {e.status}."
|
|
|
|
)
|
|
|
|
else:
|
2022-10-30 16:31:38 +00:00
|
|
|
return await ctx.respond(f"\N{white heavy check mark} Unbanned {student_id}. Unbanned {user_id}.")
|
2022-10-04 16:20:01 +01:00
|
|
|
|
2023-01-19 14:04:28 +00:00
|
|
|
@commands.slash_command(name="block")
|
|
|
|
@owner_or_admin()
|
2023-01-26 15:14:37 +00:00
|
|
|
async def block_user(
|
|
|
|
self,
|
|
|
|
ctx: discord.ApplicationContext,
|
|
|
|
user: discord.Member,
|
|
|
|
reason: str,
|
|
|
|
until: str
|
|
|
|
):
|
2023-01-19 14:04:28 +00:00
|
|
|
"""Blocks a user from using the bot."""
|
|
|
|
await ctx.defer()
|
2023-01-26 15:14:37 +00:00
|
|
|
date = datetime.utcnow()
|
|
|
|
_time = until
|
|
|
|
try:
|
|
|
|
date, _time = until.split(" ")
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
date = datetime.strptime(date, "%d/%m/%Y")
|
|
|
|
except ValueError:
|
|
|
|
return await ctx.respond("Invalid date format. Use `DD/MM/YYYY`.")
|
2023-01-19 14:04:28 +00:00
|
|
|
try:
|
2023-01-26 15:15:19 +00:00
|
|
|
hour, minute = map(int, _time.split(":"))
|
2023-01-19 14:04:28 +00:00
|
|
|
except ValueError:
|
|
|
|
return await ctx.respond("\N{cross mark} Invalid time format. Use HH:MM.")
|
2023-01-26 15:14:37 +00:00
|
|
|
end = date.replace(hour=hour, minute=minute)
|
2023-01-19 14:04:28 +00:00
|
|
|
|
|
|
|
# get an entry for the user's ID, and if it doesn't exist, create it. Otherwise, alert the user.
|
|
|
|
entry = await get_or_none(JimmyBans, user_id=user.id)
|
|
|
|
if entry is None:
|
|
|
|
await JimmyBans.objects.create(user_id=user.id, reason=reason, until=end.timestamp())
|
|
|
|
else:
|
|
|
|
return await ctx.respond("\N{cross mark} That user is already blocked.")
|
|
|
|
await ctx.respond(f"\N{white heavy check mark} Blocked {user.mention} until {discord.utils.format_dt(end)}.")
|
|
|
|
|
|
|
|
@commands.slash_command(name="unblock")
|
|
|
|
@owner_or_admin()
|
|
|
|
async def unblock_user(self, ctx: discord.ApplicationContext, user: discord.Member):
|
|
|
|
"""Unblocks a user from using the bot."""
|
|
|
|
await ctx.defer()
|
|
|
|
entry = await get_or_none(JimmyBans, user_id=user.id)
|
|
|
|
if entry is None:
|
|
|
|
return await ctx.respond("\N{cross mark} That user isn't blocked.")
|
|
|
|
await entry.delete()
|
|
|
|
await ctx.respond(f"\N{white heavy check mark} Unblocked {user.mention}.")
|
|
|
|
|
2022-10-04 16:20:01 +01:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Mod(bot))
|