【问题标题】:How to kick after a certain amount of infractions discord.js如何在一定数量的违规后踢 discord.js
【发布时间】:2021-03-05 09:16:05
【问题描述】:

我正在开发一个 discord.js 审核机器人,我希望它在某些违规行为后踢出某人。有没有办法让它记录这些违规行为(咒骂),然后在一定次数后踢?机器人已经找到并删除了脏话,所以我希望能够在该系统中实现这一点。我想知道是否有办法将违规记录系统直接构建到反发誓中。我不会把未经修改的代码放进去,因为它是反脏话的,而且它需要一些东西作为基础,但我会在下面放一个经过审查的版本

// require the discord.js module
const Discord = require('discord.js');

// create a new Discord client
const client = new Discord.Client();

// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
    console.log('Ready!');
});

client.login('TOKEN');

client.on('message', message => {
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }
  if (message.content.includes('bad word')) {
    message.delete();
    message.author.send('Please do not swear on this server.');
  }

});

【问题讨论】:

    标签: discord.js bots


    【解决方案1】:

    您应该使用 discord.js Collection,即:

    带有其他实用方法的地图。这在整个 discord.js 中使用,而不是用于任何具有 ID 的数组,以显着提高性能和易用性。

    Map 对象保存键值对并记住键的原始插入顺序。任何值都可以用作键或值。这是一个快速演示:

    // let's say we had two people: John and Sarah
    const people = new Map();
    
    // each of them were a different age
    people.set('John', 25); // in this example, 'John' is the key, and 25 is the value
    people.set('Sarah', 19); // in this example, 'Sarah' is the key, and 25 is the value
    
    // each person has an individual age
    // you can `get()` the key, and it will return the value
    console.log(`Sarah is ${people.get('Sarah')} years old.`);
    console.log(`John is ${people.get('John')} years old.`);

    您可以使用这种格式创建一个集合,其中每个键代表不同的用户 ID,每个值代表他们有多少违规行为。每当有人发誓时,将他们的违规计数加一,然后检查它是否超过了给定的数字。

    此外,您可以通过将每个坏词放入一个数组中,然后使用 Array.prototype.some()

    在每个元素中迭代 相同的函数,大大简化您当前的系统
    // const Discord = require('discord.js');
    
    const infractions = new Discord.Collection();
    const words = ["bad word", "bad word", "bad word", "..."];
    
    // since it's the same function, you should only have to write it once
    if (words.some((word) => message.content.includes(word))) {
      message.delete();
      message.author.send("Please do not swear on this server.");
      if (!infractions.has(message.author.id))
        // if they don't have any infractions
        infractions.set(message.author.id, 1);
      // create a new element for them with 1 infraction
      // otherwise, increment their existing infractions by 1
      else
        infractions.set(message.author.id, infractions.get(message.author.id) + 1);
      // check if it's above the specified number
      if (infractions.get(message.author.id) >= 5) {
        // kick the member, send log messages, etc.
      }
    }
    

    【讨论】:

    • 它有效,除非他们犯了这么多违规行为,否则它不会采取任何行动。我现在将它设置为发送消息,但它什么也不做
    • 有没有办法记录谁被踢了并使用频道ID将其发送到日志服务器?
    猜你喜欢
    • 2021-06-01
    • 2021-11-13
    • 2021-04-15
    • 2021-08-22
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多