summary refs log tree commit diff
path: root/slowcord/bot/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'slowcord/bot/src/commands')
-rw-r--r--slowcord/bot/src/commands/index.ts37
-rw-r--r--slowcord/bot/src/commands/instance.ts35
2 files changed, 72 insertions, 0 deletions
diff --git a/slowcord/bot/src/commands/index.ts b/slowcord/bot/src/commands/index.ts
new file mode 100644

index 00000000..d3b39e0f --- /dev/null +++ b/slowcord/bot/src/commands/index.ts
@@ -0,0 +1,37 @@ +import { Message, GuildMember, Guild, User } from "discord.js"; +import fs from "fs"; + +export type CommandContext = { + user: User, + guild: Guild | null, + member: GuildMember | null, + message: Message, + args: string[], +}; + +export type Command = { + name: string; + exec: (ctx: CommandContext) => any; +}; + +const walk = async (path: string) => { + const files = fs.readdirSync(path); + const out = []; + for (var file of files) { + if (fs.statSync(`${path}/${file}`).isDirectory()) continue; + if (file.indexOf("index") !== -1) + continue; + if (file.indexOf(".js") !== file.length - 3) continue; + var imported = (await import(`./${file}`)).default; + out.push(imported); + } + return out; +}; + +export const getCommands = async () => { + const map: { [key: string]: Command } = {}; + for (var cmd of await walk("./build/commands")) { + map[cmd.name] = cmd; + } + return map; +}; diff --git a/slowcord/bot/src/commands/instance.ts b/slowcord/bot/src/commands/instance.ts new file mode 100644
index 00000000..d1b08cef --- /dev/null +++ b/slowcord/bot/src/commands/instance.ts
@@ -0,0 +1,35 @@ +import { Command } from "./index.js"; +import { User, Guild, Message } from "@fosscord/util"; + +const cache: { [key: string]: number; } = { + users: 0, + guilds: 0, + messages: 0, + lastChecked: 0, +}; + +export default { + name: "instance", + exec: async ({ message }) => { + if (Date.now() > cache.lastChecked + parseInt(process.env.CACHE_TTL as string)) { + cache.users = await User.count(); + cache.guilds = await Guild.count(); + cache.messages = await Message.count(); + cache.lastChecked = Date.now(); + } + + return message.reply({ + embeds: [{ + title: "Instance Stats", + footer: { + text: `Last checked: ${Math.floor((Date.now() - cache.lastChecked) / (1000 * 60))} minutes ago`, + }, + fields: [ + { inline: true, name: "Total Users", value: cache.users.toString() }, + { inline: true, name: "Total Guilds", value: cache.guilds.toString() }, + { inline: true, name: "Total Messages", value: cache.messages.toString() }, + ] + }] + }); + } +} as Command; \ No newline at end of file