discord-bot/cmd/user.ts

55 lines
No EOL
2.2 KiB
TypeScript

// commands/overview.ts
import { ChatInputCommandInteraction, MessageFlags, SlashCommandBuilder } from "discord.js";
import { db } from "../db/db";
import { createEmbed } from "../lib/embed";
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");
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;
}
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;
}
const files = await db.all<StereoFile[]>`SELECT * FROM files WHERE owner = ${user.id}`;
const totalSize = files.reduce((a, b) => a + b.size, 0);
const embed = createEmbed(user)
.setDescription("here's your account info:")
.addFields([
{ name: "uploads", value: `${files.length} files` },
{ name: "uploaded", value: `${formatSize(totalSize)} / 15 GB (${(totalSize / (15 * 1024 * 1024 * 1024)).toFixed(2)}%)` },
{ name: "plan", value: `free` }
].map(field => ({ ...field, inline: true })))
await interaction.reply({ embeds: [embed] });
}
export default [data, execute] as const;