#discord.py help
36 messages · Page 1 of 1 (latest)
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
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.
but why would it matter what any of that is? it would put a value for "ticket" making the second if statement not pass
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.
the bot is admin so it seeing it shouldnt be a problem
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.
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
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.
kinda new to discord.py sorry
its printing the same channel name
Ok, is the bot seeing that channel in the interaction.guild.text_channels?
not sure how id find out exactly
ohhh gotcha
oh goodness
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
ive tried changing that
still same results unfortunately
I dunno. Everything looks correct... Maybe try hard coding a channel name into utils.get to see if it finds it.
what about keeping track of the ticket channels that have been created in a data structure and then removing it when the ticket is closed/deleted
do you think thats inefficient?
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.
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 😄
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 
btw im used to just storing ids and things of that sort in just another seperate python file - is there a real reason for storing them in a different type of file?
No, JSON is just really popular because it'll handle all the type conversions for you.