首页 > 解决方案 > 无法在 discord.js 中获取频道

问题描述

所以,我一直在做一个建议命令discord.js,我不断收到以下错误:

Cannot read property 'channels' of undefined

这是我的代码,我尝试以多种方式更改它,但它不起作用。

module.exports = {
    name: "suggestion",
    aliases: ['suggest', 'suggestion'],
    permissions: [],
    description: "suggetion command",
    execute(message, args, cmd, client, discord) {
        let channel = message.guild.channels.cache.get("865868649839460353");
        if (!channel)
            return message.reply('A suggestions channel does not exist! Please create one or contact a server administrator.')
            .then(message => {
                message.delete(6000)
            })
            .catch(error => {
                console.error;
            });
    }
}

标签: javascriptdiscord.js

解决方案


第一个想法:您正在执行的频道不是公会。

通过修复此问题,您可以执行以下操作:

if(!message.guild){
     return
}

第二个想法:您可能在错误的服务器中指定了错误的频道。请执行下列操作:

let guild = client.guilds.cache.get(`YourGuildId`)
let channel = guild.channels.cache.get(`ChannelId`)

或者

let guild = client.guilds.cache.get(`${message.guild.id}`)
let channel = guild.channels.cache.get(`ChannelId`)

我希望这些想法对你有所帮助!<3


推荐阅读