Skip to content

Commit

Permalink
Slash Command のバグ修正 (#35)
Browse files Browse the repository at this point in the history
* fix: Fix buildCommandStr

* fix: Extract hardcode and Add gurad

* fix: Fix to remove old commands

* refactor: Remove unused class
  • Loading branch information
MikuroXina authored Aug 4, 2021
1 parent f939156 commit bc3e8d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 72 deletions.
63 changes: 0 additions & 63 deletions src/bot/skin/discord-command.ts

This file was deleted.

41 changes: 32 additions & 9 deletions src/bot/skin/interactions-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const prCommand: ApplicationCommand = {

const commands = [repositoryCommand, branchCommand, issueCommand, prCommand];

const GUILD_ID = "683939861539192860";

export type Handler = (message: Message) => Promise<void>;

export class InteractionsCommandReceiver {
Expand All @@ -67,16 +69,23 @@ export class InteractionsCommandReceiver {
return;
}
this.initialized = true;
const registrar = client.guilds.cache.get("683939861539192860")?.commands;
const registrar = client.guilds.cache.get(GUILD_ID)?.commands;
if (!registrar) {
return;
}
const oldCommands = await registrar.fetch();
await Promise.all(
[...oldCommands.values()].map((com) => registrar.delete(com)),
);
await Promise.all(commands.map((command) => registrar.create(command)));
});
client.on("interactionCreate", (interaction) => {
if (!interaction.isCommand()) {
return;
}
if (interaction.guildId !== GUILD_ID) {
return;
}
this.onCommand(interaction);
});
}
Expand Down Expand Up @@ -123,14 +132,28 @@ export class InteractionsCommandReceiver {

private buildCommandStr(interaction: CommandInteraction): string {
let commandStr = `/${interaction.commandName} `;
commandStr += interaction.options.data
.filter(({ name }) => name !== "branch")
.map(({ value }) => value)
.join("/");
commandStr += interaction.options.data
.filter(({ name }) => name === "branch")
.map(({ value }) => value)
.join(" ");
const [orgArg] = interaction.options.data.filter(
({ name }) => name === "org",
);
if (orgArg) {
commandStr += `${orgArg.value}/`;
}
const [repoArg] = interaction.options.data.filter(
({ name }) => name === "repo",
);
commandStr += repoArg.value;
const [issueArg] = interaction.options.data.filter(
({ name }) => name === "issue",
);
if (issueArg) {
commandStr += `/${issueArg.value}`;
}
const [branchArg] = interaction.options.data.filter(
({ name }) => name === "branch",
);
if (branchArg) {
commandStr += ` ${branchArg.value}`;
}
return commandStr;
}
}

0 comments on commit bc3e8d3

Please sign in to comment.