Added ability for super_events
This commit is contained in:
parent
489e46a17d
commit
fff07a160d
35
src/app.ts
35
src/app.ts
@ -3,7 +3,7 @@ import { Client, Intents, Message } from "discord.js";
|
|||||||
import { config } from "dotenv";
|
import { config } from "dotenv";
|
||||||
import { readdirSync } from "fs";
|
import { readdirSync } from "fs";
|
||||||
import { emouty } from "./utils/emotes";
|
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";
|
import { formatCas, oddiakritikovat } from "./utils/utils.js";
|
||||||
|
|
||||||
const ints = Intents.FLAGS;
|
const ints = Intents.FLAGS;
|
||||||
@ -12,14 +12,20 @@ config();
|
|||||||
|
|
||||||
const prefix = process.env.PREFIX || "more";
|
const prefix = process.env.PREFIX || "more";
|
||||||
const modulFolder = `${__dirname}/modules/`;
|
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 komandy: Record<string, Komand> = {};
|
||||||
const aliasy: Record<string, string> = {};
|
const aliasy: Record<string, string> = {};
|
||||||
let spink = false;
|
let spink = false;
|
||||||
const kuldan_log: Record<string, Record<string, number>> = {};
|
const kuldan_log: Record<string, Record<string, number>> = {};
|
||||||
|
|
||||||
const runEvent = (name: string, args: any[]) => {
|
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);
|
listener(...args);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -30,12 +36,21 @@ readdirSync(modulFolder).forEach(soubor => {
|
|||||||
console.log(`Loaded: ${modulFolder}${soubor}`);
|
console.log(`Loaded: ${modulFolder}${soubor}`);
|
||||||
modul.client = client;
|
modul.client = client;
|
||||||
Object.keys(modul).forEach(name => {
|
Object.keys(modul).forEach(name => {
|
||||||
if (name.startsWith("on_")) {
|
const regex = /^(?<s>super_)?on_(?<h>.+)/.exec(name);
|
||||||
if (!eventy[name]) {
|
if (regex) {
|
||||||
eventy[name] = [];
|
const groups = regex.groups!;
|
||||||
if (name !== "on_message") client.on(name.slice(3), (...args) => runEvent(name, args));
|
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") {
|
} else if (name === "more_komandy") {
|
||||||
Object.keys(modul[name]).forEach(cmdName => {
|
Object.keys(modul[name]).forEach(cmdName => {
|
||||||
const value = modul[name][cmdName];
|
const value = modul[name][cmdName];
|
||||||
@ -85,14 +100,14 @@ const maKuldan = (id: string, komand: string, kuldan_komandu: number) => {
|
|||||||
client.on("messageCreate", mes => {
|
client.on("messageCreate", mes => {
|
||||||
if (process.env.IGNORE_MESS || spim(mes)) return;
|
if (process.env.IGNORE_MESS || spim(mes)) return;
|
||||||
|
|
||||||
runEvent("on_message", [mes]);
|
|
||||||
const [mes_prefix, komandSDiakritikou, ...args] = mes.content.split(" ");
|
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");
|
if (!komandSDiakritikou) return void mes.channel.send("coe voe");
|
||||||
|
|
||||||
const komand = oddiakritikovat(komandSDiakritikou).toLowerCase();
|
const komand = oddiakritikovat(komandSDiakritikou).toLowerCase();
|
||||||
const celArgs = args.join(" ");
|
const celArgs = args.join(" ");
|
||||||
const cmdName = aliasy[komand] ?? komand;
|
const cmdName = aliasy[komand] ?? komand;
|
||||||
|
if (runEvent("message", [mes, cmdName])) return;
|
||||||
const cmd = komandy[cmdName];
|
const cmd = komandy[cmdName];
|
||||||
if (!cmd) return void mes.channel.send("co to znamena ti gadzovko");
|
if (!cmd) return void mes.channel.send("co to znamena ti gadzovko");
|
||||||
|
|
||||||
|
|||||||
@ -10,10 +10,17 @@ interface KomandRaw {
|
|||||||
|
|
||||||
export type ListenerFunkce = (...args: any[]) => void;
|
export type ListenerFunkce = (...args: any[]) => void;
|
||||||
|
|
||||||
|
export type SuperListenerFunkce = (...args: any[]) => boolean;
|
||||||
|
|
||||||
|
interface SuperObject {
|
||||||
|
pos: number;
|
||||||
|
fun: SuperListenerFunkce;
|
||||||
|
}
|
||||||
|
|
||||||
export type Modul = {
|
export type Modul = {
|
||||||
more_komandy: Record<string, RunFunkce | KomandRaw | string>;
|
more_komandy: Record<string, RunFunkce | KomandRaw | string>;
|
||||||
client: Client<boolean>;
|
client: Client<boolean>;
|
||||||
} & Record<string, ListenerFunkce>;
|
} & Record<string, ListenerFunkce | SuperObject | SuperListenerFunkce>;
|
||||||
|
|
||||||
export interface Komand {
|
export interface Komand {
|
||||||
run: RunFunkce | string;
|
run: RunFunkce | string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user