简体   繁体   English

Discord Bot 如何删除特定用户角色

[英]Discord Bot how to remove specific user roles

I have been trying to make a bot which is, if the user has 'role 1' then he typed in channel 'role 2' the bot should check if he has any of 'role 1 or role 3' remove them from the user then add 'role 2' to the user.我一直在尝试制作一个机器人,如果用户有“角色 1”,那么他输入频道“角色 2”,机器人应该检查他是否有“角色 1”或“角色 3”中的任何一个,然后将它们从用户中删除向用户添加“角色 2”。

if (message == 'role 2') {
  var role = message.guild.roles.find("name", "2");

  if (message.member.roles.has('1')) {
    console.log('user has role 1');
    await message.member.removeRole("1");
    try {
      console.log('removed role 1');
    } catch(e){
      console.error(e)
    }
  }
  message.member.addRole(role);
}

But this isn't working, it's only adding roles not removing.但这不起作用,它只是添加角色而不是删除角色。 console.log prints the following: console.log打印以下内容:

DeprecationWarning: Collection#find: pass a function insteadDeprecationWarning: Collection#find: pass a function instead DeprecationWarning: Collection#find: 传递一个函数DeprecationWarning: Collection#find: 传递一个函数

How I can check for user roles and remove them before adding a new one?如何在添加新角色之前检查用户角色并删除它们?


Edit: error fixed with this new code:编辑:使用此新代码修复的错误:

var role = message.guild.roles.find(role => role.name === "2")

But removing role command still not working.但是删除角色命令仍然不起作用。

  • It appears like message is a Message object.看起来message是一个Message对象。 You should be comparing its content property rather than the object itself.您应该比较其content属性而不是对象本身。
  • As Saksham Saraswat has also stated, you should pass a function into Collection.find() .正如Saksham Saraswat也说过的,您应该将一个函数传递给Collection.find() Not doing so is deprecated.*不建议这样做。*
  • Map.has() searches by key. Map.has()按关键字搜索。 Collection s use Discord IDs as their keys, which are Snowflakes . Collection s 使用 Discord ID 作为它们的键,它们是Snowflakes The ID shown in your code is not an ID, so the block for that if statement isn't executed.代码中显示的 ID 不是 ID,因此不会执行该if语句的块。
  • The way you've written await(...) is for executing a function.您编写await(...)是用于执行函数。 See the documentation here on the await keyword.请参阅此处有关await关键字的文档。 Note that it can only be used inside of async functions .请注意,它只能在异步函数内部使用。
  • You aren't catching any rejected Promises .*你没有发现任何被拒绝的承诺。*

* This is not affecting the current outcome of your code. * 这不会影响您代码的当前结果。

Implementing these solutions...实施这些解决方案...

if (message.content === 'role 2') {
  try {
    // message.member will be null for a DM, so check that the message is not a DM.
    if (!message.guild) return await message.channel.send('You must be in a guild.');

    // Find Role 2.
    const role2 = message.guild.roles.find(role => role.name === '2');
    if (!role2) return console.log('Role 2 missing.');

    // If the user has Role 1, remove it from them.
    const role1 = message.member.roles.find(role => role.name === '1');
    if (role1) await message.member.removeRole(role1);

    // Add Role 2 to the user.
    await message.member.addRole(role2);
  } catch(err) {
    // Log any errors.
    console.error(err);
  }
}

I am guessing in message.guild.roles.find you have to pass in a function like message.guild.roles.find(function);我猜在 message.guild.roles.find 你必须传入一个函数,比如 message.guild.roles.find(function); also I think find is deprecated which means, out of date and substituted for a better solution/function.此外,我认为 find 已被弃用,这意味着已过时并取代了更好的解决方案/功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM