ticket2

 avatar
hisoka
python
a year ago
5.2 kB
6
Indexable
Never
import asyncio
import contextlib
import json
import logging
import os
import platform
import random
import string
import time
import traceback
from typing import Union

import discord
from discord import app_commands, ui
from discord.ext import commands

import settings

logger = settings.logging.getLogger("bot")

# ? default_role = @everyone
# ? interaction.user = user who opened ticket
# ? interaction.guild.me = the bot
# ? ticketmanager_role = role for staff to see tickets


class TicketLauncherView(ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(
        label="Open a Ticket",
        style=discord.ButtonStyle.blurple,
        custom_id="open_ticket_button",
    )
    async def ticket(self, interaction: discord.Interaction, button: discord.ui.Button):
        ticket = utils.get(
            interaction.guild.text_channels, name=f"ticket-{interaction.user.name}"
        )
        # ? TICKET EMBEDS
        # USER ALREADY HAS A TICKET OPENED EMBED MESSAGE
        ticketerrorembed = discord.Embed(
            title="Error!",
            description=f"{interaction.user.name}\n**New Ticket Request Error:**\nUser has an active ticket open.\n\n**Ticket Location:**\n{ticket.mention}\n\nMultiple tickets are not allowed!\nPlease use the current active ticket.",
            color=0xFFFFFF,
        )
        ticketerrorembed.set_footer(
            text="Ticket System",
            icon_url="https://i.imgur.com/4DuUUVU.png",
        )
        # USER NEW TICKET OPENED EMBED MESSAGE -> FOR NEW TICKET CHANNEL
        newticketembed = discord.Embed(
            title="Ticket",
            description=f"{channel.mention} opened for {interaction.user.name}\n\n__Note__\n**Please do not ping staff, they will be with you when they are available.**",
            color=0xFFFFFF,
        )
        newticketembed.set_footer(
            text="Ticket System",
            icon_url="https://i.imgur.com/4DuUUVU.png",
        )
        # TICKET HAS BEEN CREATED EMBED -> FOR OPEN A TICKET CHANNEL
        ticketcreatedembed = discord.Embed(
            title="Ticket Opened",
            description=f"{interaction.user.name}, your ticket has been created.\n\n**Ticket Location:**\n{channel.mention}",
            color=0xFFFFFF,
        )
        ticketcreatedembed.set_footer(
            text="Ticket System",
            icon_url="https://i.imgur.com/4DuUUVU.png",
        )
        # IF USER HAS TICKET OPENED, SEND EPHEMERAL ERROR EMBED
        if ticket is not None:
            await interaction.response.send_message(
                embed=ticketerrorembed,
                ephemeral=True,
            )

        # IF USER DOES NOT HAVE TICKET OPENED, CREATE NEW CHANNEL AND SET PERMISSIONS
        else:
            overwrites = {
                interaction.guild.default_role: discord.PermissionOverwrite(
                    view_channel=False
                ),
                interaction.user: discord.PermissionOverwrite(
                    view_channel=True,
                    send_messages=True,
                    attach_files=True,
                    embed_links=True,
                    external_emojis=True,
                    external_stickers=True,
                ),
                interaction.guild.me: discord.PermissionOverwrite(
                    view_channel=True, send_messages=True, read_message_history=True
                ),
                ###############? INSERT TICKET MANAGER ROLE HERE! ?##############
                # interaction.guild.ticketmanager_role: discord.PermissionOverwrite(view_channel = True, send_messages = True, attach_files = True, embed_links = True, external_emojis = True, external_stickers = True, manage_messages = True, read_message_history = True)
            }
            channel = await interaction.guild.create_text_channel(
                name=f"ticket-{interaction.user.name}",
                overwrites=overwrites,
                reason=f"Ticket opened for {interaction.user}",
            )
            # SEND EMBED INTO NEW TICKET CHANNEL
            await channel.send(text=f"{interaction.user.mention}", embed=newticketembed)
            # NOTIFY USER OF NEW TICKET OPENED
            await interaction.response.send_message(
                embed=ticketcreatedembed, ephemeral=True
            )


class TicketLauncher(commands.Cog):
    @commands.command()
    async def ticket_launcher(self, interaction: discord.Interaction):
        embed = discord.Embed(
            title="Ticket System",
            description="To open a ticket, click the button below.",
            color=0xFFFFFF,
        )
        embed.set_footer(
            text="Ticket System",
            icon_url="https://i.imgur.com/4DuUUVU.png",
        )
        await interaction.channel.send(embed=embed, view=TicketLauncherView())
        await interaction.response.send_message(
            "Ticket system enabled!", ephemeral=True
        )


async def setup(self):
    await self.add_cog(TicketLauncher())