General structure works

This commit is contained in:
Bradley Bickford 2025-06-22 21:10:15 -04:00
parent de638f3e98
commit b307630642
7 changed files with 105 additions and 5 deletions

View File

@ -3,7 +3,10 @@
"version": "1.0.0", "version": "1.0.0",
"main": "breadbot.ts", "main": "breadbot.ts",
"scripts": { "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": "", "author": "",
"license": "ISC", "license": "ISC",
@ -16,8 +19,5 @@
"tsup": "^8.5.0", "tsup": "^8.5.0",
"tsx": "^4.20.3", "tsx": "^4.20.3",
"typescript": "^5.8.3" "typescript": "^5.8.3"
}, }
"dev": "tsx watch src/index.ts",
"start": "node dist/index.js",
"build": "tsup src/index.ts --minify"
} }

View File

@ -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)

5
src/commands/index.ts Normal file
View File

@ -0,0 +1,5 @@
import * as ping from "./ping";
export const commands = {
ping
}

11
src/commands/ping.ts Normal file
View File

@ -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!")
}

14
src/config.ts Normal file
View File

@ -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
}

View File

@ -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)
}
}

5
src/utilties/index.ts Normal file
View File

@ -0,0 +1,5 @@
import * as command_utils from "./discord/command_utils"
export const utilities = {
command_utils
}