DiscordJS制作机器人,可以在消息响应时分配角色

时间:2020-06-03 19:03:27

标签: javascript node.js discord discord.js

运行该命令后,机器人应发布带有响应的嵌入消息(确实如此,但有错误)。错误与分配我认为的角色有关,因为添加反应时未分配任何角色。如果有使用角色ID而不是搜索角色名称的简便方法,我也可以这样做。

read-me-first.js

module.exports = {
    name: 'read-me-first',
    cooldown: 5,
    description: 'Server verification',
    // eslint-disable-next-line no-unused-vars
    execute(message, args) {
        message.delete();
        const Discord = require('discord.js');
        const exampleEmbed = new Discord.MessageEmbed()
            .setColor('#E33333')
            .setTitle('VERIFICATION')
            .setDescription('<a:loading:717623386247004270> By reacting to this message, you agree to the <#717624229713018910> and gain access to the rest of the discord server. <a:loading:717623386247004270>')
            .attachFiles(['/home/shares/public/BytePvP/bytepvp.png'])
            .setThumbnail('attachment://bytepvp.png')
            // .setTimestamp()
            .setFooter('BytePvP', 'attachment://bytepvp.png');

        if (!message.member.hasPermission('MANAGE_MESSAGES')) {
            return message.reply('You do not have permission to use this command!');
        }
        message.channel.send({ embed: exampleEmbed }).then(embedMessage => {
            embedMessage.react('717623421068116008')
                .catch(() => console.error('One of the emojis failed to react.'));
        });
        const filter = (reaction, user) => {
            return ['717623421068116008'].includes(reaction.emoji.name) && user.id === message.author.id;
        };

        message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
                const reaction = collected.first();

                const role = message.guild.roles.cache.find(role => role.name === 'Member');
                const member = message.mentions.members.first();
                member.roles.add(role);
            });
    },
};

错误:

(node:32547) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of undefined
    at /home/shares/public/BytePvP/commands/read-me-first.js:35:12
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:32547) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:32547) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

所做的编辑:

        message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then((collected, user) => {
                const reaction = collected.first();

                const role = message.guild.roles.cache.find(role => role.name === 'Member');
                const member = message.guild.member(user);
                member.roles.add(role);
            });

新错误:

(node:24026) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'roles' of null
    at /home/shares/public/BytePvP/commands/read-me-first.js:35:12
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:24026) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:24026) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```

1 个答案:

答案 0 :(得分:0)

或者,您可以使用:

.then((collected, user) => {

,然后使用:

将用户转换为成员
const member = message.guild.member(user);