Bot update

This commit is contained in:
Madeline
2022-07-22 22:30:38 +10:00
parent 96c8c854ca
commit ce224e200b
11 changed files with 766 additions and 516 deletions
+3 -4
View File
@@ -22,13 +22,12 @@ export default class Bot {
type: "WATCHING",
}]
});
};
onMessageCreate = (msg: Message) => {
onMessageCreate = async (msg: Message) => {
const prefix = process.env.PREFIX as string;
if (msg.content.indexOf(prefix) === -1) return;
if (msg.author.bot) return;
if (msg.content && msg.content.indexOf(prefix) === -1) return;
const content = msg.content.slice(prefix.length).split(" ");
const cmd = content.shift();
@@ -38,7 +37,7 @@ export default class Bot {
const command = this.commands[cmd];
if (!command) return;
command.exec({
await command.exec({
user: msg.author,
member: msg.member,
guild: msg.guild,
+13 -9
View File
@@ -14,20 +14,24 @@ export type Command = {
exec: (ctx: CommandContext) => any;
};
const walk = async (path: string): Promise<Command[]> => {
const walk = async (path: string) => {
const files = fs.readdirSync(path);
const out: Command[] = [];
const out = [];
for (var file of files) {
if (file.indexOf("index") !== -1) continue;
var imported = await import(`${path}/${file}`);
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; } = {};
(await walk("./build/commands")).forEach((val) => map[val.name] = val);
const map: { [key: string]: Command } = {};
for (var cmd of await walk("./build/commands")) {
map[cmd.name] = cmd;
}
return map;
};
};
+29 -2
View File
@@ -1,8 +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: ({ message }) => {
message.reply("Test");
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;
+5 -1
View File
@@ -1,6 +1,7 @@
import "dotenv/config";
import Fosscord from "fosscord-gopnik";
import Bot from "./Bot.js"; // huh?
import { initDatabase } from "@fosscord/util";
const client = new Fosscord.Client({
intents: ["GUILD_MESSAGES"],
@@ -17,4 +18,7 @@ const bot = new Bot(client);
client.on("ready", bot.onReady);
client.on("messageCreate", bot.onMessageCreate);
client.login(process.env.TOKEN);
(async () => {
await initDatabase();
await client.login(process.env.TOKEN);
})();