Files
QuizBot/cogs/event.py
2025-04-18 23:34:11 +02:00

120 lines
4.0 KiB
Python

from datetime import datetime
from zoneinfo import ZoneInfo
from discord import ApplicationContext, Attachment, SlashCommandGroup, option
from discord.ext.commands import Cog
from classes import PycordEvent, PycordGuild
from classes.pycord_bot import PycordBot
class Event(Cog):
"""Cog with event management commands."""
def __init__(self, bot: PycordBot):
self.bot: PycordBot = bot
# TODO Introduce i18n
command_group: SlashCommandGroup = SlashCommandGroup("event", "Event management")
# TODO Implement the command
@command_group.command(
name="create",
description="Create new event",
)
@option("name", description="Name of the event", required=True)
@option("start", description="Date when the event starts (DD.MM.YYYY HH:MM)", required=True)
@option("finish", description="Date when the event finishes (DD.MM.YYYY HH:MM)", required=True)
@option("thumbnail", description="Thumbnail of the event", required=False)
async def command_event_create(
self,
ctx: ApplicationContext,
name: str,
start: str,
finish: str,
thumbnail: Attachment = None,
) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
if not guild.is_configured():
await ctx.respond("Guild is not configured.")
return
guild_timezone: ZoneInfo = ZoneInfo(guild.timezone)
try:
start_date: datetime = datetime.strptime(start, "%d.%m.%Y %H:%M")
finish_date: datetime = datetime.strptime(finish, "%d.%m.%Y %H:%M")
start_date = start_date.replace(tzinfo=guild_timezone)
finish_date = finish_date.replace(tzinfo=guild_timezone)
except ValueError:
await ctx.respond("Could not parse start and finish dates.")
return
if start_date > finish_date:
await ctx.respond("Start date must be before finish date")
return
elif start_date < datetime.now(tz=guild_timezone):
await ctx.respond("Start date must not be in the past")
return
event: PycordEvent = await self.bot.create_event(
name=name,
guild_id=guild.id,
creator_id=ctx.author.id,
starts=start_date.astimezone(ZoneInfo("UTC")),
ends=finish_date.astimezone(ZoneInfo("UTC")),
thumbnail_id=thumbnail.id if thumbnail else None,
)
await ctx.respond("Event has been created.")
# TODO Implement the command
@command_group.command(
name="edit",
description="Edit event",
)
@option("event", description="Name of the event", required=True)
@option("name", description="New name of the event", required=False)
@option("start", description="Date when the event starts (DD.MM.YYYY HH:MM)", required=False)
@option("finish", description="Date when the event finishes (DD.MM.YYYY HH:MM)", required=False)
@option("thumbnail", description="Thumbnail of the event", required=False)
async def command_event_edit(
self,
ctx: ApplicationContext,
event: str,
name: str,
start: str,
finish: str,
thumbnail: Attachment = None,
) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
if not guild.is_configured():
await ctx.respond("Guild is not configured.")
return
guild_timezone: ZoneInfo = ZoneInfo(guild.timezone)
await ctx.respond("Not implemented.")
# TODO Implement the command
@command_group.command(
name="cancel",
description="Cancel event",
)
@option("name", description="Name of the event", required=True)
async def command_event_cancel(
self,
ctx: ApplicationContext,
name: str,
) -> None:
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
await ctx.respond("Not implemented.")
def setup(bot: PycordBot) -> None:
bot.add_cog(Event(bot))