Skip to content

Commit

Permalink
feat: update application-command-index
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisheng Lu committed Jan 3, 2024
1 parent 1222bf6 commit 0cdbfcd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
4. 现在可以运行示例了

```bash
npx tsx example/imagine-ws.ts
npx tsx example/test.ts
```

或者
Expand Down
26 changes: 17 additions & 9 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function getCommandName(name: string): CommandName | undefined {
export class Command {
constructor(public config: MJConfig) {}
cache: Partial<Record<CommandName, Command>> = {};

application_commands_res:any;
async cacheCommand(name: CommandName) {
if (this.cache[name] !== undefined) {
return this.cache[name];
Expand Down Expand Up @@ -69,21 +69,28 @@ 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, {
headers: { authorization: this.config.SalaiToken },
});
const data = await response.json();
if ("application_commands" in data) {
const application_commands = data.application_commands;
if(!this.application_commands_res){
const url = `${this.config.DiscordBaseUrl}/api/v9/channels/${this.config.ChannelId}/application-command-index`;
const response = await this.config.fetch(url, {
headers: { authorization: this.config.SalaiToken },
});
const data = await response.json();
// console.log('data getCommand',data)
this.application_commands_res=data
}
// const url = `${this.config.DiscordBaseUrl}/api/v9/channels/${this.config.ChannelId}/application-commands/search?${searchParams}`;
if ("application_commands" in this.application_commands_res) {
const application_commands = this.application_commands_res.application_commands;
let filterArr=application_commands.filter((item:any)=>item.name===name);
// console.log('filterArr',filterArr)
if (filterArr[0]) {
// console.log(
// `got ${name} application_commands`,
Expand All @@ -92,11 +99,12 @@ export class Command {
return filterArr[0]
}
if(application_commands[0]){
// console.log('application_commands[0]',application_commands[0])
return application_commands[0]
}
}
console.log(`Failed to get application_commands for command ${name}`,this.config.ChannelId);
return data;
return this.application_commands_res;
}
async imaginePayload(prompt: string, nonce?: string) {
const data = await this.commandData("imagine", [
Expand Down
1 change: 0 additions & 1 deletion src/midjourney.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export class MidjourneyApi extends Command {
}

if (response.status >= 400) {
console.log('response',response)
console.error("api.error.config", {
payload: JSON.stringify(payload),
config: this.config,
Expand Down

0 comments on commit 0cdbfcd

Please sign in to comment.