Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Client, GatewayIntentBits } from "discord.js";
import { config } from "./env.js";
import { loadCommands, registerCommands } from "./utils/commands.js";
import { loadEvents } from "./utils/events.js";

const client = new Client({
Expand All @@ -12,9 +11,7 @@ const client = new Client({
GatewayIntentBits.GuildMembers,
});

loadCommands(client);
loadEvents(client);
registerCommands();

// Graceful shutdown
process.on("SIGINT", async () => {
Expand Down
45 changes: 1 addition & 44 deletions src/utils/commands.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
import {
type ChatInputCommandInteraction,
type Client,
Collection,
REST,
Routes,
} from "discord.js";
import { commands } from "../commands/index.js";
import type { Command } from "../commands/types.js";
import { config } from "../env.js";
import type { ChatInputCommandInteraction } from "discord.js";

export const buildCommandString = (interaction: ChatInputCommandInteraction): string => {
const commandName = interaction.commandName;
return `/${commandName} ${interaction.options.data
.map((option) => `${option.name}:${option.value}`)
.join(" ")}`;
};

export async function loadCommands(client: Client) {
if (!client.commands) {
client.commands = new Collection<string, Command>();
}

let loaded = 0;
for (const command of commands.values()) {
client.commands.set(command.data.name, command);
loaded += 1;
}

console.log(`Successfully loaded ${loaded} command handlers into client.commands.`);
}

export async function registerCommands() {
const commandsData = Array.from(commands.values()).map((command) => command.data);

try {
const guildId = config.discord.serverId;
const scope = guildId ? `guild ${guildId}` : "global";
console.log(`Started refreshing ${commandsData.length} ${scope} application commands.`);

const rest = new REST({ version: "10" }).setToken(config.discord.token);

await rest.put(Routes.applicationGuildCommands(config.discord.clientId, guildId), {
body: commandsData,
});

console.log(`Successfully reloaded ${commandsData.length} ${scope} commands.`);
} catch (error) {
console.error(JSON.stringify(error, null, 2));
}
}
3 changes: 0 additions & 3 deletions src/utils/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ export async function deployCommands(): Promise<void> {

const rest = new REST({ version: "10" }).setToken(config.discord.token);

(await rest.put(Routes.applicationCommands(config.discord.clientId), {
body: [],
})) as RESTPutAPIApplicationCommandsResult;
try {
const result = (await rest.put(
Routes.applicationGuildCommands(config.discord.clientId, config.discord.serverId),
Expand Down