javascript Node.js - Discord.js v14 -获取通道返回未定义/不工作

qfe3c7zg  于 2023-02-15  发布在  Java
关注(0)|答案(2)|浏览(118)

尝试使用此代码定义channel将返回undefined。

const { Events, InteractionType, Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
    ],
  })

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {

        if(interaction.type == InteractionType.ApplicationCommand){
            const command = interaction.client.commands.get(interaction.commandName);

            if (!command) {
                console.error(`No command matching ${interaction.commandName} was found.`);
                return;
            }
    
            try {
                await command.execute(interaction);
            } catch (error) {
                console.error(`Error executing ${interaction.commandName}`);
                console.error(error);
            }
        }
        else if(interaction.isAutocomplete()){
            const command = interaction.client.commands.get(interaction.commandName);

            if(!command) {
                console.error(`No command matching ${interaction.commandName} was found.`);
                return;
            }

            try{
                await command.autocomplete(interaction);
            } catch(error) {
                console.error(error);
            }
        }
         else if(interaction.isButton()){
            console.log(client.channels);
            const channel = await client.channels.fetch('1074044721535656016 ');
            console.log(client.channels);
            console.log(channel);
        }
    },
};

const channel = await client.channels.fetch('1074044721535656016 ');是这里的主线。在代码的底部和顶部是这个问题的重要内容。我相信我没有正确使用fetch方法。
正如console.log(client.channels);所证明的那样,我试图查看通道缓存,但由于某种原因,通道不在那里,因此我使用fetch()而不是cache.get()
谢谢你的帮助!
其他信息:运行npm列表显示:14.7.1版本,16.0.3版本节点js版本19.6.0版本
How I got my channel id
错误信息:

ChannelManager {} //<--- this is from the first console.log
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: Expected token to be set for this request, but none was present

问题是因为通道未定义还是因为它尚未解决?

ruoxqz4g

ruoxqz4g1#

检查您的频道ID,其格式可能如下所示:
公会id /频道id

eulz3vhy

eulz3vhy2#

使用interaction.client,而不是创建新的客户端对象。交互已经有了自己的客户端对象,并具有相关属性。

相关问题