0

我无法从 message.mentions.roles.first() 和 message.mentions.members.first() 获取正确信息

这是我在 Index.js 中的命令处理程序

bot.on('message', async (message, guild, helpList, roles) => {
    if (!message.content.startsWith(prefix) || message.author.bot || !message.member.hasPermission('MANAGE_ROLES')) return;
    const args = message.content.slice(prefix.length).split(' ') ;
    const command = args.shift().toLowerCase();
    const aRole = message.mentions.roles.first();
    const Role = aRole.id
    const name = args.join(' ');
    const User = message.mentions.members.first();

    if (!Role && command !== 'create' && command !== 'give' && command !== 'take' && command !== 'help' && command !== 'list') {return message.channel.send('Sorry, that role was not found.')}
    if (!User && (command == 'give' || command == 'take') ) {return message.reply ("That name doesnt match a user.");}

    try{
      bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
    } catch(e){
      console.log(e)
    }  

});

      bot.commands.get(command).execute(message, Role, name, User, guild, helpList);
    } catch(e){
      console.log(e)
    }  

});

这是我的 take.js,一个从指定成员中删除角色的命令。

module.exports = {
    name: 'take',
    description: '',
  execute(message, Role, User, roles){ 
     try{
    User.roles.remove(Role);
        return message.channel.send (`${Role} has been removed from  ${User}'s list of rolls.`);
      }catch(e){
          console.log(e);
          console.log(Role)
          console.log(User)
      }}}

这是我得到的错误。

TypeError: Cannot read property 'remove' of undefined
    at Object.execute (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/commands/take.js:6:16)
    at Client.<anonymous> (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/index.js:34:33)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:437:22)
    at WebSocketShard.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:294:10)
    at WebSocket.onMessage (/mnt/c/Users/bgera/OneDrive/Desktop/BOTT/RoleBot/node_modules/ws/lib/event-target.js:125:16)
    at WebSocket.emit (events.js:310:20)
711442214043254854
<@&711442214043254854> <@!271438275619586062>

为什么角色不起作用?任何帮助是极大的赞赏。

4

1 回答 1

0

execute当你在这里调用函数时,实际上在你的代码中

bot.commands.get(command).execute(message, Role, name, User, guild, helpList);

中的execute函数take.js将等待这些参数:

  • [参数] => [收到的变量类型]
  • 消息 =>消息
  • 角色 => 字符串(提到的角色的 ID)
  • 用户 =>字符串 (不是预期的GuildMember
  • 公会 => 未定义
  • 帮助列表 => 未定义

事实上,按照你给execute函数提供参数的顺序,你给的是变量name而不是User。因此,它不是UserGuildMember 而是一个字符串,当然String.rolesundefined,这就是错误说的原因cannot read property 'remove' of undefined

要修复您的错误,您应该修改

bot.commands.get(command).execute(message, Role, name, User, guild, helpList);

bot.commands.get(command).execute(message, Role, User, name, guild, helpList);

(仔细看,我name用参数反转User参数)

但是等等,还没完

代码中的另一个问题是您声明了太多参数。

bot.on("message", callback(Message))只会callback()使用一个参数调用(请参阅此处的文档)。

guild, helpList, roles在这里总是undefined作为参数。

所以你应该删除不必要的参数:

bot.on("message", (message) => {
    ...
}

execute函数调用相同,删除未使用的参数:

bot.commands.get(command).execute(message, Role, User);

我想就是这样。我希望我是可以理解的:D

于 2020-05-18T01:19:26.210 回答