Axe
Axe
DIAdiscord.js - Imagine a bot
Created by Axe on 9/18/2024 in #djs-questions
I have 3 replicas that all respond to the same thing
Yo! I have a bot that sends a message when a thread is created, but Im running 3 replicas, so the problem is that they all send the messge at the same time. I tried to resolve this by just checking if the last message was by the bot, but since they all check at once its false for all of them, and all of them end up sending. Any idea how I can resolve this without creating a database / api or creating extra overhead beyond the function itself?
client.on(Events.ThreadCreate, async (thread: ThreadChannel) => {
if (thread.parent?.name === 'whatever') {
const lastMessage = (await thread.messages.fetch({ limit: 1 })).first();

// Checks if the bot has already sent this message
if (lastMessage && lastMessage?.author.id === thread.guild.members.me?.id) {
return
}

return thread.send({
content: "whatever"
})
}
})
client.on(Events.ThreadCreate, async (thread: ThreadChannel) => {
if (thread.parent?.name === 'whatever') {
const lastMessage = (await thread.messages.fetch({ limit: 1 })).first();

// Checks if the bot has already sent this message
if (lastMessage && lastMessage?.author.id === thread.guild.members.me?.id) {
return
}

return thread.send({
content: "whatever"
})
}
})
59 replies
DIAdiscord.js - Imagine a bot
Created by Axe on 9/4/2024 in #djs-questions
Issue catching unreact event after bot has restarted
When I react to a message then restart the bot then unreact it doesnt catch the unreaction event. Function: https://codeshare.io/pAe73x
// Process existing reactions
for (const [_, reaction] of roleMessage.reactions.cache) {
const users = await reaction.users.fetch();
users.forEach(async (user: User) => {
if (user.bot) return;

try {
const member = await guild.members.fetch(user.id);

const emoji = reaction._emoji.name;
const reactionText = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reactionText.trim()) {
await member.roles.add(roleIds[i]);
break;
}
}
} catch (error: any) {
if (error.code === 10007) {
// Member not found (likely left the guild)
} else {
console.error("Error fetching member or adding role:", error);
}
}
});
}
// Process existing reactions
for (const [_, reaction] of roleMessage.reactions.cache) {
const users = await reaction.users.fetch();
users.forEach(async (user: User) => {
if (user.bot) return;

try {
const member = await guild.members.fetch(user.id);

const emoji = reaction._emoji.name;
const reactionText = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reactionText.trim()) {
await member.roles.add(roleIds[i]);
break;
}
}
} catch (error: any) {
if (error.code === 10007) {
// Member not found (likely left the guild)
} else {
console.error("Error fetching member or adding role:", error);
}
}
});
}
if I log reactionText it logs my reaction successfully after the restart. If I then wait for
console.log("Finished processing existing reactions.");
console.log("Finished processing existing reactions.");
12 replies