#discord.py help

36 messages · Page 1 of 1 (latest)

heavy pebble
#

Clearly ticket is None. We don't have any code here to tell us why that is.

hallow elm
# heavy pebble Clearly `ticket` is `None`. We don't have any code here to tell us why that is.
class ticket_launcher(discord.ui.View):
    def __init__(self) -> None:
        super().__init__(timeout = None) #  So the ticket window stays up even after bot restart
    
    @discord.ui.button(label = "Createa a Ticket", style = discord.ButtonStyle.blurple, custom_id = "ticket_button")    #   Ticket button
    async def ticket(self, interaction: discord.Interaction, button: discord.ui.Button):
        ticket = utils.get(interaction.guild.text_channels, name = f"ticket-for-{interaction.user.name}-{interaction.user.discriminator}")
        if ticket is None:
            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),
                interaction.guild.me: discord.PermissionOverwrite(view_channel = True, send_messages = True, read_message_history = True)
            }
            channel = await interaction.guild.create_text_channel(name = f"ticket-for-{interaction.user.name}-{interaction.user.discriminator}", overwrites = overwrites, reason = 
            f"Ticket for {interaction.user}")
            await channel.send(f"{interaction.user.mention} created a ticket!")
            await interaction.response.send_message(f"I've opened a ticket for you at {channel.mention}!", ephemeral = True)
        else:
            await interaction.response.send_message(f"You already have a ticket open at {ticket.mention}!", ephemeral = True)






@tree.command(name = 'ticket', description='Launches the ticketing system', guild = discord.Object(id = GUILD))
async def ticketing(interaction: discord.Interaction):
    embed = discord.Embed(title = "If you need support, click the button below and create a ticket!", color = discord.Colour.green())
    await interaction.channel.send(embed = embed, view = ticket_launcher())
    await interaction.response.send_message("Ticketing system launched", ephemeral = True)```

Here is the rest
heavy pebble
#

Ok, so it's pretty clear that this

f"ticket-for-{interaction.user.name}-{interaction.user.discriminator}"

Isn't matching an existing text channel. That or the bot can't see the channel.

#

I'd add a print to see if the channel name you're looking for is correct. I'd also check the interaction.guild.text_channels contains the channel you're looking for.

hallow elm
heavy pebble
#

Nope. utils.get will return None if no objects in the iterable match the filters.

#

So, I know that either the channel name your searching for doesn't exist or the bot cannot see the channel.

hallow elm
#

the bot is admin so it seeing it shouldnt be a problem

heavy pebble
#

Then I can only assume the name is not being generated correctly for some reason.

#

Keep in mind you may not have the correct intents set, which could cause the channel list to be empty.

hallow elm
#
class aclient(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.synced = False
        self.added = False

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync(guild = discord.Object(id = GUILD))
            self.synced = True
        if not self.added:
            self.add_view(ticket_launcher())
            self.added = True
        print(f"We have logged in as {self.user}.") #Lets you know the bot is up :D

client = aclient()
tree = app_commands.CommandTree(client)```

I believe I do have them
heavy pebble
#

That's why I say you should print out the channels and the channel name that you generated. This way you can see what isn't what you expected.

hallow elm
hallow elm
#

its printing the same channel name

heavy pebble
#

Ok, is the bot seeing that channel in the interaction.guild.text_channels?

hallow elm
heavy pebble
#

Print it out

#

print(interaction.guild.text_channels)

hallow elm
#

ohhh gotcha

hallow elm
heavy pebble
#

I see the problem. ticket-for-joeyy-1873. You've got a for in the channel name but when you look it up with utils.get you don't have the for in the channel name.

#

Hmm or not

hallow elm
#

still same results unfortunately

heavy pebble
#

I dunno. Everything looks correct... Maybe try hard coding a channel name into utils.get to see if it finds it.

hallow elm
heavy pebble
#

That's probably the better way to do it. It lets you change how you name the channels without having to change how you track the channels.

hallow elm
# heavy pebble That's probably the better way to do it. It lets you change how you name the cha...
tickets = {}

class ticket_launcher(discord.ui.View):
    def __init__(self) -> None:
        super().__init__(timeout = None)
    
    @discord.ui.button(label = "Create a Ticket", style = discord.ButtonStyle.blurple, custom_id = "ticket_button")
    async def ticket(self, interaction: discord.Interaction, button: discord.ui.Button):
        user_id = interaction.user.id
        if user_id in tickets:
            await interaction.response.send_message(f"You already have a ticket open at {tickets[user_id].mention}!", ephemeral = True)
            return
        
        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),
            interaction.guild.me: discord.PermissionOverwrite(view_channel = True, send_messages = True, read_message_history = True)
        }
        channel = await interaction.guild.create_text_channel(name = f"ticket-{interaction.user.name}-{interaction.user.discriminator}", overwrites = overwrites, reason = 
        f"Ticket for {interaction.user}")
        await channel.send(f"{interaction.user.mention} created a ticket!")
        await interaction.response.send_message(f"I've opened a ticket for you at {channel.mention}!", ephemeral = True)
        tickets[user_id] = channel

Changed the code to include that in a data structure and now its working just fine

#

probably wouldve been more optimized the way I had it planned but oh well

#

thank you for the help 😄

heavy pebble
#

Keep in mind this will lose all the user IDs if you restart the bot. Might wanna store them in a JSON file or something wolfwink

hallow elm
#

oh yes

#

also forgot to have it remove the users when the ticket is deleted

hallow elm
heavy pebble
#

No, JSON is just really popular because it'll handle all the type conversions for you.