1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-03 04:23:14 +00:00
Lumi/modules/misc/introduction.py

163 lines
5.4 KiB
Python
Raw Normal View History

2024-03-18 17:48:12 +00:00
import asyncio
from typing import Optional, Dict
2024-03-18 17:48:12 +00:00
import discord
from discord.ext import bridge
2024-03-18 17:48:12 +00:00
from lib.constants import CONST
from lib.embed_builder import EmbedBuilder
from lib.interactions.introduction import (
IntroductionStartButtons,
IntroductionFinishButtons,
)
2024-03-18 17:48:12 +00:00
async def cmd(self, ctx: bridge.Context) -> None:
guild: Optional[discord.Guild] = self.client.get_guild(CONST.KRC_GUILD_ID)
member: Optional[discord.Member] = (
guild.get_member(ctx.author.id) if guild else None
)
if not guild or not member:
await ctx.respond(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_no_guild_author"],
description=CONST.STRINGS["intro_no_guild"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)
return
2024-03-18 17:48:12 +00:00
question_mapping: Dict[str, str] = CONST.KRC_QUESTION_MAPPING
channel: Optional[discord.abc.GuildChannel] = guild.get_channel(
CONST.KRC_INTRO_CHANNEL_ID
)
2024-03-18 17:48:12 +00:00
if not channel or isinstance(
channel, (discord.ForumChannel, discord.CategoryChannel)
):
await ctx.respond(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_no_channel_author"],
description=CONST.STRINGS["intro_no_channel"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)
return
view: IntroductionStartButtons | IntroductionFinishButtons = (
IntroductionStartButtons(ctx)
)
await ctx.respond(
embed=EmbedBuilder.create_embed(
ctx,
author_text=CONST.STRINGS["intro_service_name"],
description=CONST.STRINGS["intro_start"].format(channel.mention),
footer_text=CONST.STRINGS["intro_start_footer"],
),
view=view,
)
2024-03-18 17:48:12 +00:00
await view.wait()
2024-03-25 14:48:00 +00:00
if view.clickedStop:
await ctx.send(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_stopped_author"],
description=CONST.STRINGS["intro_stopped"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)
return
2024-03-18 17:48:12 +00:00
if view.clickedStart:
2024-03-18 17:48:12 +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=EmbedBuilder.create_embed(
ctx,
author_text=key,
description=question,
footer_text=CONST.STRINGS["intro_question_footer"],
)
)
2024-03-18 17:48:12 +00:00
try:
answer: discord.Message = await self.client.wait_for(
"message", check=check, timeout=120
)
answer_content: str = answer.content.replace("\n", " ")
2024-03-18 17:48:12 +00:00
if len(answer_content) > 200:
await ctx.send(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_too_long_author"],
description=CONST.STRINGS["intro_too_long"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)
return
2024-03-18 17:48:12 +00:00
answer_mapping[key] = answer_content
2024-03-18 17:48:12 +00:00
except asyncio.TimeoutError:
await ctx.send(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_timeout_author"],
description=CONST.STRINGS["intro_timeout"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)
return
2024-03-18 17:48:12 +00:00
description: str = "".join(
CONST.STRINGS["intro_preview_field"].format(key, value)
for key, value in answer_mapping.items()
)
preview: discord.Embed = EmbedBuilder.create_embed(
ctx,
author_text=ctx.author.name,
author_icon_url=ctx.author.display_avatar.url,
description=description,
footer_text=CONST.STRINGS["intro_service_name"],
)
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:
await channel.send(
embed=preview,
content=CONST.STRINGS["intro_content"].format(ctx.author.mention),
)
await ctx.send(
embed=EmbedBuilder.create_embed(
ctx,
description=CONST.STRINGS["intro_post_confirmation"].format(
channel.mention
),
)
)
2024-03-25 14:48:00 +00:00
else:
await ctx.send(
embed=EmbedBuilder.create_error_embed(
ctx,
author_text=CONST.STRINGS["intro_stopped_author"],
description=CONST.STRINGS["intro_stopped"],
footer_text=CONST.STRINGS["intro_service_name"],
)
)