From 49d6b51de65572f2392556d67db73c159db0023a Mon Sep 17 00:00:00 2001 From: grngxd <36968271+grngxd@users.noreply.github.com> Date: Thu, 31 Jul 2025 19:25:31 +0100 Subject: [PATCH] refactor: reorganize project structure and improve user command handling --- cmd/index.ts | 7 +++++++ cmd/register.ts | 18 ++++++++++++++++++ cmd/user.ts | 27 +++++++++++++++++++++------ db.ts => db/db.ts | 0 index.ts | 38 ++++++++++++++++++-------------------- lib.ts => lib/files.ts | 0 types.ts => types/files.ts | 0 7 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 cmd/index.ts create mode 100644 cmd/register.ts rename db.ts => db/db.ts (100%) rename lib.ts => lib/files.ts (100%) rename types.ts => types/files.ts (100%) diff --git a/cmd/index.ts b/cmd/index.ts new file mode 100644 index 0000000..a703f5d --- /dev/null +++ b/cmd/index.ts @@ -0,0 +1,7 @@ +import register from "./register"; +import user from "./user"; + +export default [ + user, + register +] \ No newline at end of file diff --git a/cmd/register.ts b/cmd/register.ts new file mode 100644 index 0000000..a72d90c --- /dev/null +++ b/cmd/register.ts @@ -0,0 +1,18 @@ +import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; +import { db } from "../db/db"; + +export const data = new SlashCommandBuilder() + .setName("register") + .setDescription("create your stereo account"); + +export const execute = async (interaction: ChatInputCommandInteraction) => { + const existingUser = await db.get<{ id: string }>`SELECT id FROM users WHERE id = ${interaction.user.id}`; + if (existingUser) { + await interaction.reply({ content: "You already have a stereo account.", ephemeral: true }); + return; + } + + await interaction.reply({ content: "Your stereo account has been created!", ephemeral: true }); +} + +export default [data, execute] as const; \ No newline at end of file diff --git a/cmd/user.ts b/cmd/user.ts index 5714dcc..20f0a63 100644 --- a/cmd/user.ts +++ b/cmd/user.ts @@ -1,16 +1,26 @@ // commands/overview.ts import { ChatInputCommandInteraction, EmbedBuilder, MessageFlags, SlashCommandBuilder } from "discord.js"; -import { db } from "../db"; -import { formatSize } from "../lib"; -import { StereoFile } from "../types"; +import { db } from "../db/db"; +import { formatSize } from "../lib/files"; +import { StereoFile } from "../types/files"; export const data = new SlashCommandBuilder() - .setName("user") - .addUserOption(option => option.setName("user").setDescription("user to look up").setRequired(false)) - .setDescription("get information about a user (or yourself) on stereo"); + .setName("user") + .addUserOption(option => option.setName("user").setDescription("user to look up").setRequired(false)) + .setDescription("get information about a user (or yourself) on stereo"); export const execute = async (interaction: ChatInputCommandInteraction) => { const user = interaction.options.getUser("user") || interaction.user; + if (user.id === interaction.client.user?.id) { + await interaction.reply({ content: "hey! stop that!!!!", flags: MessageFlags.Ephemeral }); + return; + } + + if (user.bot) { + await interaction.reply({ content: "bots can't have stereo accounts...", flags: MessageFlags.Ephemeral }); + return; + } + if (!user) { await interaction.reply({ content: "couldn't find user", flags: MessageFlags.Ephemeral }); return; @@ -18,6 +28,11 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { const id = await db.get<{ id: string }>`SELECT id FROM users WHERE id = ${user.id}`; if (!id) { + if (user.id === interaction.user.id) { + await interaction.reply({ content: "you don't have a stereo account yet, use the `/register` command to make one", flags: MessageFlags.Ephemeral }); + return; + } + await interaction.reply({ content: "this user doesn't have a stereo account", flags: MessageFlags.Ephemeral }); return; } diff --git a/db.ts b/db/db.ts similarity index 100% rename from db.ts rename to db/db.ts diff --git a/index.ts b/index.ts index 5822beb..d66a3df 100644 --- a/index.ts +++ b/index.ts @@ -1,22 +1,23 @@ import { ActivityType, Client, Events, GatewayIntentBits, REST, Routes } from "discord.js"; -import user from "./cmd/user"; -import { db } from "./db"; -import { StereoFile } from "./types"; +import commands from "./cmd"; +import { db } from "./db/db"; +import { StereoFile } from "./types/files"; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, - ] - }); -const rest = new REST({ version: "10" }).setToken(process.env.TOKEN); +}); + +const rest = new REST({ version: "10" }) + .setToken(process.env.TOKEN); client.once(Events.ClientReady, async (c) => { await rest.put( Routes.applicationCommands(process.env.CLIENT_ID), - { body: commands.map(([data]) => data) } + { body: commands.map(([data, _]) => data) } ); const files = (await db.all`SELECT * FROM files`).length @@ -28,20 +29,17 @@ client.once(Events.ClientReady, async (c) => { console.log(c.user.tag); }); -const commands = [ - user -] - client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isChatInputCommand()) return; - const cmd = commands.find(([data]) => data.name === interaction.commandName); - if (!cmd) return; - try { - await cmd[1](interaction); - } catch (err) { - console.error(err); - await interaction.reply({ content: "there was an error executing this command", ephemeral: true }); - } + if (!interaction.isChatInputCommand()) return; + const cmd = commands.find(([data]) => data.name === interaction.commandName); + if (!cmd) return; + + try { + await cmd[1](interaction); + } catch (err) { + console.error(err); + await interaction.reply({ content: "there was an error executing this command", ephemeral: true }); + } }); client.login(process.env.TOKEN).catch((err) => { diff --git a/lib.ts b/lib/files.ts similarity index 100% rename from lib.ts rename to lib/files.ts diff --git a/types.ts b/types/files.ts similarity index 100% rename from types.ts rename to types/files.ts