Skip to content

Commit

Permalink
Refactor command retrieval logic and fix server ID fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Feb 28, 2024
1 parent 0d16875 commit 2f74423
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
20 changes: 10 additions & 10 deletions example/imagine-dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ async function main() {
Ws: true,
});
await client.Connect();
const info = await client.Info();
console.log(info);
return
// const msg = await client.Imagine(
// "A little white dog",
// (uri: string, progress: string) => {
// console.log("loading", uri, "progress", progress);
// }
// );
// console.log({ msg });
// const info = await client.Info();
// console.log(info);
// return
const msg = await client.Imagine(
"A little white dog",
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log({ msg });
}
main()
.then(() => {
Expand Down
25 changes: 14 additions & 11 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ export class Command {
if (this.cache[name] !== undefined) {
return this.cache[name];
}
if (this.config.ServerId) {
const command = await this.getCommand(name);
this.cache[name] = command;
return command;
}
const command = await this.getCommand(name);
console.log("=========", { command });
this.cache[name] = command;
return command;
this.allCommand();
return this.cache[name];
}
async allCommand() {
const searchParams = new URLSearchParams({
type: "1",
include_applications: "true",
});
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${this.config.ServerId}/application-command-index`;
let serverId = this.config.ServerId;
if (!serverId) {
serverId = this.config.ChannelId;
}
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${serverId}/application-command-index`;
const response = await this.safeFetch(url, {
headers: { authorization: this.config.SalaiToken },
});
Expand All @@ -66,7 +65,11 @@ export class Command {
}

async getCommand(name: CommandName) {
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${this.config.ServerId}/application-command-index`;
let serverId = this.config.ServerId;
if (!serverId) {
serverId = this.config.ChannelId;
}
const url = `${this.config.DiscordBaseUrl}/api/v9/guilds/${serverId}/application-command-index`;
const response = await this.safeFetch(url, {
headers: { authorization: this.config.SalaiToken },
});
Expand Down

0 comments on commit 2f74423

Please sign in to comment.