我如何修复 TypeError:无法读取 undefined

时间:2021-03-08 05:18:08

标签: javascript discord.js

当管理员类型 !competition bot 将发送与代码中的嵌入类似的嵌入时,我想与系统创建一个 discordbot。在嵌入中会有一个反应,当有人选择反应时,机器人会将该人添加到其角色中。但我收到了标题上的错误。这是我的代码:

const client = new Discord.Client({partials: ["MESSAGE", "CHANNEL", "REACTION"]});
const { MessageEmbed } = require("discord.js");

module.exports = {
    name: 'competition',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = '818342514599657513';
        const redTeamRole = message.guild.roles.cache.find(role => role.name === "Red Team");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "Blue Team");

    const redTeamEmoji = ':watermelon:'
    const blueTeamEmoji = ':blueberries:'

    let embed = new Discord.MessageEmbed()
        .setColor('f8c300')
        .setTitle('Pilih Tim')
        .setDescription('Memilih tim akan membuatmu bisa berinteraksi dengan 1 timmu\nTim ini digunakan untuk berkompetisi\n\n'
            + `${redTeamEmoji} Untuk tim Merah`
            + `${blueTeamEmoji} Untuk Tim Biru`);

    let messageEmbed = await message.channel.send(embed);
    messageEmbed.react(redTeamEmoji);
    messageEmbed.react(blueTeamEmoji);

    client.on('messageReactionAdd', async (reaction, user) => {
        if (reaction.message.partial) await reaction.message.fetch();
        if (reaction.partial) await reaction.fetch();
        if (user.bot) return;
        if (!reaction.message.guild) return;

        if (reaction.message.channel.id == channel) {
            if (reaction.emoji.name === redTeamEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(redTeamRole);
                await reaction.message.guild.members.cache.get(user.id).roles.remove(blueTeamRole);
            }
            if (reaction.emoji.name === blueTeamEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.add(blueTeamRole);
                await reaction.message.guild.members.cache.get(user.id).roles.remove(redTeamRole);
            }
        } else {
            return;
        }
    });

    client.on('messageReactionRemove', async (reaction, user) => {
        if (reaction.message.partial) await reaction.message.fetch();
        if (reaction.partial) await reaction.fetch();
        if (user.bot) return;
        if (!reaction.message.guild) return;

        if (reaction.message.channel.id == channel) {
            if (reaction.emoji.name === redTeamEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.remove(redTeamRole);
            }
            if (reaction.emoji.name === blueTeamEmoji) {
                await reaction.message.guild.members.cache.get(user.id).roles.remove(blueTeamRole);
            }
        } else {
            return;
        }
    });
}
}

0 个答案:

没有答案