Files
bot.py
requirements.txt
Code
2 299
bot.py
import logging
import os
import re
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("goat_ticket_bot")
BOT_NAME = "『𝐆𝐎𝐀𝐓 𝐄𝐥𝐢𝐭𝐞 𝐁𝐨𝐭』"
TOKEN = os.getenv("DISCORD_TOKEN")
SUPPORT_ROLE_ID = int(os.getenv("SUPPORT_ROLE_ID", "0"))
TICKET_CATEGORY_ID = int(os.getenv("TICKET_CATEGORY_ID", "0"))
class TicketPanelView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(
label="افتح تذكرة",
emoji="🎫",
style=discord.ButtonStyle.primary,
custom_id="goat_tickets:open",
)
async def open_ticket(
self, interaction: discord.Interaction, button: discord.ui.Button
):
await interaction.response.send_modal(TicketModal())
class TicketModal(discord.ui.Modal, title="فتح تذكرة دعم"):
subject = discord.ui.TextInput(
label="عنوان التذكرة",
placeholder="اكتب باختصار سبب فتح التذكرة",
max_length=100,
)
details = discord.ui.TextInput(
label="التفاصيل",
placeholder="اشرح كيف يمكن لفريق الدعم مساعدتك",
style=discord.TextStyle.paragraph,
max_length=1000,
)
async def on_submit(self, interaction: discord.Interaction):
guild = interaction.guild
if guild is None:
await interaction.response.send_message(
"يمكن فتح التذاكر داخل السيرفر فقط.", ephemeral=True
)
return
owner_marker = f"ticket-owner:{interaction.user.id};"
existing = next(
(
channel
for channel in guild.text_channels
if channel.topic
and channel.topic.startswith(owner_marker)
and not channel.name.startswith("closed-")
),
None,
)
if existing:
await interaction.response.send_message(
f"لديك تذكرة مفتوحة بالفعل: {existing.mention}", ephemeral=True
)
return
category = None
if TICKET_CATEGORY_ID:
configured_category = guild.get_channel(TICKET_CATEGORY_ID)
if not isinstance(configured_category, discord.CategoryChannel):
await interaction.response.send_message(
"إعداد تصنيف التذاكر غير صحيح. يرجى إبلاغ الإدارة.",
ephemeral=True,
)
return
category = configured_category
support_role = guild.get_role(SUPPORT_ROLE_ID) if SUPPORT_ROLE_ID else None
if SUPPORT_ROLE_ID and support_role is None:
await interaction.response.send_message(
"رتبة الدعم المحددة غير موجودة في هذا السيرفر.", ephemeral=True
)
return
overwrites = {
guild.default_role: discord.PermissionOverwrite(view_channel=False),
interaction.user: discord.PermissionOverwrite(
view_channel=True,
send_messages=True,
read_message_history=True,
attach_files=True,
),
}
if guild.me:
overwrites[guild.me] = discord.PermissionOverwrite(
view_channel=True,
send_messages=True,
read_message_history=True,
manage_channels=True,
)
if support_role:
overwrites[support_role] = discord.PermissionOverwrite(
view_channel=True,
send_messages=True,
read_message_history=True,
)
username = re.sub(r"[^a-z0-9-]", "", interaction.user.name.lower())[:20]
channel_name = f"ticket-{username or 'user'}-{interaction.user.id}"
try:
channel = await guild.create_text_channel(
channel_name,
category=category,
overwrites=overwrites,
topic=f"{owner_marker} {self.subject.value}",
reason=f"Ticket opened by {interaction.user} ({interaction.user.id})",
)
embed = discord.Embed(
title=f"🎫 {self.subject.value}",
description=self.details.value,
color=discord.Color.blurple(),
)
embed.add_field(name="صاحب التذكرة", value=interaction.user.mention)
embed.set_footer(text=BOT_NAME)
await channel.send(
content=interaction.user.mention,
embed=embed,
view=TicketCloseView(),
allowed_mentions=discord.AllowedMentions(users=True),
)
await interaction.response.send_message(
f"تم فتح تذكرتك: {channel.mention}", ephemeral=True
)
except discord.HTTPException:
logger.exception("Failed to create a ticket for user %s", interaction.user.id)
if interaction.response.is_done():
await interaction.followup.send(
"تعذر فتح التذكرة. تأكد من صلاحيات البوت وحاول مرة أخرى.",
ephemeral=True,
)
else:
await interaction.response.send_message(
"تعذر فتح التذكرة. تأكد من صلاحيات البوت وحاول مرة أخرى.",
ephemeral=True,
)
class TicketCloseView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(
label="إغلاق التذكرة",
emoji="🔒",
style=discord.ButtonStyle.danger,
custom_id="goat_tickets:close",
)
async def close_ticket(
self, interaction: discord.Interaction, button: discord.ui.Button
):
channel = interaction.channel
guild = interaction.guild
if not isinstance(channel, discord.TextChannel) or guild is None:
await interaction.response.send_message(
"هذا الزر يعمل داخل قناة التذكرة فقط.", ephemeral=True
)
return
owner_match = re.match(r"ticket-owner:(\d+);", channel.topic or "")
if owner_match is None:
await interaction.response.send_message(
"لم أتمكن من التحقق من هذه التذكرة.", ephemeral=True
)
return
owner_id = int(owner_match.group(1))
is_support = (
SUPPORT_ROLE_ID != 0
and isinstance(interaction.user, discord.Member)
and any(role.id == SUPPORT_ROLE_ID for role in interaction.user.roles)
)
is_admin = (
isinstance(interaction.user, discord.Member)
and interaction.user.guild_permissions.administrator
)
if interaction.user.id != owner_id and not is_support and not is_admin:
await interaction.response.send_message(
"يمكن لصاحب التذكرة أو فريق الدعم إغلاقها فقط.", ephemeral=True
)