#Category and Channel fetch issues
1 messages ยท Page 1 of 1 (latest)
So here's the code when a user selects the option to create a support ticket
The issue: he bot is having trouble findning the right category and channel even if it exists, or having trouble finding a channel it created.
I solved a few steps of it by asking help here and checking docs for 3 days... Anyways here's the code.
case 'newTicket': {
const reason = int.values[0].split('_')[1];
const cat = int.guild.channels.cache.find(categ=> categ.name === 'TICKETS' && categ.type === "GUILD_CATEGORY");
if (!cat) {
const cat = await int.guild.channels.create("TICKETS", {
type: 'GUILD_CATEGORY'
});
}
const ticket = await int.guild.channels.cache.find(x => x.name === `๐ใป${int.member.user.username.toLowerCase()}`);
if (!ticket) {
await int.guild.channels.create(`๐ใป${int.member.user.username.toLowerCase()}`, {
type: 'GUILD_TEXT',
parent: cat.id,
topic: `Ticket created by ๐ใป${int.member.user.username}${reason ? ` (${reason})` : ''} ${new Date(Date.now()).toLocaleString()}`,
permissionOverwrites: [
{
id: int.guild.id,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES']
},
{
id: int.member.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES']
},
{
id: client.user.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES']
}
]
});
const ticketEmbed = new MessageEmbed();
ticketEmbed.setColor('GREEN');
ticketEmbed.setAuthor(`Welcome to ${int.guild.name} support! ๐ใป${int.member.user.username}${reason ? ` (${reason})` : ''} โ
`);
ticketEmbed.setDescription(`Opened by ๐ใป${int.member.user.username} for ${reason ? ` ${reason}` : ''}. `);
const closeButton = new MessageButton();
closeButton.setStyle('DANGER');
closeButton.setLabel('Close this ticket');
closeButton.setCustomId(`closeTicket_${int.member.id}`);
const row = new MessageActionRow().addComponents(closeButton);
const ticket = int.guild.channels.cache.find(x => x.name === `๐ใป${int.member.user.username.toLowerCase()}`);
await ticket.send({ embeds: [ticketEmbed], components: [row] });
return int.update({ content: `Your ticket is open <@${int.member.id}> <#${channel.id}> โ
`, components: [], ephemeral: true });
} else {
return int.update({ content: `โ Oops, you already have an open ticket: <#${channel.id}> `, components: [], ephemeral: true });
}
}
public
so it's creating a category and a channel and still can not find it
or is it already on the server
Are u getting undefined for cat?
Here's the error:
Situation: There is no category setup yet and the bot has to create one, and then open the channel in that ticket.
Process: The bot successfully creates the category, but fails to create the ticket under it, resulting in the following error
parent: cat.id,
^
TypeError: Cannot read properties of undefined (reading 'id')
So cat is the created channel?
@worthy gale
yes
Scope issue
category
const cat is declared inside the if statement
Not valid anywhere else
So it uses the undefined value from .find
[
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
these are the scopes i have enabled
Not intents, scope
@sick bison
Declare cat like let cat = //find code and in the if statement, just use cat = await ... without const at the start
@sick bison did you do it
can you guys gimme some time till i get back on my pc?
im so sorry
ill be back in a few hours
Ok I won't be online though, but you can still ping me if it works
I would like to know
Surew
ik about oauth2 scopes, but what exactly are you talking bout?
works to an extent! it created the category and channel, but ticket seems undefined
await ticket.send({ embeds: [ticketEmbed], components: [row] });
^
TypeError: Cannot read properties of undefined (reading 'send')
let cat = int.guild.channels.cache.find(categ=> categ.name === 'TICKETS' && categ.type === "GUILD_CATEGORY");
if (!cat) {
cat = await int.guild.channels.create("TICKETS", {
type: 'GUILD_CATEGORY'
});
}
@bright vortex @worthy gale sorry this got archived. I just opened it again.
Console log the ticket channel?
@worthy gale "const ticket =...." The 4th variable
send screen shot of it