Skip to content

Commit

Permalink
Support register commands only on guilds
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 30, 2023
1 parent 441771a commit 269872e
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/core/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ export type RegisterConfig = {
* default: true
*/
enabled?: boolean;

/**
* Register commands on guild
*/
guilds?: string[];

/**
* Skip registering global commands
*
* default: false
*/
guildsOnly?: boolean;
};

/**
Expand All @@ -16,19 +28,37 @@ export async function registerCommands(
config: RegisterConfig | null | undefined,
context: LoadContext
) {
const application = context.client.application;
const register = {
enabled: config?.enabled ?? true,
guilds: config?.guilds ?? [],
guildsOnly: config?.guildsOnly ?? false,
};

if (register.enabled === true) {
const application = context.client.application;
console.log("Registering commands...");
if (!register.enabled) {
console.log("Commands registration skipped");
return;
}

if (application == null) {
throw new Error("Client is not ready to register commands");
}

if (application == null)
throw new Error("Client is not ready to register commands");
console.log("Registering commands...");

if (!register.guildsOnly) {
await application.commands.set(context.commands);
} else {
console.log("Commands registration skipped");
console.log("Registered on global");
}

for (const guildId of register.guilds) {
const guild = await application.client.guilds.fetch(guildId);

if (guild == null) {
throw new Error(`Failed to find guild ${guildId}`);
}

await guild.commands.set(context.commands);
console.log(`Registered on guild ${guildId} (${guild.name})`);
}
}

0 comments on commit 269872e

Please sign in to comment.