Skip to content

Commit

Permalink
Add custom ticket title support
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Dec 28, 2023
1 parent ab781ee commit 231a402
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
5 changes: 1 addition & 4 deletions bot/cogs/tickets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import uuid
from functools import lru_cache
from typing import TYPE_CHECKING, NamedTuple, Optional, Union

Expand Down Expand Up @@ -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})",
)
Expand Down
1 change: 1 addition & 0 deletions bot/libs/tickets/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ThreadWithGuild(NamedTuple):


class TicketThread(msgspec.Struct):
title: str
user: Union[discord.User, discord.Member]
location_id: int
content: str
Expand Down
46 changes: 43 additions & 3 deletions bot/libs/tickets/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 231a402

Please sign in to comment.