Implemented "/user update_channels" (#10)
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from bson import ObjectId
|
||||
from bson.errors import InvalidId
|
||||
from discord import (
|
||||
ApplicationContext,
|
||||
SlashCommandGroup,
|
||||
User,
|
||||
option,
|
||||
File,
|
||||
TextChannel,
|
||||
)
|
||||
from discord.ext.commands import Cog
|
||||
from libbot.i18n import _, in_every_locale
|
||||
|
||||
from classes import PycordUser
|
||||
from classes import PycordUser, PycordEvent, PycordGuild
|
||||
from classes.errors import GuildNotFoundError
|
||||
from classes.pycord_bot import PycordBot
|
||||
from modules.database import col_users
|
||||
from modules.utils import is_operation_confirmed
|
||||
|
||||
|
||||
@@ -37,16 +48,82 @@ class CogUser(Cog):
|
||||
# await ctx.respond("Not implemented.")
|
||||
|
||||
# TODO Implement the command
|
||||
# @command_group.command(
|
||||
# name="update_channel",
|
||||
# description="Update user's channel",
|
||||
# )
|
||||
# @option(
|
||||
# "user",
|
||||
# description="Selected user",
|
||||
# )
|
||||
# async def command_user_update_channel(self, ctx: ApplicationContext, user: User) -> None:
|
||||
# await ctx.respond("Not implemented.")
|
||||
@command_group.command(
|
||||
name="update_channels",
|
||||
description="Update user's event channels",
|
||||
)
|
||||
@option(
|
||||
"user",
|
||||
description="Selected user",
|
||||
)
|
||||
async def command_user_update_channels(self, ctx: ApplicationContext, user: User) -> None:
|
||||
try:
|
||||
guild: PycordGuild = await self.bot.find_guild(ctx.guild.id)
|
||||
except (InvalidId, GuildNotFoundError):
|
||||
await ctx.respond(self.bot._("unexpected_error", "messages", locale=ctx.locale))
|
||||
return
|
||||
|
||||
pycord_user: PycordUser = await self.bot.find_user(user.id, ctx.guild.id)
|
||||
events: List[PycordEvent] = []
|
||||
|
||||
pipeline: List[Dict[str, Any]] = [
|
||||
{"$match": {"id": pycord_user.id}},
|
||||
{
|
||||
"$lookup": {
|
||||
"from": "events",
|
||||
"localField": "registered_event_ids",
|
||||
"foreignField": "_id",
|
||||
"as": "registered_events",
|
||||
}
|
||||
},
|
||||
{
|
||||
"$match": {
|
||||
"registered_events.ended": None,
|
||||
"registered_events.ends": {"$gt": datetime.now(tz=ZoneInfo("UTC"))},
|
||||
"registered_events.starts": {"$lt": datetime.now(tz=ZoneInfo("UTC"))},
|
||||
"registered_events.is_cancelled": False,
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
async for result in col_users.aggregate(pipeline):
|
||||
for registered_event in result["registered_events"]:
|
||||
events.append(PycordEvent(**registered_event))
|
||||
|
||||
for event in events:
|
||||
if pycord_user.current_event_id is not None and pycord_user.current_event_id != event._id:
|
||||
continue
|
||||
|
||||
if pycord_user.current_event_id is None:
|
||||
await pycord_user.set_event(event._id, cache=self.bot.cache)
|
||||
|
||||
channel: TextChannel | None = await pycord_user.fix_event_channel(
|
||||
self.bot, ctx.guild, guild, event, cache=self.bot.cache
|
||||
)
|
||||
|
||||
if channel is None:
|
||||
continue
|
||||
|
||||
thumbnail: File | None = (
|
||||
None
|
||||
if event.thumbnail is None
|
||||
else File(Path(f"data/{event.thumbnail['id']}"), event.thumbnail["filename"])
|
||||
)
|
||||
|
||||
await channel.send(
|
||||
self.bot._("notice_event_already_started", "messages").format(event_name=event.name),
|
||||
file=thumbnail,
|
||||
)
|
||||
|
||||
stage_id: ObjectId = (
|
||||
event.stage_ids[0] if pycord_user.current_stage_id is None else pycord_user.current_stage_id
|
||||
)
|
||||
|
||||
await pycord_user.set_event_stage(stage_id, cache=self.bot.cache)
|
||||
|
||||
await self.bot.send_stage_question(channel, event, await self.bot.find_event_stage(stage_id))
|
||||
|
||||
await ctx.respond("Channels were updated.")
|
||||
|
||||
# TODO Implement the command
|
||||
# @command_group.command(
|
||||
|
Reference in New Issue
Block a user