Skip to content

Commit 99082ea

Browse files
committed
made guildid optional
1 parent c1bb98e commit 99082ea

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ for (const file of eventFiles) {
5757
logger.startup(`Loaded event ${file.replace(/\.[jt]s$/, '')}`);
5858
}
5959

60-
await loadCommands(client, config.clientId, config.guildId, config.botToken)
60+
await loadCommands(client, config.clientId, config.botToken, config.guildId ?? undefined)
6161

6262
try {
6363
client.login(config.botToken);

src/libs/loadCommands.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import {
1010
import * as fs from "fs";
1111
import * as path from "path";
1212
import { fileURLToPath, pathToFileURL } from "url";
13+
import { logger } from "./logger.js";
1314

1415
export interface Command {
1516
data:
16-
| SlashCommandBuilder
17-
| SlashCommandSubcommandsOnlyBuilder
18-
| Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
17+
| SlashCommandBuilder
18+
| SlashCommandSubcommandsOnlyBuilder
19+
| Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">;
1920
execute: (interaction: ChatInputCommandInteraction) => Promise<void>;
2021
}
2122

@@ -24,8 +25,8 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url));
2425
export async function loadCommands(
2526
client: Client & { commands?: Collection<string, Command> },
2627
clientId: string,
27-
guildId: string,
28-
token: string
28+
token: string,
29+
guildId?: string,
2930
): Promise<void> {
3031
client.commands = new Collection<string, Command>();
3132

@@ -48,11 +49,23 @@ export async function loadCommands(
4849

4950
const rest = new REST().setToken(token);
5051

51-
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
52-
body: [],
53-
});
52+
if (clientId && guildId) {
53+
logger.info("here")
54+
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
55+
body: [],
56+
});
5457

55-
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
56-
body: commandData,
57-
});
58+
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
59+
body: commandData,
60+
});
61+
} else if (clientId) {
62+
logger.info("a")
63+
await rest.put(Routes.applicationCommands(clientId), {
64+
body: [],
65+
});
66+
67+
await rest.put(Routes.applicationCommands(clientId), {
68+
body: commandData,
69+
});
70+
}
5871
}

src/libs/loadVariables.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { logger } from "./logger.js";
33
export interface Config {
44
botToken: string;
55
clientId: string;
6-
guildId: string;
6+
guildId: string | undefined;
77
greetChannelId: string;
88
logChannelId: string;
99
databaseUrl: string;
1010
}
1111

1212
function requireEnv(key: string): string {
1313
const value = process.env[key];
14+
console.log("here")
1415
if (!value) {
1516
logger.fatal(`Missing required environment variable: ${key}`);
1617
}
@@ -21,7 +22,7 @@ export function loadVariables(): Config {
2122
return {
2223
botToken: requireEnv("BOT_TOKEN"),
2324
clientId: requireEnv("CLIENT_ID"),
24-
guildId: requireEnv("GUILD_ID"),
25+
guildId: process.env.GUILD_ID?.toString() ?? undefined,
2526
greetChannelId: requireEnv("GREET_CHANNEL_ID"),
2627
logChannelId: requireEnv("LOG_CHANNEL_ID"),
2728
databaseUrl: requireEnv("DATABASE_URL"),

0 commit comments

Comments
 (0)