2023-02-06 20:20:40 +01:00
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from database import GuildSettings
|
|
|
|
|
2023-05-31 20:07:39 +02:00
|
|
|
from interactions import (
|
2023-02-06 20:20:40 +01:00
|
|
|
Extension,
|
|
|
|
Client,
|
|
|
|
slash_command,
|
|
|
|
slash_option,
|
|
|
|
InteractionContext,
|
|
|
|
ModalContext,
|
|
|
|
Permissions,
|
2023-05-31 20:07:39 +02:00
|
|
|
OptionType,
|
2023-02-06 20:20:40 +01:00
|
|
|
Button,
|
2023-05-31 20:07:39 +02:00
|
|
|
ButtonStyle,
|
2023-02-06 20:20:40 +01:00
|
|
|
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.",
|
2023-05-31 20:07:39 +02:00
|
|
|
opt_type=OptionType.STRING,
|
2023-02-06 20:20:40 +01:00
|
|
|
required=True
|
|
|
|
)
|
|
|
|
@slash_option(
|
|
|
|
name="notify-role",
|
|
|
|
description="Role that should be tagged upon creation of the modmail thread.",
|
2023-05-31 20:07:39 +02:00
|
|
|
opt_type=OptionType.ROLE,
|
2023-02-06 20:20:40 +01:00
|
|
|
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(
|
2023-05-31 20:07:39 +02:00
|
|
|
style=ButtonStyle.GREEN,
|
2023-02-06 20:20:40 +01:00
|
|
|
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(
|
2023-05-31 20:07:39 +02:00
|
|
|
style=ButtonStyle.LINK,
|
2023-02-06 20:20:40 +01:00
|
|
|
label="Go to message",
|
|
|
|
url=msg.jump_url,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def setup(client: Client):
|
|
|
|
ModMail(client)
|
|
|
|
logging.info(f"{ModMail.__name__} extension loaded.")
|