Discord.js Ping Latency Inside Embed from Slash Command



新手。我试图得到一个斜杠命令发送一个嵌入的时间与初始消息和消息响应时间之间的量。我得到了TypeError: Cannot read properties of undefined (reading 'createdTimestamp')Error [INTERACTION_ALREADY_REPLIED]: The reply to this interaction has already been sent or deferred。我跳来跳去看别人的代码,试图找到一种方法来实现这个工作,但斜杠命令处理对我来说仍然没有很大的意义。我的代码块在下面。我跟随https://discordjs.guide,所以如果你有任何其他关于结构的建议,请随时在下面评论。谢谢你!

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
await interaction.reply("Pinging bot...").then (async (msg) =>{
const exampleEmbed = new MessageEmbed()
.setColor('0x0000ff')
.setTitle('Pong! :ping_pong:')
.addField("Time taken: ", `${msg.createdTimestamp - message.createdTimestamp}`)
.setThumbnail("https://78.media.tumblr.com/be43242341a7be9d50bb2ff8965abf61/tumblr_o1ximcnp1I1qf84u9o1_500.gif")
interaction.editReply({ embeds: [exampleEmbed] });
})
},
};

首先您需要获取您发送的回复,您可以使用fetchReply来获取交互回复。而不是回复"ping bot…"您可以延迟回复,然后使用createdTimestamp。ping命令的一个基本示例是

const { SlashCommandBuilder } = require('@discordjs/builders');
module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!'),
async execute(interaction) {
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
await interaction.editReply(`:ping_pong: Pong!n:stopwatch: Uptime: ${Math.round(interaction.client.uptime / 60000)} minutesn:sparkling_heart: Websocket heartbeat: ${interaction.client.ws.ping}ms.n:round_pushpin: Rountrip Latency: ${sent.createdTimestamp - interaction.createdTimestamp}ms`);
},
};

你可以自定义响应嵌入或任何你喜欢的。dj指南中有一节是关于ping命令的

相关内容

  • 没有找到相关文章

最新更新