英文:
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
问题在于你试图访问未定义的 client
的 channels
属性。
对于斜杠命令,你可以通过使用 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');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论