Skip to content

Instantly share code, notes, and snippets.

@Glowstik-YT
Created September 5, 2022 02:14
Show Gist options
  • Save Glowstik-YT/53cbbad341745ee22413c1c44d829591 to your computer and use it in GitHub Desktop.
Save Glowstik-YT/53cbbad341745ee22413c1c44d829591 to your computer and use it in GitHub Desktop.
from nextcord.ext import commands
import nextcord
import asyncio
from sqlalchemy import over
from config import TOKEN
import os
import aiosqlite
class AddUser(nextcord.ui.Modal):
def __init__(self, channel):
super().__init__(
"Add User to Ticket",
timeout=300,
)
self.channel = channel
self.user = nextcord.ui.TextInput(
label="User ID",
min_length=2,
max_length=30,
required=True,
placeholder="User ID (Must be INT)"
)
self.add_item(self.user)
async def callback(self, interaction: nextcord.Interaction) -> None:
user = interaction.guild.get_member(int(self.user.value))
if user is None:
return await interaction.send(f"Invaid User ID, make sure the user is in this guild!")
overwrite = nextcord.PermissionOverwrite()
overwrite.read_messages = True
await self.channel.set_permissions(user, overwrite=overwrite)
await interaction.send(f"{user.mention} has been added to this ticket!")
class RemoveUser(nextcord.ui.Modal):
def __init__(self, channel):
super().__init__(
"Remove User to Ticket",
timeout=300,
)
self.channel = channel
self.user = nextcord.ui.TextInput(
label="User ID",
min_length=2,
max_length=30,
required=True,
placeholder="User ID (Must be INT)"
)
self.add_item(self.user)
async def callback(self, interaction: nextcord.Interaction) -> None:
user = interaction.guild.get_member(int(self.user.value))
if user is None:
return await interaction.send(f"Invaid User ID, make sure the user is in this guild!")
overwrite = nextcord.PermissionOverwrite()
overwrite.read_messages = False
await self.channel.set_permissions(user, overwrite=overwrite)
await interaction.send(f"{user.mention} has been removed from this ticket!")
class CreateTicket(nextcord.ui.View):
def __init__(self, bot):
super().__init__(timeout=None)
self.bot = bot
@nextcord.ui.button(
label="Create Ticket", style=nextcord.ButtonStyle.blurple, custom_id="create_ticket:blurple"
)
async def create_ticket(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
msg = await interaction.response.send_message("A ticket is being created...", ephemeral=True)
async with self.bot.db.cursor() as cursor:
await cursor.execute("SELECT role FROM roles WHERE guild = ?", (interaction.guild.id,))
role = await cursor.fetchone()
if role:
overwrites = {
interaction.guild.default_role: nextcord.PermissionOverwrite(read_messages=False),
interaction.guild.me: nextcord.PermissionOverwrite(read_messages=True),
interaction.guild.get_role(role[0]): nextcord.PermissionOverwrite(read_messages=True)
}
else:
overwrites = {
interaction.guild.default_role: nextcord.PermissionOverwrite(read_messages=False),
interaction.guild.me: nextcord.PermissionOverwrite(read_messages=True),
}
channel = await interaction.guild.create_text_channel(f"{interaction.user.name}-ticket", overwrites=overwrites)
await msg.edit(f"Channel created successfully! {channel.mention}")
embed = nextcord.Embed(title=f"Ticket Created", description=f"{interaction.user.mention} created a ticket! Click one of the buttons below to alter the settings.")
await channel.send(embed=embed, view=TicketSettings())
class TicketSettings(nextcord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@nextcord.ui.button(
label="Add User", style=nextcord.ButtonStyle.green, custom_id="ticket_settings:green"
)
async def add_user(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await interaction.response.send_modal(AddUser(interaction.channel))
@nextcord.ui.button(
label="Remove User", style=nextcord.ButtonStyle.gray, custom_id="ticket_settings:gray"
)
async def remove_user(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
await interaction.response.send_modal(RemoveUser(interaction.channel))
@nextcord.ui.button(
label="Close Ticket", style=nextcord.ButtonStyle.red, custom_id="ticket_settings:red"
)
async def close_ticket(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
messages = await interaction.channel.history(limit=None, oldest_first=True).flatten()
contents = [message.content for message in messages]
final = ""
for msg in contents:
msg = msg + "\n"
final = final + msg
with open('transcript.txt', 'w') as f:
f.write(final)
await interaction.response.send_message("Ticket is being closed.", ephemeral=True)
await interaction.channel.delete()
await interaction.user.send(f"Ticket closed successfully!", file=nextcord.File(r'transcript.txt'))
os.remove("transcript.txt")
class Bot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.persistent_views_added = False
async def on_ready(self):
if not self.persistent_views_added:
self.add_view(CreateTicket(self))
self.add_view(TicketSettings())
self.persistent_views_added = True
print("Persistent views added")
self.db = await aiosqlite.connect("tickets.db")
async with self.db.cursor() as cursor:
await cursor.execute(f"CREATE TABLE IF NOT EXISTS roles (role INTEGER, guild INTEGER)")
print("Database ready")
print(f"{self.user} is ready!")
print(f"Bot is up and ready | Logged in as {self.user}")
bot = Bot(command_prefix='!', intents = nextcord.Intents.all())
@bot.command()
@commands.has_permissions(manage_guild=True)
async def setup_tickets(ctx: commands.Context):
embed = nextcord.Embed(title="Create a ticket!", description="Click the `Create Ticket` button below to create a ticket. The server's staff will be notified and shortly aid you with your problem.")
await ctx.send(embed=embed, view=CreateTicket(bot))
@bot.command()
@commands.has_permissions(manage_guild=True)
async def setup_role(ctx: commands.Context, role: nextcord.Role):
async with bot.db.cursor() as cursor:
await cursor.execute("SELECT role FROM roles WHERE guild = ?", (ctx.guild.id,))
role2 = await cursor.fetchone()
if role2:
await cursor.execute("UPDATE roles SET role = ? WHERE guild = ?", (role.id, ctx.guild.id,))
await ctx.send(f"Tickets Auto-Assigned role updated!")
else:
await cursor.execute("INSERT INTO roles VALUES (?, ?)", (role.id, ctx.guild.id,))
await ctx.send(f"Tickets Auto-Assigned role added!")
await bot.db.commit()
bot.run(TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment