将用户ID添加到Array,以便在返回公会时被禁止吗?

时间:2020-05-21 14:26:24

标签: javascript node.js discord.js

如标题中所述,我想扩展禁用函数的当前代码(包括在下面)。
如果用户离开了行会/服务器,我如何将其userID添加到数组中?这样,每次有人加入行会时,我就可以遍历它,并检查他们以前是否被禁止过:如果这样,他们将被正确禁止。

因此过程将是:

  • <prefix>ban <userid>
  • 如果用户不在行会中,则将ID添加到阵列并显示禁止消息
  • 当新成员加入时,针对Array检查它们,如果ID存在,则禁止它们并将其从Array中删除。
 switch (args[0]) {

    case 'ban':
      if (!message.content.startsWith(PREFIX)) return;

      if (!message.member.roles.cache.find(r => r.name === 'Moderator') && !message.member.roles.cache.find(r => r.name === 'Staff')) return message.channel.send('You dont not have the required permissions').then(msg => {
        msg.delete({ timeout: 5000 })
      })
      var user1 = message.mentions.users.first();

      if (member) {
        // There's a valid user, you can do your stuff as usual
        member.ban().then(() => {
          message.channel.send('User has been banned')
        })
         } else {
        let user = message.mentions.users.first(),
          // The ID will be either picked from the mention (if there's one) or from the argument
          // (I used args, I don't know how your variable's called)
          userID = user ? user.id : args[1]


        if (userID) {
          // Read the existing banned ids. You can choose your own path for the JSON file
          let bannedIDs = require('./bannedIDs.json').ids || []

          // If this is a new ID, add it to the array
          if (!bannedIDs.includes(userID)) bannedIDs.push(userID)

          // Save the file
          fs.writeFileSync('./bannedIDs.json', JSON.stringify({ ids: bannedIDs }))

          // You can then send any message you want
          message.channel.send('User added to the list')
        } else {
          message.channel.send('No ID was entered')
        }
      }
  }

}

);
bot.on('guildMemberAdd', member => {
  let banned = require('./bannedIDs.json').ids || []
  if (banned.includes(member.id)) {
    // The user should be properly banned
    member.ban({
      reason: 'Previously banned by a moderator.'
    })

    // You can also remove them from the array
    let newFile = {
      ids: banned.filter(id => id != member.id)
    }
    fs.writeFileSync('./bannedIDs.json', JSON.stringify(newFile))
  }
})

1 个答案:

答案 0 :(得分:0)

这是可以做到的,但是您需要手动获取用户ID,因为您不能提及不在公会中的人(从技术上讲可以,但是无论如何您都需要获取ID)。

如果没有用户提及或有一个用户提及,但该用户不在行会中,则需要获取其ID并将其推送到数组中。您可能需要将该数组保存在JSON文件中,以便在两次重装之间保留该数组:您可以使用fs.writeFileSync()
这是一个示例:

let member = message.mentions.members.first()
if (member) {
  // There's a valid user, you can do your stuff as usual
  member.ban().then(() => {
    message.channel.send('User has been banned')
  })
} else {
  let user = message.mentions.users.first(),
    // The ID will be either picked from the mention (if there's one) or from the argument
    // (I used args, I don't know how your variable's called)
    userID = user ? user.id : args[0]

  if (userID) {
    // Read the existing banned ids. You can choose your own path for the JSON file
    let bannedIDs = require('./bannedIDs.json').ids || [] // WRONG: read the edit below

    // If this is a new ID, add it to the array
    if (!bannedIDs.includes(userID)) bannedIDs.push(userID)

    // Save the file
    fs.writeFileSync('./bannedIDs.json', JSON.stringify({ ids: bannedIDs }))

    // You can then send any message you want
    message.channel.send('User added to the list')
  } else {
    message.channel.send('No ID was entered')
  }
}

请记住,您必须导入fs模块,该模块包含在Node中:

const fs = require('fs')

此外,创建一个如下所示的JSON文件:

{
  ids: []
}

新用户加入后,您可以检查其ID:

client.on('guildMemberAdd', member => {
  let banned = require('./bannedIDs.json').ids || [] // WRONG: read the edit below
  if (banned.includes(member.id)) {
    // The user should be properly banned
    member.ban({
      reason: 'Previously banned by a moderator.'
    })

    // You can also remove them from the array
    let newFile = {
      ids: banned.filter(id => id != member.id)
    }
    fs.writeFileSync('./bannedIDs.json', JSON.stringify(newFile))
  }
})

编辑:

如何解决正在缓存的阵列的问题:

  1. 启动bot时,仅加载一次数组,并将其存储在变量中。每次需要读取或写入数组时,都使用该变量,并定期更新JSON文件。
  2. 正如您在评论中提到的,您可以将require()替换为fs.readFileSync()。您可以使用此fs方法读取文件,然后可以使用JSON.parse()函数来解析JSON对象。
    操作方法如下:
// Wrong way to do it:
let bannedIDs = require('./bannedIDs.json').ids || []

// Better way to do it:
let file = fs.readFileSync('./bannedIDs.json')
let bannedIDs = JSON.parse(file).ids || []