Discord.js v14 在不同频道发送消息不起作用

huangapple go评论52阅读模式
英文:

Discord.js v14 Sending Message in Separate Channel Not Working

问题

我正在创建一个命令,在按钮点击时应该在特定频道中发送一条消息。

为了实现这一目标,我使用了discord.js文档中提供的代码,如下所示:

const channel = client.channels.cache.get('id');
channel.send('content');

然而,在点击按钮时,控制台会显示以下错误:

TypeError: Cannot read properties of undefined (reading 'channels')

我对discord.js并不是很了解,所以我不知道该怎么办。

这是按钮交互的完整代码:

const filter = i => i.customId === 'accept' && i.member.roles.cache.has('1111582370265563200');
const collector = interaction.channel.createMessageComponentCollector({ filter });
collector.on('collect', async i => {
    if (i.customId === 'accept'){
        accept.setDisabled(true)
        const channel = client.channels.cache.get('1111616693358301237');
        channel.send({embeds: [embedAccept]});
        i.update({embeds: [yesembed]});
    }
});

任何帮助都将不胜感激。提前感谢。

英文:

I am creating a command, where on a button click it is supposed to send a message in a specific channel.

To achieve this, I have used the code given in the discord.js documentations faq;

const channel = client.channels.cache.get('id');
channel.send('content');

However, on clicking the button, the console gives the following error;

> TypeError: Cannot read properties of undefined (reading 'channels')

I'm not very well versed at discord.js whatsoever, so I have no idea what to do.

Here's the full code for the button interaction;

   const filter = i => i.customId === 'accept' && i.member.roles.cache.has('1111582370265563200');
    const collector = interaction.channel.createMessageComponentCollector({ filter });
    collector.on('collect', async i => {
        if (i.customId === 'accept'){
         accept.setDisabled(true)
            const channel = client.channels.cache.get('1111616693358301237');
            channel.send({embeds: [embedAccept]});
         i.update({embeds: [yesembed]});
        }
    });

Any help is appreciated. Thanks in advance.

答案1

得分: 1

问题在于你试图访问未定义的 clientchannels 属性。

对于斜杠命令,你可以通过使用 interaction.client 来访问你的客户端实例。

const channel = interaction.client.channels.cache.get('1111616693358301237');
英文:

The issue is that you're trying to access the channels property of an undefined client.

For slash commands, you can access your client instance by using interaction.client.

const channel = interaction.client.channels.cache.get('1111616693358301237');

huangapple
  • 本文由 发表于 2023年6月11日 20:43:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定