Denim-Bot/src/app.ts
2021-09-23 12:13:52 +02:00

102 lines
3.6 KiB
TypeScript

import { Client, Intents } from "discord.js";
import { readdirSync } from "fs";
import { Komand, ListenerFunkce, Modul, SuperListenerFunkce } from "./utils/types";
import { formatCas, loadEnv, oddiakritikovat } from "./utils/utils.js";
const ints = Intents.FLAGS;
const client = new Client({ intents: [ints.GUILDS, ints.GUILD_VOICE_STATES, ints.GUILD_PRESENCES, ints.GUILD_MESSAGES] });
loadEnv();
const prefix = process.env.prefix || "more";
const modulFolder = `${__dirname}/modules/`;
const eventy: Record<string, ListenerFunkce[]> = {};
const superEventy: Record<string, SuperListenerFunkce[]> = {};
const komandy: Record<string, Komand> = {};
const aliasy: Record<string, string> = {};
let spink = false;
const kuldan_log: Record<string, Record<string, number>> = {};
const runEvent = (name: string, args: any[]) => {
for (const listener of superEventy[name] || []) {
if (!listener) continue;
if (listener(...args)) return true;
}
eventy[name]?.forEach(listener => {
listener(...args);
});
};
readdirSync(modulFolder).forEach(soubor => {
if (!soubor.endsWith(".js")) return;
const modul: Modul = require(`${modulFolder}${soubor}`);
console.log(`Loaded: ${modulFolder}${soubor}`);
modul.client = client;
Object.keys(modul).forEach(name => {
const regex = /^(?<s>super_)?on_(?<h>.+)/.exec(name);
if (regex) {
const groups = regex.groups!;
const ev = groups.s ? superEventy : eventy;
if (!ev[groups.h]) {
ev[groups.h] = [];
if (groups.h != "message") client.on(groups.h, (...args) => void runEvent(groups.h, args));
}
const n = modul[name];
if (typeof n == "object") {
const prev = ev[groups.h][n.pos] as SuperListenerFunkce | undefined;
ev[groups.h][n.pos] = n.fun;
if (prev) ev[groups.h].push(prev);
}
else ev[groups.h].push(n);
} else if (name === "more_komandy") {
Object.keys(modul[name]).forEach(cmdName => {
const value = modul[name][cmdName];
if (typeof value !== "object") {
komandy[cmdName] = { run: value };
} else {
komandy[cmdName] = { run: value.run, cd: value.cd };
value.als?.forEach(al => aliasy[al] = cmdName);
}
});
}
});
});
const maKuldan = (id: string, komand: string, kuldan_komandu: number) => {
if (!kuldan_log[komand]) kuldan_log[komand] = {};
const cas_ted = Date.now() / 1000;
const rozdil = cas_ted - kuldan_log[komand][id];
if (rozdil < kuldan_komandu) return kuldan_komandu - rozdil;
kuldan_log[komand][id] = cas_ted;
return 0;
};
client.on("messageCreate", async mes => {
if (process.env.ignoreMess) return;
const [mes_prefix, komandSDiakritikou, ...args] = mes.content.split(" ");
if (oddiakritikovat(mes_prefix.toLowerCase()) != prefix) return void runEvent("message", [mes]);
if (!komandSDiakritikou) return void mes.channel.send("coe voe");
const komand = oddiakritikovat(komandSDiakritikou).toLowerCase();
const celArgs = args.join(" ");
const cmdName = aliasy[komand] ?? komand;
if (runEvent("message", [mes, cmdName])) return;
const cmd = komandy[cmdName];
if (!cmd) return void mes.channel.send("co to znamena ti gadzovko");
if (cmd.cd) {
const zbyva = Math.round(maKuldan(mes.author.id, cmdName, cmd.cd));
if (zbyva) return void mes.channel.send(`si kkt vole maz kuldan jeste ${formatCas(zbyva)}`);
}
const akce = cmd.run;
if (typeof akce === "string") return void mes.channel.send(akce);
const result = await akce(celArgs, mes);
if (result && !(result instanceof Promise)) mes.channel.send(result);
});
client.login(process.env.token);