kin.ts
kin.ts5mo ago

follow up action after respond to the request

Currently, i have this for my http interaction discord bot
// ping.js
execute(api: API, interaction: APIPingInteraction): Response {
something(api, interaction);
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}

async function something(api: API, interaction: APIInteraction): Promise<void> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
}
// ping.js
execute(api: API, interaction: APIPingInteraction): Response {
something(api, interaction);
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}

async function something(api: API, interaction: APIInteraction): Promise<void> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
}
is there a way call interactions.editReply in the execute function without creating another function like this? previously i used Deno.serveHttp() to respond to the request, but because its will be deprecated, i moved to Deno.serve
10 Replies
kin.ts
kin.ts5mo ago
@Deno AI Helper
Deno AI Helper
Deno AI Helper5mo ago
Hello! I see that you have a question, but you haven't provided the details of your question yet. If you have a specific question about Deno or a piece of code you're working on, please share it with me, and I'll do my best to assist you. If your question is related to Deno's features, APIs, or best practices, I can certainly help clarify those based on the information available up to my knowledge cutoff date. However, if your question is very specific and I can't find the answer in the official Deno documentation, I'll let you know. Please provide the details of your question or the code you're having trouble with, and I'll take a look! Remember, any advice I give that isn't directly from the Deno documentation should be double-checked for accuracy.
kin.ts
kin.ts5mo ago
Currently, i have this for my http interaction discord bot
// ping.js
execute(api: API, interaction: APIPingInteraction): Response {
something(api, interaction);
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}

async function something(api: API, interaction: APIInteraction): Promise<void> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
}
// ping.js
execute(api: API, interaction: APIPingInteraction): Response {
something(api, interaction);
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}

async function something(api: API, interaction: APIInteraction): Promise<void> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
}
is there a way call interactions.editReply in the execute function without creating another function like this? previously i used Deno.serveHttp() to respond to the request, but because its will be deprecated, i moved to Deno.serve
Deno AI Helper
Deno AI Helper5mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, it seems like you're trying to streamline your function calls for handling Discord interactions. If you want to call interactions.editReply directly within the execute function without creating a separate function like something, you could do so by making the execute function async and then using await to call interactions.editReply directly within it. Here's a modified version of your code:
// ping.js
async execute(api: API, interaction: APIPingInteraction): Promise<Response> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}
// ping.js
async execute(api: API, interaction: APIPingInteraction): Promise<Response> {
await api.interactions.editReply(interaction.application_id, interaction.token, { content: `${Date.now() - generateTimestamp(interaction)}ms` });
return Response.json({ type: InteractionResponseType.ChannelMessageWithSource, data: { content: "pong" } });
}
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice. Make sure that the API and APIPingInteraction types are correctly defined to support the operations you're performing, and that generateTimestamp is a function that returns the timestamp from the interaction object. Remember to handle any potential exceptions that might occur during the asynchronous operation, and ensure that your bot's logic aligns with Discord's API requirements and rate limits.
kin.ts
kin.ts5mo ago
the problem is i have to respond to the request before editing the interaction response
Deno AI Helper
Deno AI Helper5mo ago
I see, if you need to respond to the request before editing the interaction response, you can do so by sending an initial response and then using the editReply method to update that response. Here's how you could modify your execute function to do this:
// ping.js
async execute(api: API, interaction: APIPingInteraction): Promise<Response> {
// Send an initial response
const initialResponse = Response.json({
type: InteractionResponseType.ChannelMessageWithSource,
data: { content: "Calculating ping..." },
});

// Edit the reply after sending the initial response
setTimeout(async () => {
await api.interactions.editReply(interaction.application_id, interaction.token, {
content: `${Date.now() - generateTimestamp(interaction)}ms`,
});
}, 0); // setTimeout with 0 to defer the execution until after the initial response is sent

return initialResponse;
}
// ping.js
async execute(api: API, interaction: APIPingInteraction): Promise<Response> {
// Send an initial response
const initialResponse = Response.json({
type: InteractionResponseType.ChannelMessageWithSource,
data: { content: "Calculating ping..." },
});

// Edit the reply after sending the initial response
setTimeout(async () => {
await api.interactions.editReply(interaction.application_id, interaction.token, {
content: `${Date.now() - generateTimestamp(interaction)}ms`,
});
}, 0); // setTimeout with 0 to defer the execution until after the initial response is sent

return initialResponse;
}
In this example, I'm using setTimeout with a delay of 0 to defer the execution of the editReply method until after the initial response is sent. This is a common JavaScript technique to allow time for the event loop to process the initial response before executing subsequent code. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice. Make sure that the API and APIPingInteraction types are correctly defined to support the operations you're performing, and that generateTimestamp is a function that returns the timestamp from the interaction object. Also, be aware that using setTimeout in this way is a workaround and may not be the best practice for all situations. It's important to test this thoroughly to ensure it works as expected in your application and within the constraints of Discord's
Leokuma
Leokuma5mo ago
Alternatively to setTimeout, you could use queueMicrotask
cknight
cknight5mo ago
Or use Deno queues?
kin.ts
kin.ts5mo ago
thank you @Leokuma, @cknight . I'll take your advice