i did stuff

This commit is contained in:
grngxd 2025-07-31 14:45:38 +01:00
parent 67d53818f3
commit 3fb2883091
8 changed files with 105 additions and 10 deletions

View file

@ -1 +1,5 @@
TOKEN=
API=
DB_TYPE=
DB_URL=

View file

@ -5,6 +5,7 @@
"name": "bot",
"dependencies": {
"discord.js": "^14.21.0",
"ky": "^1.8.2",
},
"devDependencies": {
"@types/bun": "latest",
@ -53,6 +54,8 @@
"fast-deep-equal": ["fast-deep-equal@3.1.3", "", {}, "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="],
"ky": ["ky@1.8.2", "", {}, "sha512-XybQJ3d4Ea1kI27DoelE5ZCT3bSJlibYTtQuMsyzKox3TMyayw1asgQdl54WroAm+fIA3ZCr8zXW2RpR7qWVpA=="],
"lodash": ["lodash@4.17.21", "", {}, "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="],
"lodash.snakecase": ["lodash.snakecase@4.1.1", "", {}, "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw=="],

42
db.ts Normal file
View file

@ -0,0 +1,42 @@
// db.ts
import { sql as pgsql } from "bun";
import { Database as SQLiteDB } from "bun:sqlite";
const dbType = process.env.DB_TYPE;
const sqlite = () => {
const db = new SQLiteDB(process.env.DB_URL || ":memory:");
function toSql(strings: TemplateStringsArray, values: any[]) {
let sql = strings[0] || "";
for (let i = 0; i < values.length; i++) {
sql += "?" + (strings[i + 1] || "");
}
return sql;
}
return {
all: async <T extends any[]>(strings: TemplateStringsArray, ...params: any[]) => {
const sql = toSql(strings, params);
return db.query(sql).all(...params) as T;
},
get: async <T>(strings: TemplateStringsArray, ...params: any[]) => {
const sql = toSql(strings, params);
return db.query(sql).get(...params) as T;
},
run: async <T>(strings: TemplateStringsArray, ...params: any[]) => {
const sql = toSql(strings, params);
return db.query(sql).run(...params) as T;
},
};
};
const postgres = () => {
return {
all: async <T>(strings: TemplateStringsArray, ...values: any[]) => await pgsql(strings, ...values) as T,
get: async <T>(strings: TemplateStringsArray, ...values: any[]) => (await pgsql(strings, ...values))[0] as T,
run: async <T>(strings: TemplateStringsArray, ...values: any[]) => await pgsql(strings, ...values) as T,
};
};
export const db = dbType === "sqlite" ? sqlite() : postgres();

3
globals.d.ts vendored
View file

@ -1,5 +1,8 @@
declare module "bun" {
interface Env {
TOKEN: string;
API: string;
DB_URL: string;
DB_TYPE: "sqlite" | "postgres";
}
}

View file

@ -1,19 +1,40 @@
import { Client, Events, GatewayIntentBits } from 'discord.js';
import { Client, EmbedBuilder, Events, GatewayIntentBits } from "discord.js";
import { db } from "./db";
import { formatSize } from "./lib";
import { StereoFile } from "./types";
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.once(Events.ClientReady, (c) => {
console.log(c.user.tag);
console.log(c.user.tag);
});
client.on(Events.MessageCreate, async (message) => {
if (message.author.bot) return;
if (client.user && message.mentions.has(client.user)) {
await message.reply(`hi ${message.author}\n-# TODO: make this show user info`);
}
if (message.author.bot) return;
if (client.user && message.mentions.has(client.user)) {
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()
.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.login(process.env.TOKEN).catch((err) => {
console.error('Failed to login:', err)
process.exit(1)
console.error("Failed to login:", err)
process.exit(1)
});

13
lib.ts Normal file
View file

@ -0,0 +1,13 @@
export const formatSize = (bytes: number): string => {
const units = ["B", "KB", "MB", "GB", "TB"];
let size = bytes;
let unit = units[0];
for (let i = 0; i < units.length; i++) {
if (size < 1024 || i === units.length - 1) {
unit = units[i];
break;
}
size /= 1024;
}
return `${size % 1 === 0 ? size : size.toFixed(2)} ${unit}`;
}

View file

@ -10,6 +10,7 @@
"typescript": "^5"
},
"dependencies": {
"discord.js": "^14.21.0"
"discord.js": "^14.21.0",
"ky": "^1.8.2"
}
}

8
types.ts Normal file
View file

@ -0,0 +1,8 @@
export type StereoFile = {
id: string
name: string
owner: string
size: number
created_at: string
mime: string
}