add user command to retrieve user information and upload stats
This commit is contained in:
parent
2f57cb7d39
commit
5cfcb3ba0c
4 changed files with 82 additions and 31 deletions
|
@ -1,5 +1,6 @@
|
|||
TOKEN=
|
||||
API=
|
||||
CLIENT_ID=
|
||||
|
||||
DB_TYPE=
|
||||
DB_URL=
|
46
cmd/user.ts
Normal file
46
cmd/user.ts
Normal file
|
@ -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<StereoFile[]>`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;
|
5
globals.d.ts
vendored
5
globals.d.ts
vendored
|
@ -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";
|
||||
}
|
||||
|
|
61
index.ts
61
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<StereoFile[]>`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<StereoFile[]>`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) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue