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
A
Aduhmazbro58d ago
@Deno AI Helper
DAH
Deno AI Helper58d 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.
A
Aduhmazbro58d 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
DAH
Deno AI Helper58d 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.
A
Aduhmazbro58d ago
the problem is i have to respond to the request before editing the interaction response
DAH
Deno AI Helper58d 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
L
Leokuma57d ago
Alternatively to setTimeout, you could use queueMicrotask
C
cknight57d ago
Or use Deno queues?
A
Aduhmazbro55d ago
thank you @Leokuma, @cknight . I'll take your advice
More Posts
How to apply @deno-types on a dynamic import```ts // @deno-types="npm:@types/react-dom@18/client" const { hydrateRoot: hydrate } = (await impCache fresh deps on docker problemI have a brand new fresh project with only the default deps and when i try to put my app in a dockerDeno fmt with verbatimModuleSyntax?Currently Deno fmt sorts *verbatim imports* alphabetically, this is conflicting with the *Organize IHow to use private remote modules hosted on GitHub on Deno Deploy?According to [Private Modules and Repositories](https://docs.deno.com/runtime/manual/basics/modules/Encryption using Deno KV and OAuth?I'm creating a note-taking app using most of Deno's tools. I was thinking about encrypting user noteDeno is not seeing a projectIt seems deno is not seeing a project i am using. Project link: https://github.com/Swifter1243/ReMa`arch` and `platform` are empty strings in `node:process`👋 hello! I'm trying to use a node project that uses `arch` and `platform` inside `node:process`, whNPM imports fail on JSON requireWhen importing modules via `npm:` protocol, the embedded esbuild will throw the following error if tuse-case: web component development in denoMy use-case is quite simple, really. But I have a hard time finding the tooling to work with it. - Starting project errorsI have error in url of DenoConditional callback with deno_kv_oauthHello I am using deno_kv_oauth for my application and am trying to do the following: I want users tClear deno kv dbHow can I clear out the database? using `list` requires the keys to be known, but I just want to clehow to break console.log maximum characters limithi, im using usual `deno run file.ts` command and realized that there is a limit to number characternpm import : class is not found but it is well declared in node_moduleI try to use Tatum SDK in deno (https://github.com/tatumio/tatum-js). I pick up the easiest code exaInteractive Jupyter notebookHas anyone figured out how to do interactivity in a Jupyter notebook with Deno kernel in VSCode? LiRun all pending ticks synchronouslyCurrently it seems like `run_event_loop` is the only way to run all the pending ops that are schedulHow can I do server sent events on the client side in deno?I have a server api which is doing server sent events correctly, I can see it in curl doing what I wunable to connect to mongodb using deno inside containerFirst of all, I want to mention that when I run `deno task preview` on my local machine / laptop, itTC39 stage 3 decorators not working anymore in Deno DeployAs of Feb 15th, deployments to #deploy™ fail when using TC39 decorators. Despite the decision to suUse Deno permissions in your own CLIIf I create a CLI with Deno, can I somehow reuse or pass on the permissions? ```bash > mycli --allo