简体   繁体   English

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

[英]Trying to make an command to give roles by reactions. - discord.js

This is the code i'm doing, but for some reason, the function isn't correct, since it don't understand what mention is at the final.这是我正在做的代码,但由于某种原因,function 不正确,因为它不明白最后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}`)
    }
}

And this is what I receive on terminal everytime the command is used.这就是我每次使用该命令时在终端上收到的信息。

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)

Client doesn't have any roles attribut.客户端没有任何角色属性。 You need to get the guild instead.你需要得到公会。

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

to

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

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

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