switched to discord.js version 13 rewrite of js to ts finally removed remporary logging emotes are now in enum fixed vypadni to working
110 lines
3.6 KiB
TypeScript
110 lines
3.6 KiB
TypeScript
import { getVoiceConnections } from "@discordjs/voice";
|
|
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 { formatCas } 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] });
|
|
config();
|
|
|
|
const prefix = process.env.PREFIX || "more";
|
|
const modulFolder = `${__dirname}/modules/`;
|
|
const eventy: Record<string, ListenerFunkce[]> = { on_message: [] };
|
|
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 => {
|
|
listener(...args);
|
|
});
|
|
};
|
|
|
|
readdirSync(modulFolder).forEach(soubor => {
|
|
if (!soubor.endsWith(".js")) return;
|
|
const modul: Modul = require(`${modulFolder}${soubor}`);
|
|
console.log(`Načet sem: ${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));
|
|
}
|
|
eventy[name].push(modul[name]);
|
|
} 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 spim = (mes: Message) => {
|
|
const cont = mes.content.toLocaleLowerCase();
|
|
if (cont === `${prefix} zapni se`) {
|
|
if (spink) {
|
|
spink = false;
|
|
mes.client.user?.setStatus("online");
|
|
mes.channel.send("dobré ráno magoří");
|
|
}
|
|
else mes.channel.send("tak jsi kokot?");
|
|
} else if (!spink) {
|
|
if (cont === `${prefix} vypni se`) {
|
|
mes.react(emouty.purfieRIP);
|
|
getVoiceConnections().forEach(con => con.disconnect());
|
|
mes.client.user?.setStatus("invisible");
|
|
spink = true;
|
|
}
|
|
else return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
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", mes => {
|
|
if (process.env.IGNORE_MESS || spim(mes)) return;
|
|
|
|
runEvent("on_message", [mes]);
|
|
const [mes_prefix, komand, ...args] = mes.content.split(" ");
|
|
if (mes_prefix.toLowerCase() !== prefix) return;
|
|
if (!komand) return void mes.channel.send("coe voe");
|
|
|
|
const celArgs = args.join(" ");
|
|
const cmdName = aliasy[komand] ?? komand;
|
|
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 = akce(celArgs, mes);
|
|
if (result && !(result instanceof Promise)) mes.channel.send(result);
|
|
});
|
|
|
|
client.login(process.env.TOKEN);
|