Add modmail feature
continuous-integration/drone Build is failing Details

This commit is contained in:
Vegard Berg 2023-02-06 20:20:40 +01:00
parent 09674aa86b
commit db4e353bb0
2 changed files with 143 additions and 0 deletions

View File

@ -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__":

View File

@ -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.")