from discord import ( ApplicationCommandInvokeError, ApplicationContext, InputTextStyle, Interaction, slash_command, ) from discord.ext.commands import Cog, MissingPermissions, has_guild_permissions from discord.ui import InputText, Modal from src.bot import ConjointBot class TemplateModal(Modal): def __init__(self, bot: ConjointBot, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.bot = bot self.add_item( InputText( style=InputTextStyle.paragraph, label="Template", placeholder=""" Valid variables: !keyword! !channels! !user_name! !user_id! """, required=True, ) ) async def callback(self, interaction: Interaction) -> None: await interaction.response.defer() self.bot.db.set("template", self.children[0].value) await interaction.followup.send( ":saluting_face: Updated message template.", ephemeral=True ) async def on_error( self, error: ApplicationCommandInvokeError, interaction: Interaction ) -> None: if isinstance(error, ApplicationCommandInvokeError): error = error.original self.bot.logger.error(error, exc_info=True) await interaction.followup.send( f""" :confused: An error occurred. {error} """, ephemeral=True, ) class TemplateCog(Cog): def __init__(self, bot: ConjointBot) -> None: self.bot = bot @slash_command( name="template", description="Set a new message template (requires Manage Server)", guild_only=True, ) @has_guild_permissions(manage_guild=True) async def template_template(self, ctx: ApplicationContext) -> None: modal = TemplateModal(self.bot, title="Edit Template") await ctx.send_modal(modal) async def cog_command_error( self, ctx: ApplicationContext, error: ApplicationCommandInvokeError ) -> None: if isinstance(error.original, MissingPermissions): await ctx.respond( "You aren't cool enough to use this command. :neutral_face:", ephemeral=True, ) else: if isinstance(error, ApplicationCommandInvokeError): error = error.original self.bot.logger.error(error, exc_info=True) await ctx.respond( f""" :confused: An error occurred. {error} """, ephemeral=True, ) def setup(bot: ConjointBot) -> None: bot.add_cog(TemplateCog(bot))