From db4e353bb00b6f76345c3d6a6a20d316ed621053 Mon Sep 17 00:00:00 2001 From: Vegard Berg Date: Mon, 6 Feb 2023 20:20:40 +0100 Subject: [PATCH] Add modmail feature --- heimdallr/Heimdallr.py | 1 + heimdallr/commands/modmail.py | 142 ++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 heimdallr/commands/modmail.py diff --git a/heimdallr/Heimdallr.py b/heimdallr/Heimdallr.py index ff76dc7..ce73ee5 100644 --- a/heimdallr/Heimdallr.py +++ b/heimdallr/Heimdallr.py @@ -184,6 +184,7 @@ def main(): bot.load_extension("heimdallr.commands.self_roles") bot.load_extension("heimdallr.commands.polls") bot.load_extension("heimdallr.commands.bot_messages") + bot.load_extension("heimdallr.commands.modmail") bot.start(getenv("DISCORD_TOKEN")) if __name__ == "__main__": diff --git a/heimdallr/commands/modmail.py b/heimdallr/commands/modmail.py new file mode 100644 index 0000000..4ac1338 --- /dev/null +++ b/heimdallr/commands/modmail.py @@ -0,0 +1,142 @@ +import logging +import uuid + +from database import GuildSettings + +from naff import ( + Extension, + Client, + slash_command, + slash_option, + InteractionContext, + ModalContext, + Permissions, + OptionTypes, + Button, + ButtonStyles, + listen, + events, + Modal, + InputText, + TextStyles, + Role, + Embed, + EmbedAuthor, +) + +class ModMail(Extension): + def __init__(self, client: Client): + self.client: Client = client + + @slash_command( + name="adm", + sub_cmd_name="create-modmail-button", + sub_cmd_description="Create a button for users to create a modmail thread.", + dm_permission=False, + default_member_permissions=Permissions.MANAGE_GUILD, + ) + @slash_option( + name="button-text", + description="The text that should be displayed on the button.", + opt_type=OptionTypes.STRING, + required=True + ) + @slash_option( + name="notify-role", + description="Role that should be tagged upon creation of the modmail thread.", + opt_type=OptionTypes.ROLE, + required=True + ) + async def adm_create_modmail_button(self, ctx: InteractionContext, button_text: str, role: Role): + await ctx.defer(ephemeral=True) + + await ctx.channel.send( + components=Button( + style=ButtonStyles.GREEN, + label=button_text, + custom_id=f"send-modmail-button:{role.id}" + ) + ) + + await ctx.send("Button created!", ephemeral=True) + + @listen(events.ButtonPressed) + async def on_button(self, button: events.ButtonPressed): + ctx = button.ctx + + if not ctx.custom_id.startswith("send-modmail-button"): + return + + (_, role) = ctx.custom_id.split(":") + if role is None: + return + + role = await ctx.guild.fetch_role(role) + if role is None: + return + + modmail_modal = Modal( + title="Send modmail", + custom_id=f"modmail-modal:{uuid.uuid4()}", + components=[ + InputText( + label="Topic", + style=TextStyles.SHORT, + custom_id="modmail_title", + required=True, + min_length=5, + max_length=100, + placeholder="Title or topic of the modmail" + ), + InputText( + label="Message", + style=TextStyles.PARAGRAPH, + custom_id="modmail_body", + required=True, + min_length=24, + max_length=2000, + ) + ] + ) + + await ctx.send_modal(modmail_modal) + modal_ctx: ModalContext = await self.client.wait_for_modal(modmail_modal) + + thread = await ctx.channel.create_private_thread( + name=modal_ctx.responses["modmail_title"], + invitable=True, + reason=( + f"{ctx.author.username}#{ctx.author.discriminator} " + "created a support thread." + ) + ) + + msg = await thread.send( + content=f"{role.mention} {ctx.author.mention}", + embed=Embed( + title=modal_ctx.responses["modmail_title"], + description=modal_ctx.responses["modmail_body"], + author=EmbedAuthor( + name=( + f"{ctx.author.nickname} " + f"({ctx.author.username}#{ctx.author.discriminator})" + ), + icon_url=ctx.author.avatar.as_url() + ) + ) + ) + + if msg: + await modal_ctx.send( + "Message created", + ephemeral=True, + components=Button( + style=ButtonStyles.LINK, + label="Go to message", + url=msg.jump_url, + ) + ) + +def setup(client: Client): + ModMail(client) + logging.info(f"{ModMail.__name__} extension loaded.")