Discord.js TypeError: Cannot read property '0' of undefined
Discord.js TypeError: Cannot read property '0' of undefined
嗨,我在这段代码中遇到了问题,有什么帮助吗?
client.on("message", message => {
if(message.content.startsWith(prefix+"test")) {
let json = require('./json.json')
let channel = message.guild.channels.cache.get(json.channel);
let msg = channel.messages.fetch(json.msg);
let embed = msg.embeds[0];
embed.addField("User: ", message.author.username, true)
msg.edit({ embeds: [ embed ] });
}
});
错误
let embed = msg.embeds[0];
TypeError: Cannot read property '0' of undefined
我想通过命令向该嵌入消息添加字段,但出现此错误
TextChannel.messages.fetch
returns 一个承诺。所以你必须把你的代码放在 .then
里面或者使用 async/await。
所以代码将是:
client.on("message", message => {
if (message.content.startsWith(`${prefix}test`)) {
let json = require('./json.json');
let channel = message.guild.channels.cache.get(json.channel);
channel.messages.fetch(json.msg).then(msg => {
let embed = msg.embeds[0];
embed.addField("User: ", message.author.username, true);
msg.edit({ embeds: [embed] });
});
}
});
嗨,我在这段代码中遇到了问题,有什么帮助吗?
client.on("message", message => {
if(message.content.startsWith(prefix+"test")) {
let json = require('./json.json')
let channel = message.guild.channels.cache.get(json.channel);
let msg = channel.messages.fetch(json.msg);
let embed = msg.embeds[0];
embed.addField("User: ", message.author.username, true)
msg.edit({ embeds: [ embed ] });
}
});
错误
let embed = msg.embeds[0];
TypeError: Cannot read property '0' of undefined
我想通过命令向该嵌入消息添加字段,但出现此错误
TextChannel.messages.fetch
returns 一个承诺。所以你必须把你的代码放在 .then
里面或者使用 async/await。
所以代码将是:
client.on("message", message => {
if (message.content.startsWith(`${prefix}test`)) {
let json = require('./json.json');
let channel = message.guild.channels.cache.get(json.channel);
channel.messages.fetch(json.msg).then(msg => {
let embed = msg.embeds[0];
embed.addField("User: ", message.author.username, true);
msg.edit({ embeds: [embed] });
});
}
});