简体   繁体   English

如何从 Discord.js 中的不同命令更改嵌入描述

[英]How do I change the Embeds Description from a different command in Discord.js

So I have been coding my own Discord bot for a while now and I want to make a command that changes an embed from another command.所以我一直在编写我自己的 Discord 机器人一段时间,我想做一个命令来改变另一个命令的嵌入。 I have a command handler.我有一个命令处理程序。

When I do -autostatus my Discord bot will post an embed with latency, API latency, uptime, and status information on whether the bot is functioning fine.当我执行-autostatus时,我的 Discord 机器人将发布带有延迟、API 延迟、正常运行时间和有关机器人是否正常运行的状态信息的嵌入。

I have got the "functioning fine" and uptime inside the.setDescription so then I can edit it from another command.我在 .setDescription 中有“运行良好”和正常运行时间,所以我可以从另一个命令编辑它。 But when I do -botoff , I get the following error:但是当我做-botoff时,我收到以下错误:

embed.setDescription("Uptime: Offline Bot Status: Offline")

TypeError: Cannot read property of 'setDescription' of undefined

Here is my -botoff command:这是我的-botoff命令:

const Discord = require("discord.js")
const { embed } = require("./autostatus.js")
exports.run = (client, message, args) => {
   if(message.author.id != "276249983664128001") return;
   embed.setDescription("Uptime: Offline Bot Status: Offline")
}

My autostatus.js can be found here: https://hastebin.com/pezinewiqi.js我的autostatus.js可以在这里找到: https://hastebin.com/pezinewiqi.js

Thanks so much if you can help!如果您能提供帮助,非常感谢!

You are getting this error because const { embed } = require("./autostatus.js") is undefined.您收到此错误是因为const { embed } = require("./autostatus.js")未定义。 This is because you are not exporting embed in autostatus.js.这是因为您没有在 autostatus.js 中导出嵌入。 I checked your hastebin and found我检查了你的 hastebin 并发现

let embed = new Discord.MessageEmbed()
        .setTitle("My Status!")
        .setDescription(`**Uptime:** \n \`${uptime}\` \n **Bot Status:** \`✅ Operational\``)
        .addField("Latency:", `\`${ping}\``)
        .addField("API Latency:", `\`${Math.round(client.ws.ping)}\``)
        .setColor(client.config.color)
        .setFooter(`${client.config.footer} | Last updated`)
        .setTimestamp()

Here by using let you are just creating a local variable.在这里,使用let您只是创建了一个局部变量。 This can't be required from other files.这不能从其他文件中要求。

You need to save the embed message ID in a global variable of your bot and then, in your other command, fetch the message by its ID and edit the content of the fetched message.您需要将嵌入消息 ID 保存在机器人的全局变量中,然后在您的其他命令中按其 ID 获取消息并编辑获取的消息的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM