【发布时间】:2017-12-15 22:56:22
【问题描述】:
我正在编写一个多用途的 Discord 机器人来替换一些较小的机器人,并且我正在寻找一段代码,用于识别重复消息或在很短的时间段内(比如说 5000 毫秒)发送的消息的功能。
这是可以用来实现这个想法的方法。
client.on("message", (message) => {
//let's use something like a spam variable for 10 or more messages sent within 5000ms
if(message.content === spam) {
message.reply("Warning: Spamming in this channel is forbidden.");
console.log(message.author.username + " (" + message.author.id + ") has sent 10 messages or more in 5 seconds in " + message.channel.name + ".");
}
});
作为参考,我还使用 ~delete [n] 命令创建了删除消息的功能。它看起来像这样:
//this will only delete one message in the channel, the most recent one.
message.delete(1000);
//1000 represents the timeout duration. it will only delete one message, regardless of the value.
//we can delete multiple messages with this, but note it has to come before the reply message.
message.channel.bulkDelete(11);
我正在考虑以某种方式将删除命令与识别垃圾邮件结合起来。如果您有任何想法,那将是完美的。
【问题讨论】:
-
您实际上可以稍微更改您的 console.log 消息。使用 ES6,您可以将 console.log() 行从
message.author.username + " (" + message.author.id + ") has sent 10 messages or more in 5 seconds in " + message.channel.name + "."更改为`${message.author.username} (${message.author.id}) has send 10 messages or more in 5 seconds in ${message.channel.name}.` -
这要简单得多。谢谢
-
不客气 =D 我想我有办法了,让我测试一下!
-
另外,你的机器人是不是只有一个公会才有的?如果是这样,您也许可以在启动时为每个公会成员创建一个数组(尽管这不是最好的主意)
-
是的,它目前只有一个公会专有。我很期待那个反垃圾邮件解决方案 XD
标签: javascript command bots discord discord.js