eScord.js ping延迟从slash命令嵌入

发布于 2025-02-12 07:47:35 字数 1149 浏览 1 评论 0 原文

新手在这里。我正在尝试获得一个斜杠命令,以发送嵌入,并在初始消息和消息响应时间之间花费的时间。我得到 typeError:无法读取未定义的属性(读取'CreateTimestamp') error [Interaction_already_replied]:对此交互的答复已经发送或推迟。我跳来跳去查看其他代码,试图找到一种方法来制作这项工作,但是Slash Command的处理对我来说仍然没有很多意义。我的代码块在下面。我跟随 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] });
        })
    },
};

Newbie here. I am trying to get a slash command to send an embed with the amount of time it took between the initial message and the message response time. I am getting TypeError: Cannot read properties of undefined (reading 'createdTimestamp') and Error [INTERACTION_ALREADY_REPLIED]: The reply to this interaction has already been sent or deferred. I've jumped around looking at others code trying to find a way to make this work but slash command handling still doesn't make a lot of sense to me. My code block is below. I followed along with https://discordjs.guide so if you have any other suggestions with structure feel free to comment them below. Thank you!

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] });
        })
    },
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2025-02-19 07:47:35

首先,您需要获取发送的答复,您可以使用获得互动答复。您可以推迟回复,然后使用CreateTimestamp,而不是使用“ Pinging Bot ...”回复。 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)} minutes\n:sparkling_heart: Websocket heartbeat: ${interaction.client.ws.ping}ms.\n:round_pushpin: Rountrip Latency: ${sent.createdTimestamp - interaction.createdTimestamp}ms`);
    },
};

您可以将响应自定义为嵌入或随心所欲。 DJ指南在ping命令在这里

first you need to fetch the reply you send, u can use fetchReply to get the interaction reply. instead of replying with "Pinging bot..." you can defer the reply and then use the createdTimestamp. A basic example of a ping command would be

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)} minutes\n:sparkling_heart: Websocket heartbeat: ${interaction.client.ws.ping}ms.\n:round_pushpin: Rountrip Latency: ${sent.createdTimestamp - interaction.createdTimestamp}ms`);
    },
};

You can customize the response into an embed or however you like. The djs guide has a section on ping command here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文