类型错误:无法读取 setInterval 中未定义的属性"消息"



这里有个问题。我正在制作关于bot的连接和事物的更新状态,但是当我指定通道和消息时出现错误。下面是错误:

let msg = chn.messages.fetch("862718466803761163")
^
TypeError: Cannot read property 'messages' of undefined

我检查了很多次,甚至我的朋友都说没有问题。下面是代码:

setInterval(function() {
const { MessageEmbed } = require('discord.js')
let chn = client.channels.cache.get(c => c.id === "862704580423581718")
let msg = chn.messages.fetch("862718466803761163")
let state
let ping = client.ws.ping
if (ping < 200) {
emoji = '<:the_connection_is_excellent:855856200078589982>'
state = "Volta's connection is excellent."
}
if (ping < 600) {
emoji = '<:the_connection_is_good:873563598973177876>'
state = "Volta's connection is a bit slow."
}
if (ping < 1000) {
emoji = '<:the_connection_is_bad:873563598977388554>'
state = "Volta's connection is stable."
}
const embed = new MessageEmbed()
.setColor('#3EDD1B')
.setTitle('-- Actual State --')
.setDescription(`${emoji} [${ping}ms]: ${state}nn`
+ `<a:loading:855847974521536512> It updates every **5 minutes**.`)
.setFooter(`:zap: Uptime: ${prettyMs(client.uptime)}`)
msg.edit(embed)
}, checkthe_interval)

是的,它在setInterval中。是的,它已经在赛事中准备好了。不,它不是一个模块。

问题是,当您定义chn时,您实际上是将c.id更改为862704580423581718,而不是寻找它。那会返回一个空对象。获取消息的功能为<channel>.messages.fetch(...)

完整的示例:

setInterval(async function() { // Start the interval
const { MessageEmbed } = require('discord.js'); // Define MessageEmbed
let chn = client.channels.cache.get(c => c.id == "862704580423581718"); // Find a channel in the cache
let msg = await chn.messages.fetch("862718466803761163"); // Fetch the message from an ID
let state;
let ping = client.ws.ping;
if (ping < 200) {
emoji = '<:the_connection_is_excellent:855856200078589982>';
state = "Volta's connection is excellent.";
}
if (ping < 600) {
emoji = '<:the_connection_is_good:873563598973177876>';
state = "Volta's connection is a bit slow.";
}
if (ping < 1000) {
emoji = '<:the_connection_is_bad:873563598977388554>';
state = "Volta's connection is stable.";
}
const embed = new MessageEmbed()
.setColor('#3EDD1B')
.setTitle('-- Actual State --')
.setDescription(`${emoji} [${ping}ms]: ${state}nn`
+ `<a:loading:855847974521536512> It updates every **5 minutes**.`)
.setFooter(`:zap: Uptime: ${prettyMs(client.uptime)}`)
msg.edit(embed)
}, checkthe_interval)

好了,问题解决了。我不需要(c => c.id === "862704580423581718");下面是解决后的代码:

setInterval(function() {
const { MessageEmbed } = require('discord.js')
let chn = client.channels.cache.get("862704580423581718")
let msg = chn.messages.fetch("862718466803761163")
let state
let ping = client.ws.ping
if (ping < 200) {
emoji = '<:the_connection_is_excellent:855856200078589982>'
state = "Volta's connection is excellent."
}
if (ping < 600) {
emoji = '<:the_connection_is_good:873563598973177876>'
state = "Volta's connection is a bit slow."
}
if (ping < 1000) {
emoji = '<:the_connection_is_bad:873563598977388554>'
state = "Volta's connection is stable."
}
const embed = new MessageEmbed()
.setColor('#3EDD1B')
.setTitle('-- Actual State --')
.setDescription(`${emoji} [${ping}ms]: ${state}nn`
+ `<a:loading:855847974521536512> It updates every **5 minutes**.`)
.setFooter(`:zap: Uptime: ${prettyMs(client.uptime)}`)
msg.edit(embed)
}, checkthe_interval)

最新更新