143 lines
4.0 KiB
Python
143 lines
4.0 KiB
Python
|
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.")
|