【问题标题】:Javascript: Anti-spam Automoderator (Discord.js)Javascript:反垃圾邮件自动审核器 (Discord.js)
【发布时间】: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


【解决方案1】:

试试这个:

const usersMap = new Map();
const LIMIT = 7;
const DIFF = 5000;

client.on('message', async(message) => {
    if(message.author.bot) return;
    
    if(usersMap.has(message.author.id)) {
        const userData = usersMap.get(message.author.id);
        const { lastMessage, timer } = userData;
        const difference = message.createdTimestamp - lastMessage.createdTimestamp;
        let msgCount = userData.msgCount;
        console.log(difference);

        if(difference > DIFF) {
            clearTimeout(timer);
            console.log('Cleared Timeout');
            userData.msgCount = 1;
            userData.lastMessage = message;
            userData.timer = setTimeout(() => {
                usersMap.delete(message.author.id);
                console.log('Removed from map.')
            }, TIME);
            usersMap.set(message.author.id, userData)
        }
        else {
            ++msgCount;
            if(parseInt(msgCount) === LIMIT) {

               message.reply("Warning: Spamming in this channel is forbidden.");
              message.channel.bulkDelete(LIMIT);
               
            } else {
                userData.msgCount = msgCount;
                usersMap.set(message.author.id, userData);
            }
        }
    }
    else {
        let fn = setTimeout(() => {
            usersMap.delete(message.author.id);
            console.log('Removed from map.')
        }, TIME);
        usersMap.set(message.author.id, {
            msgCount: 1,
            lastMessage : message,
            timer : fn
        });
    }
})

【讨论】:

    猜你喜欢
    • 2014-05-19
    • 2021-08-16
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多