Files
bot.py
config.py
requirements.txt
Code
3 393
bot.py
import discord
from discord.ext import commands
from discord.ui import View, Select, Button
import sqlite3
import asyncio
from datetime import datetime
import os
from dotenv import load_dotenv
from config import *
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True
bot = commands.Bot(command_prefix='!', intents=intents)
conn = sqlite3.connect('ticket_data.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS tickets (
channel_id INTEGER PRIMARY KEY,
user_id INTEGER,
category TEXT,
created_at TEXT,
closed BOOLEAN,
message_id INTEGER
)''')
conn.commit()
active_timers = {}
class TicketSelect(Select):
def __init__(self):
options = [
discord.SelectOption(label="Buy", description="Purchase hosting plans", emoji="๐ฐ", value="buy"),
discord.SelectOption(label="Claim", description="Claim giveaways & rewards", emoji="๐", value="claim"),
discord.SelectOption(label="Support", description="Technical support & billing", emoji="โ๏ธ", value="support")
]
super().__init__(placeholder="Select any Option from the Dropdown", options=options, custom_id="ticket_select")
async def callback(self, interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
if self.values[0] == "buy":
cat_id = BUY_CATEGORY
cat_name = "Buy"
elif self.values[0] == "claim":
cat_id = CLAIM_CATEGORY
cat_name = "Claim"
else:
cat_id = SUPPORT_CATEGORY
cat_name = "Support"
category = interaction.guild.get_channel(cat_id)
overwrites = {
interaction.guild.default_role: discord.PermissionOverwrite(read_messages=False),
interaction.user: discord.PermissionOverwrite(read_messages=True, send_messages=True, attach_files=True, embed_links=True),
interaction.guild.get_role(SUPPORT_TEAM_ROLE): discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
if cat_name != "Buy":
overwrites[interaction.guild.get_role(STAFF_ROLE)] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
channel_name = f"{cat_name.lower()}-{interaction.user.name}"
channel = await interaction.guild.create_text_channel(channel_name, category=category, overwrites=overwrites)
now = datetime.now()
time_opened = now.strftime("%I:%M %p, %B %d, %Y")
embed = discord.Embed(
title=f"Welcome {interaction.user.mention} to Veltrion Hosting",
description=f"**Category:** {cat_name}\n**Time Opened:** {time_opened}\n\nOur Support team will assist you soon!",
color=discord.Color.green(),
timestamp=now
)
if cat_name == "Buy":
await channel.send(f"<@&{SUPPORT_TEAM_ROLE}>")
else:
await channel.send(f"<@&{SUPPORT_TEAM_ROLE}> <@&{STAFF_ROLE}>")
await channel.send(embed=embed)
view = TicketControl(channel.id, interaction.user.id, cat_name)
control_msg = await channel.send("### Ticket Controls", view=view)
c.execute("INSERT OR REPLACE INTO tickets VALUES (?, ?, ?, ?, ?, ?)",
(channel.id, interaction.user.id, cat_name, now.isoformat(), False, control_msg.id))
conn.commit()
log_channel = interaction.guild.get_channel(LOG_CHANNEL)
if log_channel:
log_embed = discord.Embed(
title="๐ซ Ticket Created",
description=f"**User:** {interaction.user.mention}\n**Category:** {cat_name}\n**Channel:** {channel.mention}\n**Time:** {time_opened}",
color=discord.Color.green(),
timestamp=now
)
await log_channel.send(embed=log_embed)
await interaction.followup.send(f"{EMOJIS['ticket']} Ticket created! Check {channel.mention}", ephemeral=True)
if cat_name in ["Claim", "Support"]:
task = asyncio.create_task(check_support_response(channel))
active_timers[channel.id] = task
class TicketControl(View):
def __init__(self, channel_id, user_id, category):
super().__init__(timeout=None)
self.channel_id = channel_id
self.user_id = user_id
self.category = category
@discord.ui.button(label="Close", style=discord.ButtonStyle.danger, custom_id="close_btn", emoji="๐")
async def close(self, interaction: discord.Interaction, button: Button):
if not (interaction.user.guild_permissions.administrator or interaction.user.id == self.user_id):
await interaction.response.send_message(f"{EMOJIS['cross']} You don't have permission!", ephemeral=True)
return
channel = interaction.guild.get_channel(self.channel_id)
if self.category == "Buy":
await channel.set_permissions(interaction.guild.get_member(self.user_id), overwrite=None)
else:
role = interaction.guild.get_role(STAFF_ROLE)
member = interaction.guild.get_member(self.user_id)
if member and role in member.roles:
await member.remove_roles(role)
if self.channel_id in active_timers:
active_timers[self.channel_id].cancel()
del active_timers[self.channel_id]
c.execute("UPDATE tickets SET closed = 1 WHERE channel_id = ?", (self.channel_id,))
conn.commit()
await channel.edit(name=f"closed-{interaction.user.name}")
embed = discord.Embed(
title="Ticket Closed",
description=f"The Ticket is closed by {interaction.user.mention}",
color=discord.Color.red(),
timestamp=datetime.now()
)
view = ReopenDelete(self.channel_id, self.user_id, self.category)
await channel.send(embed=embed, view=view)
for child in self.children:
child.disabled = True
await interaction.response.edit_message(view=self)
class ReopenDelete(View):
def __init__(self, channel_id, user_id, category):
super().__init__(timeout=None)
self.channel_id = channel_id
self.user_id = user_id
self.category = category
@discord.ui.button(label="Reopen", style=discord.ButtonStyle.success, custom_id="reopen_btn", emoji="๐")
async def reopen(self, interaction: discord.Interaction, button: Button):
if not (interaction.user.guild_permissions.administrator or interaction.user.id == self.user_id):
await interaction.response.send_message(f"{EMOJIS['cross']} You don't have permission!", ephemeral=True)
return
channel = interaction.guild.get_channel(self.channel_id)
member = interaction.guild.get_member(self.user_id)
c.execute("UPDATE tickets SET closed = 0 WHERE channel_id = ?", (self.channel_id,))
conn.commit()
await channel.edit(name=f"{self.category.lower()}-{member.name}")
embed = discord.Embed(
title="Ticket Reopened",
description=f"Ticket reopened by {interaction.user.mention}",
color=discord.Color.green(),
timestamp=datetime.now()
)
await channel.send(embed=embed)
view = TicketControl(self.channel_id, self.user_id, self.category)
msg = await channel.send("### Ticket Controls", view=view)
c.execute("UPDATE tickets SET message_id = ? WHERE channel_id = ?", (msg.id, self.channel_id))
conn.commit()
await interaction.response.send_message("Ticket reopened!", ephemeral=True)
@discord.ui.button(label="Delete", style=discord.ButtonStyle.danger, custom_id="delete_btn", emoji="๐๏ธ")
async def delete(self, interaction: discord.Interaction, button: Button):
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(f"{EMOJIS['cross']} Only administrators can delete tickets!", ephemeral=True)
return
channel = interaction.guild.get_channel(self.channel_id)
transcript_path = f"transcript_{channel.id}.html"