.demonslayer
.demonslayer
DIAdiscord.js - Imagine a bot
Created by .demonslayer on 9/1/2023 in #djs-questions
Duplicate Message Send
Hello, I've been having an issue with a channel.send() call occasionally resulting in a message being sent multiple times. Node.js: v18.12.1 Discord.js: v14.9.0 OS: Ubuntu 20.04 LTS Code:
async function sendMessage(
channelId: Snowflake,
guildId: Snowflake,
client: Client,
data: data,
timestamp: number,
roles: [Snowflake?],
) {
const guildroles = roles.length ? [...new Set(roles)] : roles;
if (data) {
const embed = new EmbedBuilder()
.setColor('#0e48b8')
.setTitle(data.name)
.setDescription(data.description)
.setTimestamp(timestamp);

(client.guilds.cache.get(guildId)?.channels.cache.get(channelId) as TextBasedChannel)
.send({
content: guildroles?.length ? guildroles.map((g) => `<@&${g}>`).join(' ') : '',
embeds: [embed],
})
.then((m) => console.log(process.pid, '[Discord.js]', 'Sent data to channel:', m.channelId))
.catch((e: any) => {
console.log(e);
});
}
}
async function sendMessage(
channelId: Snowflake,
guildId: Snowflake,
client: Client,
data: data,
timestamp: number,
roles: [Snowflake?],
) {
const guildroles = roles.length ? [...new Set(roles)] : roles;
if (data) {
const embed = new EmbedBuilder()
.setColor('#0e48b8')
.setTitle(data.name)
.setDescription(data.description)
.setTimestamp(timestamp);

(client.guilds.cache.get(guildId)?.channels.cache.get(channelId) as TextBasedChannel)
.send({
content: guildroles?.length ? guildroles.map((g) => `<@&${g}>`).join(' ') : '',
embeds: [embed],
})
.then((m) => console.log(process.pid, '[Discord.js]', 'Sent data to channel:', m.channelId))
.catch((e: any) => {
console.log(e);
});
}
}
Upon this function executing, I receive the following logs:
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Discord.js] Posted to /channels/1125283603702632531/messages
2544499 [Discord.js] Sent data to channel: 1125283603702632531
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Undici] Making request to: /api/v10/channels/1125283603702632531/messages
2544499 [Discord.js] Posted to /channels/1125283603702632531/messages
2544499 [Discord.js] Sent data to channel: 1125283603702632531
First 3 logs are requests made by undici, 4th log is from a client.rest response event 5th log is from the .then() method called on the promise returned by channel.send() It appears that despite my function only being called once, the message is being sent multiple times. If anyone has an idea as to what could cause this, please let me know.
6 replies