Bot posts randomly 1 - 3 welcome messages for the same member that already joined.

Lets start from my debugging results. First i wanted to make sure i have only one instance of the bot, so i added console.trace right before send message line, and it logs only once with nothing unusual in the trace stack. Then i made prefix "test" command that should reply multiple times if bot is in more than one instance, but also in this case, every time i get only one reply. There is no specific pattern how bot sends welcome messages, its always between 1 - 3 messages for the same person. If two people join within 2 minutes bot can post three messages for first person, and one for second person. Bot doesn't crash or anything (I use Sentry for detailed logs). I am hosting this bot on DigitalOcean, same as my other 3 bots, but only one have that issue. I am also sure that this is not related with my event handler, because all other events works fine. I ran out of ideas, I even reseted bot token, but problem still exists. What is even more weird that issue does not exists on dev version of the bot. The only difference is the token, (i use Windows WSL configured exactly the same as DigitalOcean server to host bot locally on my PC) I put the console logs in every function that is related with guildMemberAdd event, and didn't see anything wrong (on every step i also log the new joined member, and that member is logged everywhere only once) Is there anything else i can do? Is it possible that it is Discord API issue, server configuration or PM2? (in pm2 configuration file i have also explicitly 1 application instance) Node.js: 18.14.0 Runtime: [email protected] discord.js 14.11.0 OS: Ubuntu 20.04 LTS
59 Replies
d.js toolkit
d.js toolkit2y 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!
MD
MDOP2y ago
function that is executed on guildMemberAdd event:
const guildMemberAddEvent = async (client, member) => {
const guildConfig = await GuildConfig.findOne({ guildId: member.guild.id }).select('welcomeMessage');
if (!guildConfig.welcomeMessage.enabled) return;
const image = guildConfig.welcomeMessage.welcomeImage;
const [, imageType] = image.match(/image\/(\w+);/);
const buffer = Buffer.from(image.split(';base64,').pop(), 'base64');
const welcomeImage = new AttachmentBuilder(buffer, { name: `welcomeImage.${imageType}` });
const message = {
content: guildConfig.welcomeMessage.title.replace('{{username}}', member.user.username),
files: [welcomeImage],
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel(guildConfig.welcomeMessage.rulesButton || Language.get(member.guild.preferredLocale, 'bot.welcomeMessage.rulesButton'))
.setStyle(ButtonStyle.Link)
.setURL(`https://discord.com/channels/${member.guild.id}/${member.guild.rulesChannelId || guildConfig.welcomeMessage.rulesChannelId}`)
),
],
};
member.guild.systemChannel
.send(message)
.catch(console.log);
};
const guildMemberAddEvent = async (client, member) => {
const guildConfig = await GuildConfig.findOne({ guildId: member.guild.id }).select('welcomeMessage');
if (!guildConfig.welcomeMessage.enabled) return;
const image = guildConfig.welcomeMessage.welcomeImage;
const [, imageType] = image.match(/image\/(\w+);/);
const buffer = Buffer.from(image.split(';base64,').pop(), 'base64');
const welcomeImage = new AttachmentBuilder(buffer, { name: `welcomeImage.${imageType}` });
const message = {
content: guildConfig.welcomeMessage.title.replace('{{username}}', member.user.username),
files: [welcomeImage],
components: [
new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel(guildConfig.welcomeMessage.rulesButton || Language.get(member.guild.preferredLocale, 'bot.welcomeMessage.rulesButton'))
.setStyle(ButtonStyle.Link)
.setURL(`https://discord.com/channels/${member.guild.id}/${member.guild.rulesChannelId || guildConfig.welcomeMessage.rulesChannelId}`)
),
],
};
member.guild.systemChannel
.send(message)
.catch(console.log);
};
grass
grass2y ago
exit the process and see if your bot still responds
MD
MDOP2y ago
no, it doesn't bot runs in only one instance
grass
grass2y ago
show where you call client.on(...
MD
MDOP2y ago
(async () => {
try {
const files = await getAllFilesAsync({
directory: path.join(process.cwd(), 'eventHandlers/'),
extensions: ['.js'],
});

if (!files.length) {
throw new Error('Could not load event files');
}

files.forEach(({ path }) => {
const eventModule = require(path);
const eventType = eventModule.once ? 'once' : 'on';
const eventEmitter = eventModule.restType ? client.rest : client;
eventEmitter[eventType](eventModule.event, eventModule.execute.bind(null, client));
});
} catch (err) {
console.log(err);
}
})();
(async () => {
try {
const files = await getAllFilesAsync({
directory: path.join(process.cwd(), 'eventHandlers/'),
extensions: ['.js'],
});

if (!files.length) {
throw new Error('Could not load event files');
}

files.forEach(({ path }) => {
const eventModule = require(path);
const eventType = eventModule.once ? 'once' : 'on';
const eventEmitter = eventModule.restType ? client.rest : client;
eventEmitter[eventType](eventModule.event, eventModule.execute.bind(null, client));
});
} catch (err) {
console.log(err);
}
})();
i doubt the problem is here because all other events works fine
grass
grass2y ago
can you place a console.log inside your execute function to see if it logs multiple times
MD
MDOP2y ago
i did it it logs only once Like i wrote earlier even console log right before
member.guild.systemChannel
.send(message)
.catch(console.log);
member.guild.systemChannel
.send(message)
.catch(console.log);
is logged only once
grass
grass2y ago
then the only reason i can think of would be that you have multiple instances of your bot running
MD
MDOP2y ago
that is not possible i made prefix command to check that, and bot also reply only once I even reinstalled PM2, and nothing helped Anyway multiple instances of the bot does not explain why bot sometimes sends 3 sometimes 2 and sometimes 1 message. With multiple instances that should be consistent I host my bot on DigitalOcean. I created droplet and configured it by myself with all Ubuntu "good practices". But it may be a good point, all my other bots that works fine, are on different droplets.
olzabi
olzabi2y ago
Hi! Do you have any updates on this issue?
MD
MDOP17mo ago
Not really. Just more questions than answers. I didn't touch anything in the code or server since i created this thread. For 4 days bot worked perfectly fine, didn't post even one duplicated message (at that time joined around 30 people to my server), and after that 4 days, it posts again 1 - 3 messages, but this time majority are 2 messages, 1 and 3 are maybe 20% @qjuh sorry for the ping, but i have really no idea what is going on with that bot. Around 10 days ago I created new droplet on Digital Ocean, installed everything, and problem still existed. 2 days later it fixed itself (I didn't change anything on the server or in the code) And yesterday it started again, 2 - 3 messages for almost everybody that joins my server. All of the logs in my code still logs only once. I reset my bot token for new server, to make sure it is not another instance, and nothing... I created file on my server, where i log the events, and 'guildMemberAdd' is logged only once for each person. Function that logs that event is right before welcome message send. At this point i am 99% sure it is Discord API issue. Is there any way i can test it further? It's not public, but i will share you the code when i come back home sent in DM
lopnb
lopnb17mo ago
I have the same, like he send 1-3 welcome messages idk why it's simple code
LeStyloBleu
LeStyloBleu17mo ago
Same here Ok will see
MD
MDOP17mo ago
Yes, i updated discord.js to v14.13, and hoped that will fix the issue, but nothing changed. I always try to keep my bots up to date and update all libraries at least once a month. Hard to tell when exactly my bot started posting duplicated messages, because i deleted some of them, At the beginning it was very very rare, i remember i noticed it, but thought, maybe that member rejoined my server. First duplicated messages i can see around March 2023, so around discord.js v14.8, but it started to be really annoying 2 - 3 months ago when bot started posting almost every message 2 - 3 times. Around 2 months ago I even reverted one of the library updates commits, but it didn't fix it either. I will add <Client>.rest.on(RESTEvents.Response, console.dir) and come back with results. That is first log that showed something. Bot posted 3 messages for that person
{
method: 'POST',
path: '/channels/613140671175589893/messages',
route: '/channels/:id/messages',
options: {
body: FormData { [Symbol(state)]: [Array] },
headers: {
'User-Agent': 'DiscordBot (https://discord.js.org, 2.0.1) discord.js/14.13.0 Node.js/18.16.1',
Authorization: 'Bot token i deleted...'
},
method: 'POST',
dispatcher: undefined
},
data: {
body: {
content: 'Hello <@1144676812035719229> Welcome to the server!',
tts: false,
nonce: undefined,
embeds: undefined,
components: [Array],
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: [Array],
sticker_ids: undefined,
thread_name: undefined
},
files: [ [Object] ],
auth: true,
signal: undefined
},
retries: 2
}
{
method: 'POST',
path: '/channels/613140671175589893/messages',
route: '/channels/:id/messages',
options: {
body: FormData { [Symbol(state)]: [Array] },
headers: {
'User-Agent': 'DiscordBot (https://discord.js.org, 2.0.1) discord.js/14.13.0 Node.js/18.16.1',
Authorization: 'Bot token i deleted...'
},
method: 'POST',
dispatcher: undefined
},
data: {
body: {
content: 'Hello <@1144676812035719229> Welcome to the server!',
tts: false,
nonce: undefined,
embeds: undefined,
components: [Array],
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: [Array],
sticker_ids: undefined,
thread_name: undefined
},
files: [ [Object] ],
auth: true,
signal: undefined
},
retries: 2
}
All other welcome messages has retries value 0, and ofc were not posted multiple times. idk if we can connect it with any API outages, but bot posted this 3 messages at <t:1693247160:f>
LeStyloBleu
LeStyloBleu17mo ago
I got the same result as you
LeStyloBleu
LeStyloBleu17mo ago
No description
MD
MDOP17mo ago
[email protected] /var/www/bots/bot
├── @discordjs/[email protected]
└─┬ @discordjs/[email protected]
└── @discordjs/[email protected] deduped
[email protected] /var/www/bots/bot
├── @discordjs/[email protected]
└─┬ @discordjs/[email protected]
└── @discordjs/[email protected] deduped
node.js 18.16.1 i just used this function to check latency:
const pingServer = async (client, message) => {
const { ping } = client.ws;
const sent = await message.reply({ content: 'Pinging...' });
sent.edit(`Roundtrip latency: **${sent.createdTimestamp - message.createdTimestamp}ms**\nWebSocket latency: **${ping}ms**`);
};
const pingServer = async (client, message) => {
const { ping } = client.ws;
const sent = await message.reply({ content: 'Pinging...' });
sent.edit(`Roundtrip latency: **${sent.createdTimestamp - message.createdTimestamp}ms**\nWebSocket latency: **${ping}ms**`);
};
and Roundtrip latency is 347ms and ping 101 ms I have never changed any timeouts. I remember ECONNRESET error in the logs, but since i flushed the logs around a week ago, i don't see even one ECONNRESET error.
LeStyloBleu
LeStyloBleu17mo ago
Personally, i have good network speed on my vps and i didn't change anything on the timeout
MD
MDOP17mo ago
Me too, i host all of my bots on Digital Ocean, and only one bot has this issue. Problematic bot is on another droplet, but i created recently new droplet for this bot and problem still exists. From my experience Digital Ocean has really good network speed and ping, compare to other hosting services, better has only Google Cloud
LeStyloBleu
LeStyloBleu17mo ago
I have ovh vps so it's not network speed :/
MD
MDOP17mo ago
Ping to discord from the droplet
PING discord.com (162.159.136.232) 56(84) bytes of data.
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=1 ttl=58 time=1.71 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=2 ttl=58 time=1.28 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=3 ttl=58 time=1.22 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=4 ttl=58 time=1.10 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=5 ttl=58 time=1.11 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=6 ttl=58 time=1.13 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=7 ttl=58 time=1.09 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=8 ttl=58 time=1.08 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=9 ttl=58 time=1.13 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=10 ttl=58 time=1.28 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=11 ttl=58 time=1.10 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=12 ttl=58 time=1.12 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=13 ttl=58 time=1.11 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=14 ttl=58 time=1.09 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=15 ttl=58 time=1.09 ms
^C
--- discord.com ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 14019ms
rtt min/avg/max/mdev = 1.077/1.175/1.711/0.156 ms
PING discord.com (162.159.136.232) 56(84) bytes of data.
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=1 ttl=58 time=1.71 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=2 ttl=58 time=1.28 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=3 ttl=58 time=1.22 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=4 ttl=58 time=1.10 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=5 ttl=58 time=1.11 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=6 ttl=58 time=1.13 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=7 ttl=58 time=1.09 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=8 ttl=58 time=1.08 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=9 ttl=58 time=1.13 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=10 ttl=58 time=1.28 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=11 ttl=58 time=1.10 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=12 ttl=58 time=1.12 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=13 ttl=58 time=1.11 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=14 ttl=58 time=1.09 ms
64 bytes from 162.159.136.232 (162.159.136.232): icmp_seq=15 ttl=58 time=1.09 ms
^C
--- discord.com ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 14019ms
rtt min/avg/max/mdev = 1.077/1.175/1.711/0.156 ms
zay!
zay!17mo ago
node v.16.20.2
No description
zay!
zay!17mo ago
I have onboarding and rules screening verification enabled. IIRC (I had to cherry pick a month back to get those screenshots) I noticed that running through onboarding and completing rules screening normally sent different guildMemberUpdate events, but, they sometimes they sent identical events which caused my messages to double fire. mine if they're useful,
--- discord.com ping statistics ---
310 packets transmitted, 310 received, 0% packet loss, time 309496ms
rtt min/avg/max/mdev = 16.666/16.760/16.938/0.037 ms
--- discord.com ping statistics ---
310 packets transmitted, 310 received, 0% packet loss, time 309496ms
rtt min/avg/max/mdev = 16.666/16.760/16.938/0.037 ms
MD
MDOP16mo ago
I increased rest timeout to 30 seconds, and it didn't change anything. My droplet network seems to be very stable. Also i don't see any ECONNRESET error in the logs. I set up cronjob, to ping my other server on Google Cloud every 5 minutes and check time in which response came back, the highest delay was 378ms, and average around 150ms. Bot still retries to post that messages
LeStyloBleu
LeStyloBleu16mo ago
Yeah the problem is not from us nah I just tested on a discord channel 1 minute ago, and 3 messages were sent each with an interval of around 10 seconds
zay!
zay!16mo ago
Mine were always immediate succession
Zayd
Zayd16mo ago
you probably have multiple instances of the bot. it doesn’t matter if your console.log only ran once, since the older versions of the bot wouldn’t have that log anyways also probably should do development locally or have a staging server maybe
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
LeStyloBleu
LeStyloBleu16mo ago
Did u look at the messages of the guy who created this post ? And all of us ? But this is strange, i have ovh vps with really good performances for a bot The messages are sent randomly, sometimes it’s almost instant and sometimes not I swear, It’s not me, i have the same vps for multiple months and in the past, i did not experienced this issue
zay!
zay!16mo ago
yep yep, there's only one instance
MD
MDOP16mo ago
it's also not consistent for me, in old messages when i compare timestamps can be even 18 seconds difference, in messages 2 days ago is 2 seconds difference... I also added bot to my other two servers and it works there fine, not that many people joins, but bot didn't post even one duplicated message so far. I dont know if it is related in any way, but i took a suggestion from this message https://discord.com/channels/222078108977594368/1137513755840872449/1146228888180101180 I have enabled onboarding on the server where bot posts multiple messages, and disabled on servers where bot works fine (so far) 🤔
LeStyloBleu
LeStyloBleu16mo ago
No answer? I think it's discord
RedLime
RedLime16mo ago
same issue, also I just added mysql insert sql run in interaction event. but insert has run only once (checked in DB), but sometimes bot has sent 1-3 messages randomly. mostly it's 1 but yeah
MD
MDOP16mo ago
i also think it is Discord issue. On my server is the same https://discord.com/channels/222078108977594368/1137513755840872449/1146872636257861683 Default discord welcome messages are duplicated for so many people, and that also correlate with my bot duplicated messages. I checked timestamps of default Discord welcome messages, and 3 messages for the same person has exactly the same timestamp. It is not possible that one person 3 times within one second joined and left my server
Zayd
Zayd16mo ago
what are the message IDs? are they all the same or do they vary
MD
MDOP16mo ago
message id is a snowflake, that means is unique, and there is now way it can be the same for 2 messages After i set rest timeout to 30 seconds, i still get duplicated messages, but after that 30 seconds. Correct message immediately, 2nd after 30 seconds, and 3rd after 1 minute. Kinda fixed the problem when i set rest retries value to 0. It is not posting duplicated messages anymore, but if something will go wrong, it will not post message at all Thonkang
LeStyloBleu
LeStyloBleu16mo ago
Discord fix the problem no ?
Lioness100
Lioness10016mo ago
I'm experiencing this issue as well. Definitely not multiple listeners/instances in my case.
LeStyloBleu
LeStyloBleu16mo ago
Yeah sometimes there is some multiples messages
willowissnake (ssss)
Have you tried resetting your bot token? That worked for me
LeStyloBleu
LeStyloBleu16mo ago
I tried already 😅
skeddles
skeddles16mo ago
just want to say this also happens with discords built in welcome messages for me, so it might not be anything on your end.
Lawrence
Lawrence15mo ago
node v16.20.0 & node v16.20.2 & node v18.18.0 has the exact same problem.
No description
Lawrence
Lawrence15mo ago
yeah! Same for me. I have already try it. Same problem for 14.13 and bring me more issue. Which is I used an interaction collector to receive string select menu interactions and nothing has been collected. This problem has been solved by just downgrade to 14.11. Yeah. Sure. I will do that later when I'm not that busy.
MD
MDOP15mo ago
update to 14.13 didn't change anything for me
Solo
Solo14mo ago
any update?
LovelyJacob
LovelyJacob14mo ago
Well I'm glad to see I'm not the only one with this issue... I'm going to update my node version to latest and see if that changes anything, will share the results here when I have them. Edit: Attempting node v20.9.0 Another Edit: https://github.com/discord/discord-api-docs/issues/6500
GitHub
Sporadic embed duplication bug · Issue #6500 · discord/discord-api-...
Description This is a very weird issue, that we've only started seeing over the last one or two weeks. TL;DR: very occasionally, when creating a channel message that contains an embed, Discord ...
LovelyJacob
LovelyJacob14mo ago
Seems updating node version worked for me, might've been a fix by discord also.(small reminder: reset your environment variables when updating with pm2)
ShizukaMikazuki
ShizukaMikazuki13mo ago
Hello I have a similar problem when itirating over attachements and sending them one by one and adding a button to each of them. Sometimes it works perfectly, sometimes it sends the same image 2-5 times and the weird thing is that it doesn't show up on my console (normally each image sent will have it's attachement id logged). I wanted to ask if you could find any solutions to this problem. @Qjuh Is there anything I should provide to help solve this bug or whatever it is?
zay!
zay!11mo ago
I think so too, thrice just happened again
No description
zay!
zay!11mo ago
Following up on our sporadic joins, this user joined a week ago but just got a new welcome message; i dont think this is a d.js issue anymore so I'll stop posting here henceforth
No description
zay!
zay!11mo ago
We do have membership screening, I wasn't aware that passing causing a second welcome message to be fired, are you sure about that? I just ran through the server on an alt and it didn't fire until I finished onboarding. ¯\_(ツ)_/¯ I confused Screening with Onboarding, we don't have Screening
can
can11mo ago
same here
can
can11mo ago
No description
can
can11mo ago
No description
can
can11mo ago
no discord must be facing a problem there's no onboarding in my server no membership screening (I was busy setting up server roles when my friend joined)
Want results from more Discord servers?
Add your server