diff --git a/.env.example b/.env.example index 05906ba..3a994b4 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ TOKEN= API= +CLIENT_ID= DB_TYPE= DB_URL= \ No newline at end of file diff --git a/cmd/user.ts b/cmd/user.ts new file mode 100644 index 0000000..5714dcc --- /dev/null +++ b/cmd/user.ts @@ -0,0 +1,46 @@ +// commands/overview.ts +import { ChatInputCommandInteraction, EmbedBuilder, MessageFlags, SlashCommandBuilder } from "discord.js"; +import { db } from "../db"; +import { formatSize } from "../lib"; +import { StereoFile } from "../types"; + +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"); + +export const execute = async (interaction: ChatInputCommandInteraction) => { + const user = interaction.options.getUser("user") || interaction.user; + if (!user) { + await interaction.reply({ content: "couldn't find user", flags: MessageFlags.Ephemeral }); + return; + } + + const id = await db.get<{ id: string }>`SELECT id FROM users WHERE id = ${user.id}`; + if (!id) { + await interaction.reply({ content: "this user doesn't have a stereo account", flags: MessageFlags.Ephemeral }); + return; + } + + const files = await db.all`SELECT * FROM files WHERE owner = ${user.id}`; + const totalSize = files.reduce((a, b) => a + b.size, 0); + + const embed = new EmbedBuilder() + .setColor(0xff264e) + .setAuthor({ + name: `${user.globalName || "user"} on stereo`, + iconURL: user.avatarURL({ size: 512 }) || "" + }) + .setDescription("Here's your overview:") + .addFields( + { name: "Uploads", value: `${files.length} files`, inline: true }, + { name: "Uploaded", value: `${formatSize(totalSize)} / 15 GB`, inline: true }, + { name: "Plan", value: `Free`, inline: true } + ) + .setFooter({ text: "powered by stereo" }) + .setTimestamp(); + + await interaction.reply({ embeds: [embed] }); +} + +export default [data, execute] as const; \ No newline at end of file diff --git a/globals.d.ts b/globals.d.ts index 94cb302..b9d593c 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -1,7 +1,10 @@ declare module "bun" { interface Env { + // discord TOKEN: string; - API: string; + CLIENT_ID: string; + + // stereo DB_URL: string; DB_TYPE: "sqlite" | "postgres"; } diff --git a/index.ts b/index.ts index d784cc6..5822beb 100644 --- a/index.ts +++ b/index.ts @@ -1,46 +1,47 @@ -import { ActivityType, Client, EmbedBuilder, Events, GatewayIntentBits } from "discord.js"; +import { ActivityType, Client, Events, GatewayIntentBits, REST, Routes } from "discord.js"; +import user from "./cmd/user"; import { db } from "./db"; -import { formatSize } from "./lib"; import { StereoFile } from "./types"; -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, + + ] + }); +const rest = new REST({ version: "10" }).setToken(process.env.TOKEN); client.once(Events.ClientReady, async (c) => { - console.log(c.user.tag); + await rest.put( + Routes.applicationCommands(process.env.CLIENT_ID), + { body: commands.map(([data]) => data) } + ); + const files = (await db.all`SELECT * FROM files`).length c.user.setPresence({ activities: [{ name: `${files} files uploaded to stereo`, type: ActivityType.Custom }], status: "dnd" }); + + console.log(c.user.tag); }); -client.on(Events.MessageCreate, async (message) => { - if (message.author.bot) return; - if ( - client.user && - message.mentions.has(client.user) && - !message.mentions.everyone - ) { - let files = await db.all`SELECT * FROM files WHERE owner = ${message.author.id}`; - let totalSize = files.reduce((a, b) => a + b.size, 0); +const commands = [ + user +] - const embed = new EmbedBuilder() - .setColor(0xff264e) - .setAuthor({ - name: `${message.author.globalName || "user"} on stereo`, - iconURL: message.author.avatarURL({ size: 512 }) || "" - }) - .setDescription("here's your overview:") - .addFields( - { name: "Uploads", value: `${files.length} files`, inline: true }, - { name: "Uploaded", value: `${formatSize(totalSize)} / 15 GB`, inline: true }, - { name: "Plan", value: `Free`, inline: true } - ) - .setFooter({ text: "powered by stereo" }) - .setTimestamp(); - - await message.reply({ embeds: [embed] }); - } +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 }); + } }); client.login(process.env.TOKEN).catch((err) => {