feat: implement command registration for booster role bot and add tests
This commit is contained in:
@@ -53,6 +53,8 @@ SQLite default tersimpan di `./data/booster-role.sqlite`.
|
|||||||
bun run dev
|
bun run dev
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Saat startup, bot otomatis register slash command ke guild dari `DISCORD_GUILD_ID`. Pastikan bot di-invite dengan scope `applications.commands`.
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { describe, expect, test } from "bun:test";
|
||||||
|
import { registerGuildCommands, type DiscordRestClient } from "./registerCommands";
|
||||||
|
|
||||||
|
class FakeRestClient implements DiscordRestClient {
|
||||||
|
route: string | null = null;
|
||||||
|
body: unknown = null;
|
||||||
|
|
||||||
|
async put(route: string, input: { body: unknown }): Promise<unknown> {
|
||||||
|
this.route = route;
|
||||||
|
this.body = input.body;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("registerGuildCommands", () => {
|
||||||
|
test("registers booster role command to the configured guild", async () => {
|
||||||
|
const rest = new FakeRestClient();
|
||||||
|
|
||||||
|
await registerGuildCommands(rest, { clientId: "client", guildId: "guild" });
|
||||||
|
|
||||||
|
expect(rest.route).toBe("/applications/client/guilds/guild/commands");
|
||||||
|
expect(rest.body).toEqual([
|
||||||
|
expect.objectContaining({ name: "booster-role", description: "Manage your cosmetic booster role" })
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { REST, Routes } from "discord.js";
|
||||||
|
import { boosterRoleCommand } from "./commands/booster-role";
|
||||||
|
|
||||||
|
export type DiscordRestClient = {
|
||||||
|
put(route: string, input: { body: unknown }): Promise<unknown>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RegisterCommandsConfig = {
|
||||||
|
clientId: string;
|
||||||
|
guildId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function registerGuildCommands(rest: DiscordRestClient, config: RegisterCommandsConfig): Promise<void> {
|
||||||
|
await rest.put(Routes.applicationGuildCommands(config.clientId, config.guildId), {
|
||||||
|
body: [boosterRoleCommand.toJSON()]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function registerGuildCommandsWithToken(token: string, config: RegisterCommandsConfig): Promise<void> {
|
||||||
|
const rest = new REST({ version: "10" }).setToken(token);
|
||||||
|
await registerGuildCommands(rest, config);
|
||||||
|
}
|
||||||
+7
-1
@@ -1,10 +1,16 @@
|
|||||||
import { loadConfig } from "./config";
|
import { loadConfig } from "./config";
|
||||||
import { createDiscordClient } from "./discord/client";
|
import { createDiscordClient } from "./discord/client";
|
||||||
|
import { registerGuildCommandsWithToken } from "./discord/registerCommands";
|
||||||
|
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
await registerGuildCommandsWithToken(config.discordToken, {
|
||||||
|
clientId: config.discordClientId,
|
||||||
|
guildId: config.discordGuildId
|
||||||
|
});
|
||||||
|
|
||||||
const client = createDiscordClient();
|
const client = createDiscordClient();
|
||||||
|
|
||||||
client.once("ready", () => {
|
client.once("clientReady", () => {
|
||||||
console.log(`Logged in as ${client.user?.tag ?? "unknown bot"}`);
|
console.log(`Logged in as ${client.user?.tag ?? "unknown bot"}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user