discord.js 建议 - 无法读取未定义的属性“通道”

时间:2021-02-23 13:58:05

标签: discord.js

我一直在做一个建议命令,我不知道当前的问题是什么,请帮忙。

module.exports = {
    name: 'suggestions',
    aliases: ['suggest', 'suggestion'],
    permissions: [],
    description: 'creates a suggestions!',
    execute(message, args, cmd, client, discord){
        const channel = message.guild.channels.cache.find(c => c.name === 'suggestions');
        if(!channel) return message.channel.send('suggestions channel does not exist!');
    }
}

错误信息如下:

TypeError: Cannot read property 'channels' of undefined
    at Object.execute (C:\Users\MyName\Desktop\DiscordBots\commands\suggestions.js:7:39)

这是我的主文件:

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

client.commands = new Discord.Collection();
client.events = new Discord.Collection();

['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord);
})


client.login('MYTOKEN');

我认为这是我运行 execute() 的地方:

module.exports = (Discord, client, message) => {
    const prefix = '-';
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));

    if(command) command.execute(client, message, args, Discord);
}

2 个答案:

答案 0 :(得分:0)

请尝试定义频道然后发送消息

client.channels.cache.get("CHANNELID").send(`TEST`);

答案 1 :(得分:0)

这可能行得通;

module.exports = {
    name: 'suggestions',
    aliases: ['suggest', 'suggestion'],
    permissions: [],
    description: 'creates a suggestions!',
    execute(client, message, args, Discord){ // CHANGED
        const channel = message.guild.channels.cache.find(c => c.name === 'suggestions');
        if(!channel) return message.channel.send('suggestions channel does not exist!');
    }
}
相关问题