简体   繁体   中英

Discord bot's no spamming feature isn't working

So I have been attempting to make an anti-spam function on a discord bot, but have look over my code and it all seems to work, but it doesn't. I am new to javascript, so I'm not sure whats wrong... Everything else works on my bot, just not this. I am only pasting the part of my code involving the antispam:

 const Discord = require('discord.js'); const client = new Discord.Client(); client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); function antispam() { var spam = 0; } client.on('ready', () => { setInterval(antispam(), 5000); }); client.on('message', msg => { if (spam > 10) { client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!'); } }); function antispam() { var spam = 0; } client.on('message', msg => { var spam = spam + 1; }); client.login('token');

Help would be appreciated!

Hi and welcome to Stack Overfow!

Several issue here:

  • You're declaring antispam twice, declare functions always once
  • Your interval doesn't do anything. It calls antispam what creates spam inside the scope of antispam what gets deleted immediately after antispam finishes executing
  • You're declaring client.on('message'... twice. Again, just do it once
  • You're declaring `client.on('ready'...) twice.
  • If you increase spam inside client.on('message'...) that spam variable will be useless just as in antispam . You don't access it anywhere (you also couldn't if you would try to)

So here's a solution that should work:

// To count the "spam level"
const spam = 0;

// Increase spam everytime someone writes something
client.on('message', msg => {
    // Check if spam level is already too high
    if (spam > 10) {
        client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!');
    } else {
        spam++;
    }
});

// Decrease the spam level, otherwise nobody can't send message until the bot gets restarted
setInterval(() => {
    if (spam > 0) {
        spam--;
    }
}, 10000); // Let's decrease it every 10 seconds 

Your whole code:

 const Discord = require('discord.js'); const client = new Discord.Client(); // To count the "spam level" const spam = 0; client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); // Increase spam everytime someone writes something client.on('message', msg => { // Check if spam level is already too high if (spam > 10) { client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!'); } else { spam++; } }); // Decrease the spam level, otherwise nobody can't send message until the bot gets restarted setInterval(() => { if (spam > 0) { spam--; } }, 10000); // Let's decrease it every 10 seconds client.login('token');

 // To count the "spam level" const spam = 0; // Increase spam everytime someone writes something client.on('message', msg => { if(msg.author.bot) return // Check if spam level is already too high if (spam > 10) { client.channels.get('546117125702680596').send('Hey! You are sending messages too quickly!'); } else { spam++; } }); // Decrease the spam level, otherwise nobody can't send message until the bot gets restarted setInterval(() => { if (spam > 0) { spam--; } }, 10000); // Let's decrease it every 10 seconds

The code provided by CodeF0x is good , but make sure to add:

if(msg.author.bot) return

If the message being sent was by a bot, it will ignore them. IF YOU DO NOT add this to your code, get prepared for your bot to spam. It will respond to itself due to that if statement missing (especially since every time one sends a message, the spam variable will increase). So yeah, happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM