From 1641e7a861a96671cd1a29064077b200feb3ba7e Mon Sep 17 00:00:00 2001 From: No767 <73260931+No767@users.noreply.github.com> Date: Tue, 2 Jan 2024 15:47:58 -0800 Subject: [PATCH] Add file attachment support for tickets --- bot/cogs/tickets.py | 8 ++++---- bot/libs/tickets/structs.py | 1 + bot/libs/tickets/views.py | 4 ++++ bot/rodhaj.py | 13 ++++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bot/cogs/tickets.py b/bot/cogs/tickets.py index 06776bc..ca6f58e 100644 --- a/bot/cogs/tickets.py +++ b/bot/cogs/tickets.py @@ -15,6 +15,7 @@ if TYPE_CHECKING: from libs.utils import RoboContext + from rodhaj import Rodhaj @@ -43,7 +44,7 @@ class TicketOutput(NamedTuple): class ClosedEmbed(discord.Embed): def __init__(self, **kwargs): kwargs.setdefault("color", discord.Color.from_rgb(138, 255, 157)) - kwargs.setdefault("title", "SUCCESS_TICK_EMOJI Ticket Closed") + kwargs.setdefault("title", "\U00002705 Ticket Closed") kwargs.setdefault("timestamp", discord.utils.utcnow()) super().__init__(**kwargs) self.set_footer(text="Ticket closed at") @@ -197,8 +198,6 @@ async def create_ticket(self, ticket: TicketThread) -> Optional[TicketOutput]: if not isinstance(tc, discord.ForumChannel): return - # TODO: Add file attachment support later - all_tags = tc.available_tags applied_tags = [ discord.utils.get(all_tags, name=tag.title()) for tag in ticket.tags @@ -210,6 +209,7 @@ async def create_ticket(self, ticket: TicketThread) -> Optional[TicketOutput]: applied_tags=processed_tags, name=ticket.title, content=content, + files=ticket.files, reason=f"Ticket submitted by {ticket.user.global_name} (ID: {ticket.user.id})", ) @@ -261,7 +261,7 @@ async def obtain_webhook(self, guild_id: int) -> Optional[discord.Webhook]: return await dispatcher.get_webhook() async def tick_post(self, ctx: RoboContext) -> None: - await ctx.message.add_reaction(discord.PartialEmoji(name="SUCCESS_TICK_EMOJI")) + await ctx.message.add_reaction(discord.PartialEmoji(name="\U00002705")) def get_solved_tag( self, channel: Optional[Union[discord.ForumChannel, discord.TextChannel]] diff --git a/bot/libs/tickets/structs.py b/bot/libs/tickets/structs.py index 59dcc47..82e1842 100644 --- a/bot/libs/tickets/structs.py +++ b/bot/libs/tickets/structs.py @@ -31,6 +31,7 @@ class TicketThread(msgspec.Struct): location_id: int content: str tags: list[str] + files: list[discord.File] created_at: datetime.datetime diff --git a/bot/libs/tickets/views.py b/bot/libs/tickets/views.py index a3323c8..afbd7fc 100644 --- a/bot/libs/tickets/views.py +++ b/bot/libs/tickets/views.py @@ -129,6 +129,7 @@ async def callback(self, interaction: discord.Interaction[Rodhaj]) -> None: class TicketConfirmView(RoboView): def __init__( self, + attachments: list[discord.Attachment], bot: Rodhaj, ctx: RoboContext, cog: Tickets, @@ -137,6 +138,7 @@ def __init__( delete_after: bool = True, ) -> None: super().__init__(ctx=ctx, timeout=300.0) + self.attachments = attachments self.bot = bot self.ctx = ctx self.cog = cog @@ -248,12 +250,14 @@ async def confirm( await interaction.response.send_message(embed=embed, ephemeral=True) return + files = [await attachment.to_file() for attachment in self.attachments] ticket = TicketThread( title=title, user=author, location_id=self.guild.id, content=self.content, tags=applied_tags, + files=files, created_at=discord.utils.utcnow(), ) created_ticket = await self.cog.create_ticket(ticket) diff --git a/bot/rodhaj.py b/bot/rodhaj.py index 36d6ad0..9b5b54d 100644 --- a/bot/rodhaj.py +++ b/bot/rodhaj.py @@ -137,6 +137,15 @@ async def on_message(self, message: discord.Message) -> None: # Represents that there is no active ticket if potential_ticket.id is None: + # We might want to validate the content type here... + if len(message.attachments) > 10: + over_msg = ( + "There are more than 10 attachments linked. " + "Please remove some and try again" + ) + await author.send(over_msg) + return + tickets_cog: Tickets = self.get_cog("Tickets") # type: ignore default_tags = ReservedTags( question=False, serious=False, private=False @@ -161,7 +170,9 @@ async def on_message(self, message: discord.Message) -> None: "\n\nNote: Once you have created your ticket, this prompt will not show up again" ) - view = TicketConfirmView(self, ctx, tickets_cog, message.content, guild) + view = TicketConfirmView( + message.attachments, self, ctx, tickets_cog, message.content, guild + ) view.message = await author.send(embed=embed, view=view) return