javascript - Bot 说它在 0 个服务器中,有 0 个频道,有 0 个用户
问题描述
我做了一个&bot-info
命令,但每次我使用它时,它都说它在 0 个服务器上,有 0 个频道和 0 个用户,我该如何解决这个问题?一开始我有类似的代码行,当机器人打开时告诉我他所在的服务器、频道和用户的数量,它工作正常
const Discord = require('discord.js')
const client = new Discord.Client();
async function execute(message, args) {
const embed = new Discord.MessageEmbed()
.setTitle('Bot Stats')
.setColor('#000000')
.addFields(
{
name: ' Servers',
value: `Serving ${client.guilds.cache.size} servers.`,
inline: true
},
{
name: ' Channels',
value: `Serving ${client.channels.cache.size} channels.`,
inline: true
},
{
name: ' Server Users',
value: `Serving ${client.users.cache.size}`,
inline: true
}
)
await message.channel.send(embed)
}
module.exports = {
name: 'bot-info',
description: 'bot information',
execute
}
解决方案
您可以尝试为您的代码调整这个简单的解决方案,这对我有用,或者提供更多详细信息,为什么要使用异步功能。
const Discord = require('discord.js')
const client = new Discord.Client()
client.on("message", async message => {
if(message.content === "&bot-info") {
const embed = new Discord.MessageEmbed()
.setTitle('Bot Stats')
.setColor('#000000')
.addFields(
{
name: ' Servers',
value: `Serving ${client.guilds.cache.size} servers.`,
inline: true
},
{
name: ' Channels',
value: `Serving ${client.channels.cache.size} channels.`,
inline: true
},
{
name: ' Server Users',
value: `Serving ${client.users.cache.size}`,
inline: true
}
)
message.channel.send(embed)
}
})
推荐阅读
- python - 如何在 python 中保持文件对象打开并继续写入而无需调用 file.close()
- asp.net-core - 如何在 Asp.Net Core 中间件中缓存响应?
- python - Groupby 分类变量
- flutter - 如何在颤动中绘制一个一侧为半圆的矩形?
- azure-devops - Azure Pipelines 部署阶段失败且没有错误
- java - 在 Spring App 中将 MockitoMVC 与 Junit 一起使用时出现 Stackoverflow 错误
- typescript - 处理丢失的对象键
- python - Django中相关模型的有效计数
- reactjs - 如何滚动到 React 中的功能组件?
- godot - 使用自定义构建模板导出时 Godot 引擎崩溃