Failure to create channel

For some reason, I tried a lot of ways to create a channel when they click the button but it doesn't work every time.
const { Events, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, PermissionsBitField } = require('discord.js');

module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
if (interaction.commandName === 'ordersend') {
const embed = new EmbedBuilder()
.setColor('#e6072c')
.setTitle('Order Menu')
.setDescription('Click the button to open a ticket for your order!');

const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('open_ticket')
.setLabel('Open Ticket')
.setStyle(ButtonStyle.Primary)
);

await interaction.reply({
embeds: [embed],
components: [row],
});
}
}

if (interaction.isButton()) {
console.log(`Button clicked: ${interaction.customId}`); // Debugging log

if (interaction.customId === 'open_ticket') {
try {
const botMember = await interaction.guild.members.fetch(interaction.client.user.id);

if (!botMember.permissions.has(PermissionsBitField.Flags.ManageChannels)) {
return await interaction.reply({
content: 'I do not have permission to create channels!',
ephemeral: true,
});
}

const channel = await interaction.guild.channels.create({
name: `ticket-${interaction.user.username}`,
type: ChannelType.GuildText,
topic: `Ticket opened by ${interaction.user.username}`,
permissionOverwrites: [
{
id: interaction.guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: interaction.user.id,
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
});

const ticketEmbed = new EmbedBuilder()
.setColor('#00FF00')
.setTitle('Ticket Opened')
.setDescription(`Hello ${interaction.user.username}, your support ticket is now open!`);

await channel.send({ embeds: [ticketEmbed] });

await interaction.reply({
content: `Your ticket has been opened: ${channel}`,
ephemeral: true,
});
} catch (error) {
console.error('Error while creating ticket channel:', error);
await interaction.reply({
content: 'There was an error creating your ticket. Please try again later.',
ephemeral: true,
});
}
}
}
},
};
const { Events, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, PermissionsBitField } = require('discord.js');

module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (interaction.isCommand()) {
if (interaction.commandName === 'ordersend') {
const embed = new EmbedBuilder()
.setColor('#e6072c')
.setTitle('Order Menu')
.setDescription('Click the button to open a ticket for your order!');

const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId('open_ticket')
.setLabel('Open Ticket')
.setStyle(ButtonStyle.Primary)
);

await interaction.reply({
embeds: [embed],
components: [row],
});
}
}

if (interaction.isButton()) {
console.log(`Button clicked: ${interaction.customId}`); // Debugging log

if (interaction.customId === 'open_ticket') {
try {
const botMember = await interaction.guild.members.fetch(interaction.client.user.id);

if (!botMember.permissions.has(PermissionsBitField.Flags.ManageChannels)) {
return await interaction.reply({
content: 'I do not have permission to create channels!',
ephemeral: true,
});
}

const channel = await interaction.guild.channels.create({
name: `ticket-${interaction.user.username}`,
type: ChannelType.GuildText,
topic: `Ticket opened by ${interaction.user.username}`,
permissionOverwrites: [
{
id: interaction.guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: interaction.user.id,
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
});

const ticketEmbed = new EmbedBuilder()
.setColor('#00FF00')
.setTitle('Ticket Opened')
.setDescription(`Hello ${interaction.user.username}, your support ticket is now open!`);

await channel.send({ embeds: [ticketEmbed] });

await interaction.reply({
content: `Your ticket has been opened: ${channel}`,
ephemeral: true,
});
} catch (error) {
console.error('Error while creating ticket channel:', error);
await interaction.reply({
content: 'There was an error creating your ticket. Please try again later.',
ephemeral: true,
});
}
}
}
},
};
16 Replies
d.js toolkit
d.js toolkit2d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Louis
Louis2d ago
what error did you get? (sorry I'm reading the code on mobile)
blvckz
blvckzOP2d ago
I didn’t get one It’s for interactioncreate.js too.
Louis
Louis2d ago
it just does nothing huh?
DoctorNobody
DoctorNobody2d ago
are you recieving this log: console.log(`Button clicked: ${interaction.customId}`); // Debugging log
blvckz
blvckzOP2d ago
No Am I doing it wrong?
DoctorNobody
DoctorNobody2d ago
probably need to specify the correct intents to listen for the interaction. maybe i said that poorly make sure you have GatewayIntentBits.Guilds @blvckz
Louis
Louis2d ago
I don't think it's the issue, since bot doesn't need to be in guild to receive Interaction Create can you use the command?
DoctorNobody
DoctorNobody2d ago
try removing guilds intent bits and then collecting a button interaction 👌 i attempted and couldnt. I could be wrong of course
blvckz
blvckzOP2d ago
Yes K so what do I have to do
Louis
Louisthis hour
did you save your file?
blvckz
blvckzOPthis hour
Yes it’s auto save
Louis
Louisthis hour
it's really weird since you can still use the command
blvckz
blvckzOPthis hour
Yeah
Louis
Louisthis hour
the code does work for me...
blvckz
blvckzOP19h ago
hm maybe i used the wrong thing idk

Did you find this page helpful?