62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
from bson.errors import InvalidId
|
|
from discord import ApplicationContext, Cog, option, slash_command
|
|
from discord.utils import basic_autocomplete
|
|
|
|
from classes import PycordEvent, PycordGuild, PycordUser
|
|
from classes.pycord_bot import PycordBot
|
|
from modules.utils import autocomplete_active_events, get_unix_timestamp
|
|
|
|
|
|
class CogRegister(Cog):
|
|
"""Cog with the event registration command."""
|
|
|
|
def __init__(self, bot: PycordBot):
|
|
self.bot: PycordBot = bot
|
|
|
|
# TODO Introduce i18n
|
|
@slash_command(
|
|
name="register",
|
|
description="Enter the selected event",
|
|
)
|
|
@option(
|
|
"event",
|
|
description="Name of the event",
|
|
autocomplete=basic_autocomplete(autocomplete_active_events),
|
|
)
|
|
async def command_register(self, ctx: ApplicationContext, event: str) -> None:
|
|
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
|
|
|
|
try:
|
|
pycord_event: PycordEvent = await self.bot.find_event(event_id=event)
|
|
except (InvalidId, RuntimeError):
|
|
# TODO Make a nice message
|
|
await ctx.respond("Event was not found.")
|
|
return
|
|
|
|
if not guild.is_configured():
|
|
await ctx.respond(self.bot._("guild_unconfigured", "messages", locale=ctx.locale))
|
|
return
|
|
|
|
user: PycordUser = await self.bot.find_user(ctx.author, ctx.guild)
|
|
|
|
if user.is_jailed:
|
|
# TODO Make a nice message
|
|
await ctx.respond("You are jailed and cannot interact with events. Please, contact the administrator.")
|
|
return
|
|
|
|
if pycord_event._id in user.registered_event_ids:
|
|
# TODO Make a nice message
|
|
await ctx.respond("You are already registered for this event.")
|
|
return
|
|
|
|
await user.event_register(pycord_event._id, cache=self.bot.cache)
|
|
|
|
# TODO Make a nice message
|
|
await ctx.respond(
|
|
f"You are now registered for the event **{pycord_event.name}**.\n\nNew channel will be created for you and further instructions will be provided as soon as the event starts <t:{get_unix_timestamp(pycord_event.starts)}:R>. Good luck!"
|
|
)
|
|
|
|
|
|
def setup(bot: PycordBot) -> None:
|
|
bot.add_cog(CogRegister(bot))
|