I know this question has already been asked but I can't find a good answer. Whenever I click on the button I made, everything seems to work fine but I get multiple channels created. It's supposed to make only one. I then get this error.
CODE:
else if (interaction.isButton()) {
if (interaction.customId === "ticket") {
var categoryId = "998616644123381913"
var userName = interaction.user.username
var userDiscrimator = interaction.user.discriminator
var ticketExist = false;
interaction.guild.channels.cache.forEach((channel) => {
if(channel.name == userName.toLocaleLowerCase() "#" userDiscrimator){
let ticketEmbed = new MessageEmbed()
.setTitle("You already have an Ticket Open!")
.setColor("RED")
.setDescription("Please talk in that ticket!")
interaction.reply({embeds: ticketEmbed, ephemeral: true })
ticketExist = true;
return;
}
if(ticketExist) return;
interaction.guild.channels.create(userName.toLocaleLowerCase() "#" userDiscrimator).then((createdChen) =>{
createdChen.setParent(categoryId).then((settedParent) =>{
settedParent.permissionOverwrites.edit(interaction.guild.roles.cache.find(x => x.name === "@everyone"), {
SEND_MESSAGES: false,
VIEW_CHANNEL: false
});
settedParent.permissionOverwrites.edit(interaction.user.id, {
CREATE_INSTANT_INVITE: false,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
CONNECT: true,
ADD_REACTIONS: true
});
settedParent.permissionOverwrites.edit(interaction.guild.roles.cache.find(x => x.name === "member"), {
CREATE_INSTANT_INVITE: false,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
CONNECT: true,
ADD_REACTIONS: true
});
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() 1).padStart(2, '0');
let yyyy = today.getFullYear();
today = `${dd}/${mm}/${yyyy}`;
let embedParent = new MessageEmbed()
.setAuthor(interaction.user.tag, interaction.user.displayAvatarURL({ dynamic: true }))
.setTitle("New Ticket!")
.addFields(
{ name: "Created by:", value: interaction.user.username, inline: true },
{ name: "Made on ", value: today, inline: true }
)
let embed21 = new MessageEmbed()
.setTitle("✅Ticket created")
interaction.reply({ embeds: [embed21], ephemeral: true })
settedParent.send({ embeds: [embedParent] })
}).catch(err => {
let errEmbed = new MessageEmbed()
.setTitle("Something went wrong")
.setColor("RED")
interaction.reply({embeds: errEmbed, ephemeral: true })
console.log(err)
})
}).catch(err => {
let errEmbed2 = new MessageEmbed()
.setTitle("Something went wrong")
.setColor("RED")
interaction.reply({ embeds: {errEmbed2}, ephemeral: true })
console.log(err)
})
})
}
}
ERROR:
DiscordAPIError: Interaction has already been acknowledged.
at RequestHandler.execute (C:\Users\stixy\Desktop\v-13\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\stixy\Desktop\v-13\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
at async ButtonInteraction.reply (C:\Users\stixy\Desktop\v-13\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:103:5) {
method: 'post',
path: '/interactions/998629381398347907/aW50ZXJhY3Rpb246OTk4NjI5MzgxMzk4MzQ3OTA3OlE4ZHdmc3VtaHZ2cWRUMXdMemFPTlFyOW1hT0pzY003S1hrcVI4a2p2ZlBucnRCNFhCSW11NWV0b29NM1lpcTFzdFVpekQyTTFuYlJteHRHZjJzZzRVVjBBNkI5T003Sk5YWG1kQ1FUb0xJZnE4bUhLUENXTVc2T0dpYnJLckpR/callback',
code: 40060,
httpStatus: 400,
requestData: {
json: {
type: 4,
data: {
content: undefined,
tts: false,
nonce: undefined,
embeds: [
{
title: '✅Ticket created',
type: 'rich',
description: null,
url: null,
timestamp: 0,
color: null,
fields: [],
thumbnail: null,
image: null,
author: null,
footer: null
}
],
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: 64,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
}
},
files: []
}
}
CodePudding user response:
With interaction.guild.channels.cache.forEach
you create as many new channels as you have at that moment. And you also try to send more than one reply this way causing the error.
If you want to check if a channel with a certain name exists, you can use channels.cache.find()
instead and provide a callback where you check if the item/channel name is the same.
You also have other errors in your code, like you're trying to send embeds as a single embed instead of an array. You can also use interaction.guild.id
instead of finding the @everyone
role. Another thing that you can use is new Date().toLocaleDateString('en-GB')
instead of that custom massaging of today's date.
Made some changes to your code to make it a bit easier to read:
// ..
if (interaction.isButton()) {
if (interaction.customId !== 'ticket') return;
const categoryId = '998616644123381913';
const userName = interaction.user.username;
const userDiscrimator = interaction.user.discriminator;
const ticketChannelName = `${userName.toLocaleLowerCase()}#${userDiscrimator}`;
const ticketExist = interaction.guild.channels.cache.find(
(ch) => ch.name === ticketChannelName
);
if (ticketExist)
return interaction.reply({
embeds: [
new MessageEmbed()
.setTitle('You already have an Ticket Open!')
.setColor('RED')
.setDescription('Please talk in that ticket!'),
],
ephemeral: true,
});
try {
let createdChannel = await interaction.guild.channels.create(ticketChannelName);
await createdChannel.setParent(categoryId);
createdChannel.permissionOverwrites.edit(interaction.guild.id, {
SEND_MESSAGES: false,
VIEW_CHANNEL: false,
});
createdChannel.permissionOverwrites.edit(interaction.user.id, {
CREATE_INSTANT_INVITE: false,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
CONNECT: true,
ADD_REACTIONS: true,
});
// do you really need this?
createdChannel.permissionOverwrites.edit(
interaction.guild.roles.cache.find((x) => x.name === 'member'),
{
CREATE_INSTANT_INVITE: false,
READ_MESSAGE_HISTORY: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
CONNECT: true,
ADD_REACTIONS: true,
},
);
let ticketEmbed = new MessageEmbed()
.setAuthor(
interaction.user.tag,
interaction.user.displayAvatarURL({ dynamic: true }),
)
.setTitle('New Ticket!')
.addFields(
{
name: 'Created by:',
value: interaction.user.username,
inline: true,
},
{
name: 'Made on ',
value: new Date().toLocaleDateString('en-GB'),
inline: true,
},
);
interaction.reply({
embeds: [new MessageEmbed().setTitle('✅Ticket created')],
ephemeral: true,
});
createdChannel.send({ embeds: [ticketEmbed] });
} catch (err) {
interaction.reply({
embeds: [
new MessageEmbed().setTitle('Something went wrong').setColor('RED'),
],
ephemeral: true,
});
console.log(err);
}
}
// ...