因此,我一直在尝试在不和谐的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');
我们将不胜感激!
答案 0 :(得分:0)
嗨,欢迎来到Stack Overfow!
这里有几个问题:
antispam
,始终要声明一次函数antispam
在spam
范围内创建antispam
的内容,antispam
完成执行后立即删除的内容client.on('message'...
。再说一次,只需做一次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语句丢失而做出响应(特别是因为每次发送一条消息时,垃圾邮件变量都会增加)。是的,编码愉快!