Skip to content

Commit

Permalink
Add file attachment support for tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Jan 2, 2024
1 parent ef1684f commit 1641e7a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bot/cogs/tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

if TYPE_CHECKING:
from libs.utils import RoboContext

from rodhaj import Rodhaj


Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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})",
)

Expand Down Expand Up @@ -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]]
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 @@ -31,6 +31,7 @@ class TicketThread(msgspec.Struct):
location_id: int
content: str
tags: list[str]
files: list[discord.File]
created_at: datetime.datetime


Expand Down
4 changes: 4 additions & 0 deletions bot/libs/tickets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion bot/rodhaj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 1641e7a

Please sign in to comment.