Discord.js 错误 - TypeError:无法读取未定义的属性“执行”

时间:2021-02-02 17:39:27

标签: javascript node.js discord discord.js

目前我正在研究一个具有反应角色的不和谐机器人。但是我不断收到错误:

TypeError: Cannot read property 'execute' of undefined.

client.commands.get('reactionrole').execute(message, args, Discord, client);
                                    ^

我正在使用 heroku 来托管我的机器人,但似乎无法解决这个问题

为了保持简短,我只保留导致错误的命令的代码。

这是我的 main.js 代码。

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

const cheerio = require('cheerio')
const request = require('request')

const fs = require('fs');

const prefix = ';';

client.commands = new Discord.Collection();

// Types message when online and sets Discord Status
client.on("ready", () =>{
    console.log(`Logged in as cosmo boi`);
    client.user.setActivity({
        name: "to your commands sir | ;help",  
        type: "LISTENING", 
    });
 });

 // Commands

 client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
 
    if(command === 'ping'){
        message.channel.send('pong');
    } else if (command === 'reactionrole') {
        client.commands.get('reactionrole').execute(message, args, Discord, client);
    } 
});

client.login('my token is here');

这是我的 reactrole.js 代码:

module.exports = {
    name: 'reactionrole',
    description: "Sets up a reaction role message!",
    async execute(message, args, Discord, client) {
        const channel = '801138223974842420';
        const announcementsRole = message.guild.roles.cache.find(role => role.name === "Announcements Pings");
        const giveawayRole = message.guild.roles.cache.find(role => role.name === "Giveaway Pings");
        const pollRole = message.guild.roles.cache.find(role => role.name === "Poll Pings");
        const ytRole = message.guild.roles.cache.find(role => role.name === "YT Pings");
        const sponsorshipRole = message.guild.roles.cache.find(role => role.name === "Sponsorship Pings");
 
        const announcementsEmoji = '?';
        const giveawayEmoji = '?';
        const pollEmoji = '?';
        const ytEmoji = '⏯️';
        const sponsorshipEmoji = '?';
 
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Choose a role you would like!')
            .setDescription('These roles will be pinged when an announcement/event is announced!\n\n'
                + `${announcementsEmoji} for announcement pings\n`
                + `${giveawayEmoji} for event pings`
                + `${pollEmoji} for poll pings`
                + `${ytEmoji} for YT pings`
                + `${sponsorshipEmoji} for sponsorship pings`);
 
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(announcementsEmoji);
        messageEmbed.react(giveawayEmoji);
        messageEmbed.react(pollEmoji);
        messageEmbed.react(ytEmoji);
        messageEmbed.react(sponsorshipEmoji);
 
        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 === announcementsEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(announcementsRole);
                }
                if (reaction.emoji.name === giveawayEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(giveawayRole);
                }
                if (reaction.emoji.name === pollEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(pollRole);
                }
                if (reaction.emoji.name === ytEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(ytRole);
                }
                if (reaction.emoji.name === sponsorshipEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(sponsorshipRole);
                }
            } 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 === announcementsEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(announcementsRole);
                }
                if (reaction.emoji.name === giveawayEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(giveawayRole);
                }
                if (reaction.emoji.name === pollEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(pollRole);
                }
                if (reaction.emoji.name === ytEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(ytRole);
                }
                if (reaction.emoji.name === sponsorshipEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(sponsorshipRole);
                }
            } else {
                return;
            }
        });
    }
 
}   

1 个答案:

答案 0 :(得分:0)

您似乎已经声明了 client.commands,但实际上并未对其设置任何命令。 This guide 提供了您尝试使用的命令处理程序的综合教程。根据指南,您的代码应该是这样的:

const fs = require('fs');
const Discord = require('discord.js');

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}