Tickets WIP

This commit is contained in:
Soenke committed 2026-07-19 20:45:43 +02:00
1 parent 150c36da28
commit e23950b93c
3 files changed
+162 -10

No files matched your search

+141 -8
View File
@@ -5,7 +5,80 @@ from dotenv import load_dotenv
load_dotenv()
async def CreateTicketChannel(interaction: discord.Interaction, chosen_language):
raw_ticket_admin_ids = os.environ.get("TICKET_ADMIN_ROLE_IDS", "")
TICKET_ADMIN_ROLES = [int(role_id) for role_id in raw_ticket_admin_ids.split(",") if role_id]
# TODO: English/Deutsch
class TicketControlsView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label="Claim Ticket", style=discord.ButtonStyle.success, emoji="🙌", custom_id="claim_ticket")
async def claim_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
user_role_ids = [role.id for role in interaction.user.roles]
isAllowed = any(role_id in TICKET_ADMIN_ROLES for role_id in user_role_ids)
if isAllowed:
button.disabled = True
button.label = f"Claimed by {interaction.user.display_name}"
await interaction.response.edit_message(view=self)
await interaction.followup.send(f"{interaction.user.mention} hat dieses Ticket übernommen!", ephemeral=False)
bot = interaction.client
guild_id = int(os.getenv("GUILD_ID"))
category_id = int(os.getenv("TICKET_CLAIMED_CATEGORY_ID"))
guild = bot.get_guild(guild_id)
if not guild:
print("Error: Guild not found")
return None
category = guild.get_channel(category_id)
if not category or not isinstance(category, discord.CategoryChannel):
print("Error: Category not found")
return None
await interaction.message.channel.edit(category=category, sync_permissions=False)
else:
await interaction.response.send_message("Keine Berechtigungen für diese Aktion!", ephemeral=True)
@discord.ui.button(label="Add User", style=discord.ButtonStyle.blurple, emoji="🫴", custom_id="add_user")
async def add_user_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
user_role_ids = [role.id for role in interaction.user.roles]
isAllowed = any(role_id in TICKET_ADMIN_ROLES for role_id in user_role_ids)
if isAllowed:
await interaction.response.send_message(f"TODO", ephemeral=True)
else:
await interaction.response.send_message("Keine Berechtigungen für diese Aktion!", ephemeral=True)
@discord.ui.button(label="Close Ticket", style=discord.ButtonStyle.danger, emoji="🔒", custom_id="close_ticket")
async def close_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message(f"Ticket wurde von {interaction.user.mention} geschlossen!", ephemeral=False)
bot = interaction.client
guild_id = int(os.getenv("GUILD_ID"))
category_id = int(os.getenv("TICKET_CLOSED_CATEGORY_ID"))
guild = bot.get_guild(guild_id)
if not guild:
print("Error: Guild not found")
return None
category = guild.get_channel(category_id)
if not category or not isinstance(category, discord.CategoryChannel):
print("Error: Category not found")
return None
await interaction.message.channel.edit(category=category, sync_permissions=True)
#await interaction.channel.delete(reason="Ticket closed")
async def CreateTicketChannel(interaction: discord.Interaction, chosen_language, reason):
bot = interaction.client
guild_id = int(os.getenv("GUILD_ID"))
@@ -13,15 +86,23 @@ async def CreateTicketChannel(interaction: discord.Interaction, chosen_language)
guild = bot.get_guild(guild_id)
if not guild:
print("Error: Guild not found. Check your GUILD ID.")
print("Error: Guild not found")
return None
category = guild.get_channel(category_id)
if not category or not isinstance(category, discord.CategoryChannel):
print("Error: Category not found or is not a category channel.")
print("Error: Category not found")
return None
db = bot.database.conn
cursor = await db.execute(
"INSERT INTO tickets (user_id, language, reason) VALUES (?, ?, ?)",
(interaction.user.id, chosen_language, reason)
)
await db.commit()
num = 1
num = cursor.lastrowid
num_str = f"{num:04d}"
emoji = "🇩🇪" if chosen_language == "de" else "🇺🇸"
@@ -29,6 +110,57 @@ async def CreateTicketChannel(interaction: discord.Interaction, chosen_language)
ticket_channel = await category.create_text_channel(name=channel_name)
await db.execute(
"UPDATE tickets SET ticket_channel_id = ? WHERE ticket_id = ?",
(ticket_channel.id, num)
)
await db.commit()
if chosen_language == "de":
embed = discord.Embed(
description=f"""
# Ticket {num_str}
### Benutzer: {interaction.user.mention}
### Grund: ```{reason}```
""",
color=10289407
)
else:
embed = discord.Embed(
description=f"""
# Ticket {num_str}
### User: {interaction.user.mention}
### Reason: ```{reason}```
""",
color=10289407
)
await ticket_channel.send(
embed=embed,
view=TicketControlsView(),
allowed_mentions=discord.AllowedMentions(users=False)
)
overwrites = ticket_channel.overwrites_for(interaction.user)
overwrites.view_channel = True
overwrites.read_message_history = True
await ticket_channel.set_permissions(interaction.user, overwrite=overwrites)
addUserText = f"{interaction.user.mention} wurde erfolgreich zu {ticket_channel.mention} hinzugefügt!" if chosen_language == "de" else f"Successfully added {interaction.user.mention} to {ticket_channel.mention}!"
embed = discord.Embed(
title="INFO",
description=addUserText,
color=255
)
await ticket_channel.send(
embed=embed,
)
UserText = f"Hallo {interaction.user.mention}, wir kümmern uns in Kürze um Sie.\nIn der Zwischenzeit können Sie Ihr Problem näher beschreiben." if chosen_language == "de" else f"Hello, {interaction.user.mention}. We will be with you shortly.\nIn the meantime, you can provide a more detailed description of your issue."
await ticket_channel.send(UserText)
return ticket_channel
@@ -37,7 +169,7 @@ class CreateDETicketForm(discord.ui.Modal, title="Ticket erstellen"):
super().__init__()
self.chosen_language = chosen_language
form_location = discord.ui.TextInput(
form_reason = discord.ui.TextInput(
label="Warum erstellen Sie dieses Ticket?",
style=discord.TextStyle.paragraph,
placeholder="",
@@ -48,7 +180,7 @@ class CreateDETicketForm(discord.ui.Modal, title="Ticket erstellen"):
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
ticket_channel = await CreateTicketChannel(interaction, self.chosen_language)
ticket_channel = await CreateTicketChannel(interaction, self.chosen_language, self.form_reason.value)
if ticket_channel:
await interaction.followup.send(
@@ -67,7 +199,7 @@ class CreateENTicketForm(discord.ui.Modal, title="Create a Ticket"):
super().__init__()
self.chosen_language = chosen_language
form_location = discord.ui.TextInput(
form_reason = discord.ui.TextInput(
label="Why are you creating this ticket?",
style=discord.TextStyle.paragraph,
placeholder="",
@@ -78,7 +210,7 @@ class CreateENTicketForm(discord.ui.Modal, title="Create a Ticket"):
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
ticket_channel = await CreateTicketChannel(interaction, self.chosen_language)
ticket_channel = await CreateTicketChannel(interaction, self.chosen_language, self.form_reason.value)
if ticket_channel:
await interaction.followup.send(
@@ -127,6 +259,7 @@ class MainInteractiveMenu(discord.ui.View):
class Ticket(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.bot.add_view(TicketControlsView())
async def cog_load(self):
self.startup.start()