Added ability for super_events

This commit is contained in:
Histmy 2021-09-04 23:33:32 +02:00
parent 489e46a17d
commit fff07a160d
2 changed files with 33 additions and 11 deletions

View File

@ -3,7 +3,7 @@ import { Client, Intents, Message } from "discord.js";
import { config } from "dotenv";
import { readdirSync } from "fs";
import { emouty } from "./utils/emotes";
import { Komand, ListenerFunkce, Modul } from "./utils/types";
import { Komand, ListenerFunkce, Modul, SuperListenerFunkce } from "./utils/types";
import { formatCas, oddiakritikovat } from "./utils/utils.js";
const ints = Intents.FLAGS;
@ -12,14 +12,20 @@ config();
const prefix = process.env.PREFIX || "more";
const modulFolder = `${__dirname}/modules/`;
const eventy: Record<string, ListenerFunkce[]> = { on_message: [] };
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[]) => {
eventy[name].forEach(listener => {
for (const listener of superEventy[name] || []) {
if (!listener) continue;
if (listener(...args)) return true;
}
eventy[name]?.forEach(listener => {
listener(...args);
});
};
@ -30,12 +36,21 @@ readdirSync(modulFolder).forEach(soubor => {
console.log(`Loaded: ${modulFolder}${soubor}`);
modul.client = client;
Object.keys(modul).forEach(name => {
if (name.startsWith("on_")) {
if (!eventy[name]) {
eventy[name] = [];
if (name !== "on_message") client.on(name.slice(3), (...args) => runEvent(name, args));
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));
}
eventy[name].push(modul[name]);
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];
@ -85,14 +100,14 @@ const maKuldan = (id: string, komand: string, kuldan_komandu: number) => {
client.on("messageCreate", mes => {
if (process.env.IGNORE_MESS || spim(mes)) return;
runEvent("on_message", [mes]);
const [mes_prefix, komandSDiakritikou, ...args] = mes.content.split(" ");
if (mes_prefix.toLowerCase() !== prefix) return;
if (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");

View File

@ -10,10 +10,17 @@ interface KomandRaw {
export type ListenerFunkce = (...args: any[]) => void;
export type SuperListenerFunkce = (...args: any[]) => boolean;
interface SuperObject {
pos: number;
fun: SuperListenerFunkce;
}
export type Modul = {
more_komandy: Record<string, RunFunkce | KomandRaw | string>;
client: Client<boolean>;
} & Record<string, ListenerFunkce>;
} & Record<string, ListenerFunkce | SuperObject | SuperListenerFunkce>;
export interface Komand {
run: RunFunkce | string;