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=
|
TOKEN=
|
||||||
API=
|
API=
|
||||||
|
CLIENT_ID=
|
||||||
|
|
||||||
DB_TYPE=
|
DB_TYPE=
|
||||||
DB_URL=
|
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" {
|
declare module "bun" {
|
||||||
interface Env {
|
interface Env {
|
||||||
|
// discord
|
||||||
TOKEN: string;
|
TOKEN: string;
|
||||||
API: string;
|
CLIENT_ID: string;
|
||||||
|
|
||||||
|
// stereo
|
||||||
DB_URL: string;
|
DB_URL: string;
|
||||||
DB_TYPE: "sqlite" | "postgres";
|
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 { db } from "./db";
|
||||||
import { formatSize } from "./lib";
|
|
||||||
import { StereoFile } from "./types";
|
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) => {
|
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
|
const files = (await db.all<StereoFile[]>`SELECT * FROM files`).length
|
||||||
c.user.setPresence({
|
c.user.setPresence({
|
||||||
activities: [{ name: `${files} files uploaded to stereo`, type: ActivityType.Custom }],
|
activities: [{ name: `${files} files uploaded to stereo`, type: ActivityType.Custom }],
|
||||||
status: "dnd"
|
status: "dnd"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(c.user.tag);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on(Events.MessageCreate, async (message) => {
|
const commands = [
|
||||||
if (message.author.bot) return;
|
user
|
||||||
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 embed = new EmbedBuilder()
|
client.on(Events.InteractionCreate, async interaction => {
|
||||||
.setColor(0xff264e)
|
if (!interaction.isChatInputCommand()) return;
|
||||||
.setAuthor({
|
const cmd = commands.find(([data]) => data.name === interaction.commandName);
|
||||||
name: `${message.author.globalName || "user"} on stereo`,
|
if (!cmd) return;
|
||||||
iconURL: message.author.avatarURL({ size: 512 }) || ""
|
try {
|
||||||
})
|
await cmd[1](interaction);
|
||||||
.setDescription("here's your overview:")
|
} catch (err) {
|
||||||
.addFields(
|
console.error(err);
|
||||||
{ name: "Uploads", value: `${files.length} files`, inline: true },
|
await interaction.reply({ content: "there was an error executing this command", ephemeral: 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.login(process.env.TOKEN).catch((err) => {
|
client.login(process.env.TOKEN).catch((err) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue