Skip to content

Commit

Permalink
[fix] fixing discord get command api
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Feb 28, 2024
1 parent 20c47ae commit 0d16875
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DiscordImage, MJConfig } from "./interfaces";

import async from "async";
import { sleep } from "./utils";
export const Commands = [
"ask",
"blend",
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Command {
include_applications: "true",
});
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${this.config.ServerId}/application-command-index`;
const response = await this.config.fetch(url, {
const response = await this.safeFetch(url, {
headers: { authorization: this.config.SalaiToken },
});

Expand All @@ -65,15 +66,8 @@ export class Command {
}

async getCommand(name: CommandName) {
const searchParams = new URLSearchParams({
type: "1",
query: name,
limit: "1",
include_applications: "true",
// command_ids: `${this.config.BotId}`,
});
const url = `${this.config.DiscordBaseUrl}/api/v9/channels/${this.config.ChannelId}/application-commands/search?${searchParams}`;
const response = await this.config.fetch(url, {
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${this.config.ServerId}/application-command-index`;
const response = await this.safeFetch(url, {
headers: { authorization: this.config.SalaiToken },
});
const data = await response.json();
Expand All @@ -82,6 +76,39 @@ export class Command {
}
throw new Error(`Failed to get application_commands for command ${name}`);
}
private safeFetch(input: RequestInfo | URL, init?: RequestInit | undefined) {
const request = this.config.fetch.bind(this, input, init);
return new Promise<Response>((resolve, reject) => {
this.fetchQueue.push(
{
request,
callback: (res: Response) => {
resolve(res);
},
},
(error: any, result: any) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
});
}
private async processFetchRequest({
request,
callback,
}: {
request: () => Promise<Response>;
callback: (res: Response) => void;
}) {
const res = await request();
callback(res);
await sleep(1000 * 4);
}
private fetchQueue = async.queue(this.processFetchRequest, 1);

async imaginePayload(prompt: string, nonce?: string) {
const data = await this.commandData("imagine", [
{
Expand Down

0 comments on commit 0d16875

Please sign in to comment.