Discord bot的无垃圾邮件功能不起作用

时间:2019-02-17 02:53:32

标签: javascript discord

因此,我一直在尝试在不和谐的bot上创建反垃圾邮件功能,但是查看了一下我的代码,这一切似乎都有效,但是没有成功。我是javascript的新手,所以我不确定什么地方出了错...其他所有内容都可以在我的漫游器上正常运行,而不仅仅是这个。我只粘贴涉及反垃圾邮件的代码部分:

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');

我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

嗨,欢迎来到Stack Overfow!

这里有几个问题:

  • 您要声明两次antispam,始终要声明一次函数
  • 您的间隔没有任何反应。它调用antispamspam范围内创建antispam的内容,antispam完成执行后立即删除的内容
  • 您两次声明client.on('message'...。再说一次,只需做一次
  • 您两次声明`client.on('ready'...)。
  • 如果在spam内增加client.on('message'...),则spam变量将像antispam一样无用。您无法在任何地方访问它(如果尝试访问也无法访问)

因此,这是一个可行的解决方案:

// 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 

您的整个代码:

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');

答案 1 :(得分:0)

// 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 

CodeF0x提供的代码很好,但是请确保添加:

if(msg.author.bot) return

如果正在发送的消息是由漫游器发出的,它将忽略它们。如果您不将此代码添加到您的代码中,请为您的机器人做好垃圾邮件的准备。它会由于if语句丢失而做出响应(特别是因为每次发送一条消息时,垃圾邮件变量都会增加)。是的,编码愉快!