gpt4 book ai didi

javascript - Discord.js embedMsg 未定义?

转载 作者:行者123 更新时间:2023-11-28 03:15:10 26 4
gpt4 key购买 nike

您好,我正在尝试制作一个不和谐的机器人,一旦您单击添加到其嵌入消息中的 react ,它就会根据您单击的内容响应一条消息,一个表示是,一个表示否。

一切看起来都很好,但是当我运行命令时,我得到 const Collector = embedMsg.messageembedMsg 未定义。我已经尝试了一切,所以我很感谢我能得到的任何类型的帮助

这是我的代码:

const {Client, RichEmbed} = require('discord.js');
const client = new Client();
client.once('ready', () => {
console.log('dance time');

client.on('message', message => {
if(message.author.bot)
{
if(message.embeds)
{
const embedMsg= message.embed.find(msg => msg.title === 'Boogie Time!?');
if(embedMsg)
{
embedMsg.message.react('✅')
.then(reaction => reaction.message.react('❌'))

.catch(error => console.error);
}
}
return;

}
// Do this after you've added reactions

// This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌') && user.id === message.author.id;

// Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
const collector = embedMsg.message.createReactionCollector(filter, {time: 30000});

// This event is emitted when a reaction passes through the filter
collector.on('collect', r => r.name === '✅' ? console.log('Reacted Yes') : console.log('Reacted No'));

if(message.content.toLowCase() === 'boogie')
{
const embed = new RichEmbed();
embed.setTitle("Boogie Time!?")
embed.setColor("GREEN")
embed.setDescription("Are you sure?")
message.channel.send(embed);
}
})
});

最佳答案

代码中的第一个问题是 client.on('ready' ) 阻止您在 client.on('message' block 之后结束他。正确的方法是:

client.on('ready', () => {
console.log('dance time');
})

message.embeds 它的集合数组,所以需要使用

const embedMsg = message.embeds.find(msg => msg.title === 'Boogie Time!?');

为了获得代码的最佳可读性,尽量不要使用 if(somethink) then do somethink更好的方式是 if(!somethink) return;

这将使您摆脱大量括号。在您的代码中,您尝试等待 react ,无论此消息是否嵌入。并且在使用 return 添加 react 后停止函数,因此收集器将永远无法工作。

当您 const 收集器时,您不需要 embedMSG.message 因为 find 方法已经返回一条消息。

所以你的代码必须是这样的

    const {Client, RichEmbed} = require('discord.js');
const client = new Client();
client.once('ready', () => {
console.log('dance time');
})
client.on('message', message => {

if(!message.author.bot || !message.embeds) return
const embedMsg = message.embeds.find(embed => embed.title === 'Boogie Time!?');
if(!embedMsg) return

embedMsg.react('🍎')
.then(() => message.react('🍊'))
.catch(() => console.error('One of the emojis failed to react.'));

// This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
const filter = (reaction, user) => (reaction.emoji.name === '?' || reaction.emoji.name === '?') && user.id === message.author.id;

// Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter

// This event is emitted when a reaction passes through the filter
collector.on('collect', r => r.name === '?' ? console.log('Reacted Yes') : console.log('Reacted No'));

if(message.content.toLowCase() === 'boogie'){
const embed = new RichEmbed();
embed.setTitle("Boogie Time!?")
embed.setColor("GREEN")
embed.setDescription("Are you sure?")
message.channel.send(embed);
}
})

关于javascript - Discord.js embedMsg 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59708493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com