clkhlum02
clkhlum02

Reputation: 57

Discord bot unintentionally spamming messages after user response

I am making a discord bot that basically detects a word the user sends in a message and it will reply with a message. But the problem is that my bot is spamming the message instead of sending the message just once. I am not sure what is happening, I've looked all over the internet for a solution and couldn't find one which is why I came here for help.

                let Discord;
                let Database;
                if(typeof window !== "undefined"){
                    Discord = DiscordJS;
                    Database = EasyDatabase;
                } else {
                    Discord = require("discord.js");
                    Database = require("easy-json-database");
                }
                const delay = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms));
                const s4d = {
                    Discord,
                    client: null,
                    tokenInvalid: false,
                    reply: null,
                    joiningMember: null,
                    database: new Database("./db.json"),
                    checkMessageExists() {
                        if (!s4d.client) throw new Error('You cannot perform message operations without a Discord.js client')
                        if (!s4d.client.readyTimestamp) throw new Error('You cannot perform message operations while the bot is not connected to the Discord API')
                    }
                };
                s4d.client = new s4d.Discord.Client({
                    fetchAllMembers: true
                });
                s4d.client.on('raw', async (packet) => {
                    if(['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)){
                        const guild = s4d.client.guilds.cache.get(packet.d.guild_id);
                        if(!guild) return;
                        const member = guild.members.cache.get(packet.d.user_id) || guild.members.fetch(d.user_id).catch(() => {});
                        if(!member) return;
                        const channel = s4d.client.channels.cache.get(packet.d.channel_id);
                        if(!channel) return;
                        const message = channel.messages.cache.get(packet.d.message_id) || await channel.messages.fetch(packet.d.message_id).catch(() => {});
                        if(!message) return;
                        s4d.client.emit(packet.t, guild, channel, message, member, packet.d.emoji.name);
                    }
                });
                s4d.client.login('tokenNumber').catch((e) => { s4d.tokenInvalid = true; s4d.tokenError = e; });



s4d.client.on('message', (s4dmessage) => {
  if (s4dmessage.content.includes('oranges')) {
    s4dmessage.channel.send(String('Yum! I love eating oranges!'));
  }

}
);

                s4d;
            

Upvotes: 1

Views: 527

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23141

Your bot checks if the message contains the word "orange" and if it does, sends a response that it loves eating oranges. As the response also contains the word "orange", your bot repeats this function until you're rate limited.

Make sure you check if the message author is a bot, and if it is, simply exit by using return:

s4d.client.on('message', (s4dmessage) => {
  if (s4dmessage.author.bot) return;
  if (s4dmessage.content.includes('oranges')) {
    s4dmessage.channel.send(String('Yum! I love eating oranges!'));
  }
});

Upvotes: 1

Related Questions