Skip to content

Commit

Permalink
commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yuimarudev committed Jan 23, 2024
1 parent 6255783 commit 034739c
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "module",
"devDependencies": {
"@biomejs/biome": "1.5.2",
"@discordjs/builders": "^1.7.0",
"@discordjs/core": "^1.1.1",
"@discordjs/rest": "^2.2.0",
"@discordjs/voice": "^0.16.1",
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/commands/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
APIChatInputApplicationCommandInteraction,
APIInteraction,
InteractionType,
} from "@discordjs/core";

interface Unko extends APIChatInputApplicationCommandInteraction {
guild_id: string;
}

export function validate(i: APIInteraction): i is Unko {
return !!(i.type === InteractionType.ApplicationCommand && i.guild_id);
}
13 changes: 13 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
API,
APIInteraction,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from "@discordjs/core";
import Ping from "./ping.js";

export const commands: ICommand[] = [new Ping()];

export interface ICommand {
defition(): RESTPostAPIChatInputApplicationCommandsJSONBody;
run(api: API, i: APIInteraction): Promise<unknown>;
}
9 changes: 9 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { API } from "@discordjs/core";
import { commands } from "./index.js";

export async function initCommands(api: API) {
api.applicationCommands.bulkOverwriteGlobalCommands(
(await api.applications.getCurrent()).id,
commands.map((x) => x.defition()),
);
}
24 changes: 24 additions & 0 deletions src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import {
API,
APIInteraction,
MessageFlags,
RESTPostAPIChatInputApplicationCommandsJSONBody,
} from "@discordjs/core";
import { type ICommand } from "./index.js";

export default class Ping implements ICommand {
defition(): RESTPostAPIChatInputApplicationCommandsJSONBody {
return new SlashCommandBuilder()
.setName("ping")
.setDescription("Ping Pong")
.toJSON();
}

async run(api: API, i: APIInteraction): Promise<unknown> {
return await api.interactions.editReply(i.application_id, i.token, {
content: "Pong!",
flags: MessageFlags.Ephemeral,
});
}
}
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GatewayDispatchEvents, WithIntrinsicProps } from "@discordjs/core";
import interactionCreate from "./interactionCreate.js";
import ready from "./ready.js";

const handlers: {
Expand All @@ -7,6 +8,7 @@ const handlers: {
[key in GatewayDispatchEvents]?: (a: WithIntrinsicProps<any>) => unknown;
} = {
[GatewayDispatchEvents.Ready]: ready,
[GatewayDispatchEvents.InteractionCreate]: interactionCreate,
};

export default handlers;
38 changes: 38 additions & 0 deletions src/handlers/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
GatewayInteractionCreateDispatchData,
MessageFlags,
WithIntrinsicProps,
} from "@discordjs/core";
import { validate } from "../commands/helper.js";
import { commands } from "../commands/index.js";

export default async ({
api,
data,
}: WithIntrinsicProps<GatewayInteractionCreateDispatchData>) => {
console.log(0);
await api.interactions.defer(data.id, data.token);

if (!validate(data)) return false;

const command = commands.find((x) => x.defition().name === data.data.name);

if (!command)
return await api.interactions.followUp(data.application_id, data.token, {
content: "古いコマンドを参照しています.世界を削除します.",
flags: MessageFlags.Ephemeral,
});

try {
await command.run(api, data);
} catch (e) {
await api.interactions.followUp(data.application_id, data.token, {
content: `エラーです. \`\`\`${
e instanceof Error ? e.message : String(e)
}\`\`\``,
flags: MessageFlags.Ephemeral,
});
}

return true;
};
5 changes: 5 additions & 0 deletions src/handlers/ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { GatewayReadyDispatchData, WithIntrinsicProps } from "@discordjs/core";
import { initCommands } from "../commands/init.js";

export default async ({
api,
}: WithIntrinsicProps<GatewayReadyDispatchData>) => {
await initCommands(api);

console.log("ready!");

return true;
};

0 comments on commit 034739c

Please sign in to comment.