From 231a402cb73c00f8ca61bb971b0a80daab638da3 Mon Sep 17 00:00:00 2001 From: No767 <73260931+No767@users.noreply.github.com> Date: Thu, 28 Dec 2023 01:55:09 -0800 Subject: [PATCH] Add custom ticket title support --- bot/cogs/tickets.py | 5 +--- bot/libs/tickets/structs.py | 1 + bot/libs/tickets/views.py | 46 ++++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/bot/cogs/tickets.py b/bot/cogs/tickets.py index 35bbeab..758748e 100644 --- a/bot/cogs/tickets.py +++ b/bot/cogs/tickets.py @@ -1,6 +1,5 @@ from __future__ import annotations -import uuid from functools import lru_cache from typing import TYPE_CHECKING, NamedTuple, Optional, Union @@ -193,11 +192,9 @@ async def create_ticket(self, ticket: TicketThread) -> Optional[TicketOutput]: # TODO: Add file attachment support later - thread_display_id = uuid.uuid4() - thread_name = f"{ticket.user.display_name} | {thread_display_id}" content = f"({ticket.user.display_name}, {discord.utils.format_dt(ticket.created_at)})\n\n{ticket.content}" created_ticket = await tc.create_thread( - name=thread_name, + name=ticket.title, content=content, reason=f"Ticket submitted by {ticket.user.global_name} (ID: {ticket.user.id})", ) diff --git a/bot/libs/tickets/structs.py b/bot/libs/tickets/structs.py index 290bcba..c9a5652 100644 --- a/bot/libs/tickets/structs.py +++ b/bot/libs/tickets/structs.py @@ -20,6 +20,7 @@ class ThreadWithGuild(NamedTuple): class TicketThread(msgspec.Struct): + title: str user: Union[discord.User, discord.Member] location_id: int content: str diff --git a/bot/libs/tickets/views.py b/bot/libs/tickets/views.py index c1bc170..181848f 100644 --- a/bot/libs/tickets/views.py +++ b/bot/libs/tickets/views.py @@ -1,11 +1,12 @@ from __future__ import annotations import asyncio -from typing import TYPE_CHECKING +import uuid +from typing import TYPE_CHECKING, Optional import discord from libs.tickets.structs import TicketThread -from libs.utils import ErrorEmbed, RoboView +from libs.utils import ErrorEmbed, RoboModal, RoboView from .utils import register_user, safe_content @@ -16,6 +17,31 @@ from bot.rodhaj import Rodhaj +class TicketTitleModal(RoboModal, title="Ticket Title"): + def __init__(self, ctx: RoboContext, *args, **kwargs): + super().__init__(ctx=ctx, *args, **kwargs) + + self.title_input = discord.ui.TextInput( + label="Title", + style=discord.TextStyle.long, + placeholder="Input a title...", + min_length=20, + max_length=100, + ) + self.input: Optional[str] = None + self.add_item(self.title_input) + + async def on_submit( + self, interaction: discord.Interaction[Rodhaj] + ) -> Optional[str]: + self.input = self.title_input.value + await interaction.response.send_message( + f"The title of the ticket is set to: `{self.title_input.value}`", + ephemeral=True, + ) + return self.input + + class TicketConfirmView(RoboView): def __init__( self, @@ -26,7 +52,7 @@ def __init__( guild: discord.Guild, delete_after: bool = True, ) -> None: - super().__init__(ctx=ctx, timeout=10.0) + super().__init__(ctx=ctx, timeout=300.0) self.bot = bot self.ctx = ctx self.cog = cog @@ -35,6 +61,7 @@ def __init__( self.delete_after = delete_after self.triggered = asyncio.Event() self.pool = self.bot.pool + self._modal = None async def delete_response(self, interaction: discord.Interaction): await interaction.response.defer() @@ -43,6 +70,13 @@ async def delete_response(self, interaction: discord.Interaction): self.stop() + @discord.ui.button(label="Set Title", style=discord.ButtonStyle.blurple, row=1) + async def set_title( + self, interaction: discord.Interaction, button: discord.ui.Button + ) -> None: + self._modal = TicketTitleModal(self.ctx) + await interaction.response.send_modal(self._modal) + @discord.ui.button( label="Confirm", style=discord.ButtonStyle.green, @@ -54,7 +88,13 @@ async def confirm( ) -> None: await register_user(self.ctx.author.id, self.pool) author = self.ctx.author + + # TODO: Probably add user config defaults instead + thread_display_id = uuid.uuid4() + thread_name = f"{author.display_name} | {thread_display_id}" + title = self._modal.input if self._modal and self._modal.input else thread_name ticket = TicketThread( + title=title, user=author, location_id=self.guild.id, content=self.content,