2024-03-18 17:48:12 +00:00
|
|
|
import asyncio
|
2024-06-20 18:57:45 +00:00
|
|
|
from loguru import logger
|
2024-03-18 17:48:12 +00:00
|
|
|
|
|
|
|
import discord
|
2024-07-08 17:22:17 +00:00
|
|
|
from discord.ext import bridge
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-29 17:17:51 +00:00
|
|
|
from config.parser import JsonCache
|
2024-07-08 17:22:17 +00:00
|
|
|
from lib.interactions.introduction import (
|
|
|
|
IntroductionStartButtons,
|
|
|
|
IntroductionFinishButtons,
|
|
|
|
)
|
2024-03-25 14:48:00 +00:00
|
|
|
from lib.embeds.error import MiscErrors, IntroErrors
|
|
|
|
from lib.embeds.intro import General, Questions
|
2024-07-08 18:20:58 +00:00
|
|
|
from typing import Optional
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
resources = JsonCache.read_json("resources")
|
2024-03-18 17:48:12 +00:00
|
|
|
|
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
async def cmd(self, ctx: bridge.Context) -> None:
|
2024-03-25 14:48:00 +00:00
|
|
|
"""
|
|
|
|
Introduction command for v2 - heavily optimized.
|
2024-07-08 17:22:17 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
self (LumiBot): The instance of the LumiBot.
|
|
|
|
ctx (bridge.Context): The context of the command invocation.
|
2024-03-25 14:48:00 +00:00
|
|
|
"""
|
2024-07-08 17:22:17 +00:00
|
|
|
# For now, this command is only supported in one guild.
|
|
|
|
# Therefore, we check if the user is in that guild.
|
|
|
|
guild: Optional[discord.Guild] = self.client.get_guild(
|
|
|
|
int(resources["guild_specific"]["guild_id"])
|
|
|
|
)
|
2024-06-20 18:57:45 +00:00
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
if guild is None:
|
|
|
|
await ctx.respond(embed=MiscErrors.intro_no_guild(ctx))
|
|
|
|
return
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
# A list of questions and corresponding field names
|
|
|
|
# This won't be hardcoded in the future (db update)
|
|
|
|
question_mapping: dict[str, str] = resources["guild_specific"]["question_mapping"]
|
|
|
|
|
2024-07-08 17:30:05 +00:00
|
|
|
# channel = await self.client.convert_to_text_channel(
|
|
|
|
# ctx, int(resources["guild_specific"]["intro_channel_id"])
|
|
|
|
# )
|
|
|
|
channel: Optional[discord.abc.GuildChannel] = guild.get_channel(int(resources["guild_specific"]["intro_channel_id"]))
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-07-08 17:31:47 +00:00
|
|
|
if channel is None or isinstance(channel, discord.ForumChannel) or isinstance(channel, discord.CategoryChannel):
|
2024-07-08 17:22:17 +00:00
|
|
|
await ctx.respond(embed=IntroErrors.intro_no_channel(ctx))
|
|
|
|
return
|
|
|
|
|
|
|
|
view = IntroductionStartButtons(ctx)
|
2024-03-25 14:48:00 +00:00
|
|
|
await ctx.respond(embed=General.start(ctx, channel), view=view)
|
2024-03-18 17:48:12 +00:00
|
|
|
await view.wait()
|
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
if view.clickedStop:
|
2024-07-08 17:22:17 +00:00
|
|
|
await ctx.send(embed=General.clicked_stop(ctx))
|
|
|
|
return
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
elif view.clickedStart:
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
def check(message: discord.Message) -> bool:
|
|
|
|
return message.author == ctx.author and isinstance(
|
|
|
|
message.channel, discord.DMChannel
|
|
|
|
)
|
|
|
|
|
|
|
|
answer_mapping: dict[str, str] = {}
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
for key, question in question_mapping.items():
|
|
|
|
await ctx.send(embed=Questions.question(ctx, question))
|
2024-03-18 17:48:12 +00:00
|
|
|
|
|
|
|
try:
|
2024-07-08 17:22:17 +00:00
|
|
|
answer: discord.Message = await self.client.wait_for(
|
|
|
|
"message", check=check, timeout=120
|
|
|
|
)
|
2024-03-25 14:48:00 +00:00
|
|
|
answer_mapping[key] = answer.content.replace("\n", " ")
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
if len(answer_mapping[key]) > 200:
|
2024-07-08 17:22:17 +00:00
|
|
|
await ctx.send(embed=IntroErrors.too_long(ctx))
|
|
|
|
return
|
2024-03-18 17:48:12 +00:00
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
2024-07-08 17:22:17 +00:00
|
|
|
await ctx.send(embed=IntroErrors.timeout(ctx))
|
|
|
|
return
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
# Generate a preview of the introduction, and send it on confirmation.
|
|
|
|
preview: discord.Embed = General.preview(ctx, answer_mapping)
|
|
|
|
view = IntroductionFinishButtons(ctx)
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
await ctx.send(embed=preview, view=view)
|
|
|
|
await view.wait()
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
if view.clickedConfirm:
|
2024-07-08 17:22:17 +00:00
|
|
|
await channel.send(
|
|
|
|
embed=preview, content=f"Introduction by {ctx.author.mention}"
|
|
|
|
)
|
2024-03-25 14:48:00 +00:00
|
|
|
await ctx.send(embed=General.post_confirmation(ctx, channel))
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-07-08 17:22:17 +00:00
|
|
|
logger.debug(
|
|
|
|
f"Introduction by {ctx.author.name} was submitted in guild {guild.name} ({guild.id})."
|
|
|
|
)
|
2024-03-25 14:48:00 +00:00
|
|
|
return
|
2024-03-18 17:48:12 +00:00
|
|
|
|
2024-03-25 14:48:00 +00:00
|
|
|
else:
|
|
|
|
await ctx.send(embed=General.clicked_stop(ctx))
|
2024-03-18 17:48:12 +00:00
|
|
|
return
|