diff --git a/package.json b/package.json index 72ee079..e7affbe 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "version": "1.0.0", "main": "breadbot.ts", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "tsx watch src/breadbot.ts", + "start": "node dist/breadbot.js", + "build": "tsup src/breadbot.ts --minify" }, "author": "", "license": "ISC", @@ -16,8 +19,5 @@ "tsup": "^8.5.0", "tsx": "^4.20.3", "typescript": "^5.8.3" - }, - "dev": "tsx watch src/index.ts", - "start": "node dist/index.js", - "build": "tsup src/index.ts --minify" + } } diff --git a/src/breadbot.ts b/src/breadbot.ts index e69de29..1c83356 100644 --- a/src/breadbot.ts +++ b/src/breadbot.ts @@ -0,0 +1,33 @@ +import { Client, Events, GatewayIntentBits, Guild, Interaction } from "discord.js" +import { utilities } from "./utilties" +import { commands } from "./commands" +import { config } from "./config" + +const client : Client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.DirectMessages + ] +}) + +client.once(Events.ClientReady, () => { + // TODO Winston should handle this + console.log("Breadbot is ready") +}) + +client.on(Events.GuildCreate, async (guild : Guild) => { + await utilities.command_utils.deployCommands(guild.id) +}) + +client.on(Events.InteractionCreate, async (interaction: Interaction) => { + if (!interaction.isCommand()) { + return + } + + if (commands[interaction.commandName as keyof typeof commands]) { + commands[interaction.commandName as keyof typeof commands].execute(interaction) + } +}) + +client.login(config.DISCORD_TOKEN) \ No newline at end of file diff --git a/src/commands/index.ts b/src/commands/index.ts new file mode 100644 index 0000000..6333e84 --- /dev/null +++ b/src/commands/index.ts @@ -0,0 +1,5 @@ +import * as ping from "./ping"; + +export const commands = { + ping +} \ No newline at end of file diff --git a/src/commands/ping.ts b/src/commands/ping.ts new file mode 100644 index 0000000..9f445b3 --- /dev/null +++ b/src/commands/ping.ts @@ -0,0 +1,11 @@ +import { CommandInteraction, SlashCommandBuilder } from "discord.js" + +export const enabled : boolean = true + +export const data : SlashCommandBuilder = new SlashCommandBuilder() + .setName("ping") + .setDescription("Replies with Pong!") + +export async function execute(interaction: CommandInteraction) { + return interaction.reply("Pong!") +} \ No newline at end of file diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..4e3a002 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,14 @@ +import dotenv from "dotenv" + +dotenv.config() + +const { DISCORD_TOKEN, DISCORD_CLIENT_ID } = process.env + +if (!DISCORD_TOKEN || !DISCORD_CLIENT_ID) { + throw new Error("Missing environment variables") +} + +export const config = { + DISCORD_TOKEN, + DISCORD_CLIENT_ID +} \ No newline at end of file diff --git a/src/utilties/discord/command_utils.ts b/src/utilties/discord/command_utils.ts new file mode 100644 index 0000000..a26b854 --- /dev/null +++ b/src/utilties/discord/command_utils.ts @@ -0,0 +1,32 @@ +import { REST, Routes } from "discord.js" +import { config } from "../../config" +import { commands } from "../../commands" + +const commandsData = Object.values(commands) + .filter((command) => command.enabled) + .map((command) => command.data) + +const rest : REST = new REST({ version: "10" }).setToken(config.DISCORD_TOKEN) + +export async function deployCommands(guildId: string) { + try { + // TODO Winston should handle this + console.log(`Refreshing slash commands for ${guildId}`) + + await rest.put( + Routes.applicationGuildCommands( + config.DISCORD_CLIENT_ID, + guildId + ), + { + body: commandsData + } + ) + + // TODO Winston should handle this + console.log(`Successfully reloaded slash commands for ${guildId}`) + } catch (error) { + // TODO Winston should handle this + console.error(error) + } +} \ No newline at end of file diff --git a/src/utilties/index.ts b/src/utilties/index.ts new file mode 100644 index 0000000..f07e314 --- /dev/null +++ b/src/utilties/index.ts @@ -0,0 +1,5 @@ +import * as command_utils from "./discord/command_utils" + +export const utilities = { + command_utils +} \ No newline at end of file