Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
12 kB
5
Indexable
Never
import nextcord
from nextcord.ext import commands
from nextcord.ui import Button, View
import asyncio
from datetime import datetime
import pytz

# Global dictionary to track open tickets (consider persisting this in a database for real usage)
open_tickets = {}

class CloseButton(Button):
    def __init__(self, bot, ticket_channel, message_id, original_user, ticket_type):
        super().__init__(label="Close 🔒", style=nextcord.ButtonStyle.primary)
        self.bot = bot
        self.ticket_channel = ticket_channel
        self.message_id = message_id
        self.original_user = original_user
        self.ticket_type = ticket_type
        self.error_emoji = "<:error:1271946673890791506>"
        self.success_emoji = "<:success:1271946714605162516>"

    async def callback(self, interaction: nextcord.Interaction):
        await interaction.response.defer()

        if interaction.user == self.original_user:
            if self.ticket_channel.name.startswith("closed-"):
                await interaction.followup.send(f"{self.error_emoji} Ticket already closed.", ephemeral=True)
                return

            await self.ticket_channel.set_permissions(interaction.user, view_channel=False)
            await self.ticket_channel.edit(name=f"closed-{self.ticket_channel.name.split('-')[1]}")

            embed = nextcord.Embed(title="", description=f"{self.success_emoji} This ticket has been closed. For further assistance, please open a new ticket.", color=nextcord.Color.green())
            await interaction.followup.send(embed=embed)

            embed = nextcord.Embed(title="", description="``` Ticket Control Panel ```", color=None)
            view = View()
            view.add_item(ModeratorButton(self.bot, "📝 Create Log", "create_log", self.ticket_type, self.original_user, self.ticket_channel, style=nextcord.ButtonStyle.success))
            view.add_item(ModeratorButton(self.bot, "⛔ Delete", "delete", self.ticket_type, self.original_user, self.ticket_channel, style=nextcord.ButtonStyle.danger))

            await self.ticket_channel.send(embed=embed, view=view)

            open_tickets.pop(interaction.user.id, None)
        else:
            await interaction.followup.send(f"{self.error_emoji} You don't have permission to close this ticket.", ephemeral=True)

class ModeratorButton(Button):
    def __init__(self, bot, label, action, ticket_type, original_user, ticket_channel, style=nextcord.ButtonStyle.primary):
        super().__init__(label=label, style=style)
        self.bot = bot
        self.action = action
        self.ticket_type = ticket_type
        self.original_user = original_user
        self.ticket_channel = ticket_channel
        self.error_emoji = "<:error:1271946673890791506>"
        self.success_emoji = "<:success:1271946714605162516>"

    async def callback(self, interaction: nextcord.Interaction):
        print(f"ModeratorButton callback - Action: {self.action}, Ticket Type: {self.ticket_type}, Original User: {self.original_user}")

        if interaction.user.guild_permissions.administrator:
            if self.action == "create_log":

                embed = nextcord.Embed(title="", description="**Input your log notes below. These will be added to the Ticket Record.**", color=None)
                await interaction.response.send_message(embed=embed)

                def check(m):
                    return m.author == interaction.user and m.channel == interaction.channel

                try:
                    message = await self.bot.wait_for('message', check=check, timeout=300.0)
                except asyncio.TimeoutError:
                    await interaction.followup.send("You took too long to provide notes. Please try again.", ephemeral=True)
                    return

                records_channel_id = 1275991900637106226
                records_channel = self.bot.get_channel(records_channel_id)
                if not records_channel:
                    await interaction.followup.send("Could not find the records channel.", ephemeral=True)
                    return

                embed_color = nextcord.Color.blue() if self.ticket_type == "Suggestion" else nextcord.Color.red() if self.ticket_type == "Report" else nextcord.Color.green()
                
                embed = nextcord.Embed(
                    title="🎟️ Ticket Record",
                    color=embed_color,
                    timestamp=datetime.now()  # This sets the default timestamp in the embed
                )
                embed.add_field(name="Server Name:", value=self.original_user.mention, inline=True)
                embed.add_field(name="User Name:", value=f"`{self.original_user.display_name}`", inline=False)
                embed.add_field(name="Ticket Type:", value=f"`{self.ticket_type}`", inline=False)
                embed.add_field(name="Logged By:", value=interaction.user.mention, inline=False)
                embed.add_field(name="Notes:", value=message.content or "No additional notes provided.", inline=False)
                embed.set_thumbnail(url=self.original_user.avatar.url)
                embed.set_footer(text=f"User ID: {self.original_user.id}")

                print(f"Sending embed to records channel with Ticket Type: {self.ticket_type}")  # Debug print

                await records_channel.send(embed=embed)

                embed = nextcord.Embed(title="", description=f"{self.success_emoji} **The Ticket Record has been created successfully.**", color=nextcord.Color.green())
                await interaction.followup.send(embed=embed)
                
            elif self.action == "delete":
                # Handle the delete action
                await self.ticket_channel.delete(reason="Ticket deleted by a moderator.")
                await interaction.response.send_message(f"{self.success_emoji} Ticket has been deleted.", ephemeral=True)
        else:
            await interaction.response.send_message(f"{self.error_emoji} You don't have permission to perform this action.", ephemeral=True)

class TicketButton(Button):
    def __init__(self, bot, label, description, ticket_type, style):
        super().__init__(label=label, style=style)
        self.bot = bot
        self.ticket_type = ticket_type

    async def callback(self, interaction: nextcord.Interaction):
        await interaction.response.defer()

        user = interaction.user
        category_id = 1276279397904547981  # Replace with your category ID
        category = interaction.guild.get_channel(category_id)
        
        if not category or not isinstance(category, nextcord.CategoryChannel):
            await interaction.followup.send(f"{self.error_emoji} Unable to find the specified category.", ephemeral=True)
            return
        
        channel_name = f"{self.ticket_type.lower().replace(' ', '-')}-{user.name.lower()}"

        if user.id in open_tickets and open_tickets[user.id] == self.ticket_type.lower().replace(' ', '-'):
            await interaction.followup.send("You already have a similar ticket open. Please close that ticket to open a new one.", ephemeral=True)
            return

        open_tickets[user.id] = self.ticket_type.lower().replace(' ', '-')

        embed_title = "<:mmm:774710180708352021> Thanks for opening a ticket!"
        embed_color = nextcord.Color.green()  # Default color

        if self.ticket_type == "Question":
            embed_title = "<:mmm:774710180708352021> We're here to answer any questions you may have!"
            embed_color = nextcord.Color.green()
        elif self.ticket_type == "Report":
            embed_title = "<:mmm:774710180708352021> We take all reports very seriously!"
            embed_color = nextcord.Color.red()
        elif self.ticket_type == "Suggestion":
            embed_title = "<:mmm:774710180708352021> We'd love to hear your suggestions!"
            embed_color = nextcord.Color.blue()

        admin_role = interaction.guild.get_role(496548368974348293)  # Replace with the actual role ID for "Mac Miller Memoir"
        
        # Initially set the admin role to None, temporarily preventing it from having permissions
        overwrites = {
            interaction.guild.default_role: nextcord.PermissionOverwrite(view_channel=False),
            user: nextcord.PermissionOverwrite(view_channel=True, send_messages=True),
            interaction.guild.me: nextcord.PermissionOverwrite(view_channel=True, send_messages=True),
        }

        ticket_channel = await interaction.guild.create_text_channel(
            name=channel_name,
            category=category,
            overwrites=overwrites
        )

        await ticket_channel.send(f"{user.mention}\n-")

        embed = nextcord.Embed(title=embed_title, description="", color=embed_color)
        embed.add_field(name="", value="", inline=False)
        embed.add_field(name="Please type your message below.", value="Once your message is sent, one of our <@&496549370544652309>'s will respond to you shortly.", inline=False)
        embed.add_field(name="", value="", inline=False)
        embed.add_field(name="", value="`To close this ticket, press the Close 🔒 button.`", inline=False)

        close_button = CloseButton(self.bot, ticket_channel, message_id=None, original_user=user, ticket_type=self.ticket_type)
        view = View()
        view.add_item(close_button)

        sent_message = await ticket_channel.send(embed=embed, view=view)
        close_button.message_id = sent_message.id

        self.bot.loop.create_task(self.auto_delete_channel(ticket_channel, user))

        def check(m):
            return m.author == user and m.channel == ticket_channel

        message = await self.bot.wait_for('message', check=check)

        # Once the user sends their first message, update the permissions to allow the admin role to view the channel
        print(f"Ticket Type for user {user.name}: {self.ticket_type}")

        await ticket_channel.set_permissions(admin_role, view_channel=True, send_messages=True, read_message_history=True)

        if self.ticket_type == "Suggestion":
            await ticket_channel.send("<@&496549370544652309>")
        elif self.ticket_type in ["Report", "Question"]:
            moderator_role = interaction.guild.get_role(496549370544652309)
            await ticket_channel.set_permissions(moderator_role, view_channel=True, send_messages=True, read_message_history=True)
            await ticket_channel.send("<@&496549370544652309>")



    async def auto_delete_channel(self, channel, user):
        await asyncio.sleep(3600)  # Wait for 60 minutes
        async for message in channel.history(limit=1):
            if message.author == user:
                return

        await channel.delete(reason="No activity for 60 minutes.")

class TicketView(View):
    def __init__(self, bot):
        super().__init__()
        self.add_item(TicketButton(bot, "❔ Questions", "Other support.", "Question", nextcord.ButtonStyle.success))
        self.add_item(TicketButton(bot, "🚨 Report", "Report an issue.", "Report", nextcord.ButtonStyle.danger))
        self.add_item(TicketButton(bot, "💡 Server Suggestion", "Suggest something.", "Suggestion", nextcord.ButtonStyle.primary))


class SupportTicketCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @nextcord.slash_command(name="support", description="🔒 Create the Support Ticket embed.", default_member_permissions=nextcord.Permissions(administrator=True))
    async def support(self, interaction: nextcord.Interaction):
        embed = nextcord.Embed(title="MMM Support Menu", description="Select the type of support ticket you'd like to open.", color=nextcord.Color.gold())
        embed.add_field(name="", value="\n\nYour ticket will open in another channel.", inline=False)
        embed.set_thumbnail(url=interaction.guild.icon.url)
        view = TicketView(self.bot)
        await interaction.response.send_message(embed=embed, view=view)

def setup(bot):
    bot.add_cog(SupportTicketCog(bot))
Leave a Comment