我正在处理一个高级建议命令,这是我目前的代码:
if (command === 'accept') {
try {
const suggestionchan = bot.channels.cache.get('840493081659834403');
const acceptreason = message.content.split(' ')[2];
if(!acceptreason) return message.lineReply("Please provide the reason!")
const messageid = message.content.split(' ')[1];
console.log(messageid)
if(!messageid) return message.lineReply("Please provide the message id of the suggestion!")
const suggestionData = suggestionchan.messages.fetch(messageid)
const suggestionembed = suggestionData.embeds[0];
const acceptembed = new Discord.MessageEmbed()
.setAuthor(suggestionembed.author.name, suggestionembed.author.iconURL)
.setTitle("Suggestion!")
.setDescription(suggestionembed.description)
.setColor("GREEN")
.addFields(
{ name: `Accepted by ${message.author.tag}`, value: `**Reason:**\n${acceptreason}`}
)
.setFooter("Bot made by Sɱαɾƚx Gαɱιɳɠ YT")
suggestionembed.edit(acceptembed)
message.delete();
const user = bot.users.cache.find( (u) => u.tag === suggestionembed.author.name);
user.send(acceptembed)
} catch (err) { console.log(err)
message.lineReply("That message ID is not a valid ID!")}
}
我在 suggestionembed = suggestionData.embeds[0];
部分出现错误
错误是TypeError: Cannot read property '0' of undefined at Client.<anonymous>
答案 0 :(得分:1)
suggestionchan.messages.fetch
返回一个 promise,因此您需要先解决它。您可以使用 .then()
方法或 await
关键字(如下所示)。
检查获取的消息是否有任何嵌入也是一个好主意。
if (command === 'accept') {
try {
const suggestionChannel = bot.channels.cache.get('840493081659834403');
const acceptReason = message.content.split(' ')[2];
if (!acceptReason)
return message.lineReply('Please provide the reason!');
const messageId = message.content.split(' ')[1];
if (!messageId)
return message.lineReply('Please provide the message id of the suggestion!');
const suggestionData = await suggestionChannel.messages.fetch(messageId);
const suggestionEmbed = suggestionData.embeds[0];
if (!suggestionEmbed)
return message.lineReply(`The message with ID ${messageId} doesn't have any embeds!`);
const acceptEmbed = new Discord.MessageEmbed()
.setAuthor(suggestionEmbed.author.name, suggestionEmbed.author.iconURL)
.setTitle('Suggestion!')
.setDescription(suggestionEmbed.description)
.setColor('GREEN')
.addFields({
name: `Accepted by ${message.author.tag}`,
value: `**Reason:**\n${acceptReason}`,
})
.setFooter('Bot made by Sɱαɾƚx Gαɱιɳɠ YT');
suggestionData.edit(acceptEmbed);
message.delete();
const user = bot.users.cache.find(
(u) => u.tag === suggestionEmbed.author.name,
);
user.send(acceptEmbed);
} catch (err) {
console.log(err);
message.lineReply('That message ID is not a valid ID!');
}
}