试图通过命令来赋予角色角色。 -discord.js

时间:2020-08-04 21:17:42

标签: javascript discord.js

这是我正在执行的代码,但是由于某种原因,该功能不正确,因为它不了解mention的最终含义。

const { Client } = require("discord.js");

module.exports = {
    name: 'cargo',
    description: 'Give a role to an user by reaction',
    execute(message, args) {
        function getRoleFromMention(mention) {
            if (!mention)
                console.log('No role was passed');
        
            if (mention.startsWith('<@&') && mention.endsWith('>')) {
                mention = mention.slice(3, -1);
        
                console.log('\x1b[34m%s\x1b[0m', mention)
                return Client.roles.cache.get(mention);
            }
        }
        const messageId = args.slice(0, 1);
        const roleEmoji = args.slice(1, 2);
        const roleMention = args.slice(2, 3);
        //const roleGive = getRoleFromMention(`${roleMention}`);
        console.log('\x1b[35m%s\x1b[0m', 'messageId:', messageId, 'roleEmoji:', roleEmoji, 'roleMention', roleMention); // show each argument
        console.log(message.author.username, ':' ,message.content); // show message and the author
        console.log(getRoleFromMention(`${roleMention}`))
        message.channel.messages.fetch(`${messageId}`).then(msg => msg.react(`${roleEmoji}`));
        //GuildMemberRoleManager.member.add(`${roleGive}`)
    }
}

这是我每次使用命令时在终端上收到的信息。

messageId: [ '740313747872153660' ] roleEmoji: [ '�' ] roleMention [ '<@&609821930123427859>' ]
Sr_Aranha : qbcargo 740313747872153660 �  <@&609821930123427859>
609821930123427859
TypeError: Cannot read property 'cache' of undefined
    at getRoleFromMention (C:\Users\srara\Desktop\JavaScript\qbomBOT\src\commands\cargo.js:15:37)
    at Object.execute (C:\Users\srara\Desktop\JavaScript\qbomBOT\src\commands\cargo.js:25:21)
    at Client.<anonymous> (C:\Users\srara\Desktop\JavaScript\qbomBOT\src\index.js:36:17)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\Discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\Discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\Discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\Discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\Discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\srara\Desktop\JavaScript\qbomBOT\node_modules\ws\lib\event-target.js:125:16)

1 个答案:

答案 0 :(得分:0)

客户端没有任何角色归属。您需要改为公会。

return Client.roles.cache.get(mention);

return message.guild.roles.cache.get(mention);