From feec9bd6ab209c3bedc67b715a2609a90aeaf202 Mon Sep 17 00:00:00 2001 From: Histmy Date: Thu, 28 Oct 2021 20:40:26 +0200 Subject: [PATCH] Better type defs for OutputRunFunkce and Modul & added type for module exports & removed unnecessary function parameters in modules --- src/app.ts | 12 ++++++------ src/modules/komArgs.ts | 22 ++++++++++++---------- src/modules/komComplex.ts | 31 +++++++++++++++++-------------- src/modules/komRNG.ts | 18 +++++++++++------- src/modules/komStatic.ts | 6 +++++- src/modules/muzika.ts | 8 +++++--- src/modules/onReady.ts | 7 +++++-- src/modules/spink.ts | 14 ++++++++------ src/modules/status.ts | 6 ++++-- src/modules/vojs.ts | 20 +++++++++++--------- src/modules/zbytek.ts | 11 +++++++---- src/utils/types.ts | 14 ++++++++------ 12 files changed, 99 insertions(+), 70 deletions(-) diff --git a/src/app.ts b/src/app.ts index d7fe805..eed1e0b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,6 @@ import { Client, Intents } from "discord.js"; import { readdirSync } from "fs"; -import { Komand, ListenerFunkce, Modul, SuperListenerFunkce } from "./utils/types"; +import { EventSOn, Komand, ListenerFunkce, Modul, SuperListenerFunkce } from "./utils/types"; import { formatCas, loadEnv, oddiakritikovat } from "./utils/utils.js"; const ints = Intents.FLAGS; @@ -13,7 +13,6 @@ const eventy: Record = {}; const superEventy: Record = {}; const komandy: Record = {}; const aliasy: Record = {}; -let spink = false; const kuldan_log: Record> = {}; const runEvent = (name: string, args: any[]) => { @@ -41,16 +40,17 @@ readdirSync(modulFolder).forEach(soubor => { ev[groups.h] = []; if (groups.h != "message") client.on(groups.h, (...args) => void runEvent(groups.h, args)); } - const n = modul[name]; + const n = modul[name as EventSOn]!; if (typeof n == "object") { - const prev = ev[groups.h][n.pos] as SuperListenerFunkce | undefined; + const prev = ev[groups.h][n.pos]; 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]; + const n = modul[name]!; + Object.keys(n).forEach(cmdName => { + const value = n[cmdName]; if (typeof value !== "object") { komandy[cmdName] = { run: value }; } else { diff --git a/src/modules/komArgs.ts b/src/modules/komArgs.ts index f4575da..f6d165e 100644 --- a/src/modules/komArgs.ts +++ b/src/modules/komArgs.ts @@ -1,36 +1,38 @@ // Komandy, který pošlou jenom celArgs a random hovno -import { Message } from "discord.js"; import { emouty } from "../utils/emotes"; +import { Modul } from "../utils/types"; -module.exports = { +const exp: Modul = { more_komandy: { - rekni: (mes: Message, arg: string) => { + rekni: (mes, arg) => { if (mes.author.bot) return "ne"; - const corict = arg ?? "co mam jako rict"; + const corict = arg || `co mam jako rict ${mes.author}`; mes.delete(); return corict; }, - clap: (mes: Message, arg: string) => { + clap: (mes, arg) => { mes.delete(); return `${arg} ${emouty.clap}`; }, - clap2: (mes: Message, arg: string) => { + clap2: (mes, arg) => { mes.delete(); return `${emouty.clap2} ${arg}`; }, - voliz: (mes: Message, arg: string) => { + voliz: (mes, arg) => { mes.delete(); return `${emouty.lickL}${arg}${emouty.lickR}`; }, - pozdrav: (_: any, arg: string) => `zdravim ${arg}`, + pozdrav: (_, arg) => `zdravim ${arg}`, - zhejti: (_: any, arg: string) => `${arg} je pycovina zasrana vimrdana`, + zhejti: (_, arg) => `${arg} je pycovina zasrana vimrdana`, - uraz: (_: any, arg: string) => `${arg} , u suck` + uraz: (_, arg) => `${arg} , u suck` } }; + +module.exports = exp; diff --git a/src/modules/komComplex.ts b/src/modules/komComplex.ts index 9b017d7..c22a935 100644 --- a/src/modules/komComplex.ts +++ b/src/modules/komComplex.ts @@ -3,6 +3,7 @@ import { ActivityType, Message, PresenceStatusData } from "discord.js"; import { emouty } from "../utils/emotes"; +import { Modul } from "../utils/types"; const changeStatus = (mes: Message, status: PresenceStatusData) => { mes.client.user?.setStatus(status); @@ -17,35 +18,35 @@ const changeActivity = (mes: Message, activity: ActivityType | undefined = undef const ping = /^<@!?\d+>$/; -module.exports = { +const exp: Modul = { more_komandy: { online: { als: ["onlajn", "zelenej"], - run: (mes: Message) => changeStatus(mes, "online") + run: (mes) => changeStatus(mes, "online") }, idle: { als: ["zlutej", "afk", "idle", "nepritomnej"], - run: (mes: Message) => changeStatus(mes, "idle") + run: (mes) => changeStatus(mes, "idle") }, dnd: { als: ["nerusit", "cervenej"], - run: (mes: Message) => changeStatus(mes, "dnd") + run: (mes) => changeStatus(mes, "dnd") }, offline: { als: ["oflajn", "neviditelnej"], - run: (mes: Message) => changeStatus(mes, "invisible") + run: (mes) => changeStatus(mes, "invisible") }, - hraj: (mes: Message, arg: string) => changeActivity(mes, "PLAYING", arg), - sleduj: (mes: Message, arg: string) => changeActivity(mes, "WATCHING", arg), - poslouchej: (mes: Message, arg: string) => changeActivity(mes, "LISTENING", arg), - soutez: (mes: Message, arg: string) => changeActivity(mes, "COMPETING", arg), - nedelej: (mes: Message) => changeActivity(mes), + hraj: (mes, arg) => changeActivity(mes, "PLAYING", arg), + sleduj: (mes, arg) => changeActivity(mes, "WATCHING", arg), + poslouchej: (mes, arg) => changeActivity(mes, "LISTENING", arg), + soutez: (mes, arg) => changeActivity(mes, "COMPETING", arg), + nedelej: (mes) => changeActivity(mes), fight: { als: ["figh", "fajt"], - run: (mes: Message, arg: string) => { + run: (mes, arg) => { if (!ping.test(arg)) return "tak si kokot ti kokote"; const vyherce = Math.random() < 0.5 ? mes.author : arg; @@ -53,7 +54,7 @@ module.exports = { } }, - status: (mes: Message, arg: string) => { + status: (mes, arg) => { if (!ping.test(arg)) return "tak si kokot ti kokote"; const uzivatel = mes.mentions.members!.first()!; @@ -80,7 +81,7 @@ module.exports = { zareaguj: { als: ["react"], - run: async (mes: Message, arg: string) => { + run: async (mes, arg) => { const emouty = arg.match(//g); if (!emouty) return "retard"; @@ -97,7 +98,7 @@ module.exports = { } }, - odpocitej: (mes: Message) => { + odpocitej: (mes) => { const randomshit = (mes: Message, argument: string[]) => { mes.edit(argument[0]); argument.splice(0, 1); @@ -115,3 +116,5 @@ module.exports = { } } }; + +module.exports = exp; diff --git a/src/modules/komRNG.ts b/src/modules/komRNG.ts index 7dded93..f53f513 100644 --- a/src/modules/komRNG.ts +++ b/src/modules/komRNG.ts @@ -1,5 +1,7 @@ // Komandy, který jenom pošlou random hovno a jsou nějakým způsobem ovlivněný RNG +import { Modul } from "../utils/types"; + const ftipy: string[] = require("../../res/ftipy.json"); const mista = ["na šroťák", "na vrakoviště", "na smetiště", "do kontejneru", "na skládku", "do kriminálu", "pod most", "do sběru", "do hospody", "do najt klubu", "na folmavu"]; const uz = ["ne", "jeste ne", "jiz brzy", "za chvili", "vubec", "nikdy", "za dlouho", "za 5 let", "zejtra", "davno", "jo", "mozna"]; @@ -8,7 +10,7 @@ const rand = (max: number) => Math.floor(Math.random() * max); const choose = (arr: any[]) => arr[rand(arr.length)]; -module.exports = { +const exp: Modul = { more_komandy: { vtip: { @@ -25,30 +27,30 @@ module.exports = { ma: () => rand(2) ? "jo ma" : "ne nema", - nazor: (_: any, arg: string) => rand(2) ? `mam rad ${arg}` : `${arg} je picovina`, + nazor: (_, arg) => rand(2) ? `mam rad ${arg}` : `${arg} je picovina`, si: { als: ["jsi"], - run: (_: any, arg: string) => { + run: (_, arg) => { const corict = arg.replace(/\?/g, ""); return rand(2) ? `jo sem ${corict}` : `ne nejsem ${corict}`; } }, - mas: (_: any, arg: string) => { + mas: (_, arg) => { const corict = arg.replace(/\?/g, ""); return rand(2) ? `jo mam ${corict}` : `ne nemam ${corict}`; }, jakmoc: { cd: 1800, - run: (_: any, arg: string) => `${arg} na ${rand(101)}%` + run: (_, arg) => `${arg} na ${rand(101)}%` }, jakmoc0: { cd: 1800, - run: (_: any, arg: string) => `${arg} na ${rand(1001)}‰` + run: (_, arg) => `${arg} na ${rand(1001)}‰` }, uz: { @@ -56,10 +58,12 @@ module.exports = { run: () => choose(uz) }, - vyber: (_: any, arg: string) => { + vyber: (_, arg) => { if (!arg.length) return "co vole"; const moznosti = arg.split(arg.indexOf("|") > -1 ? "|" : ",").filter(m => m.length); return choose(moznosti) || "kokot"; } } }; + +module.exports = exp; diff --git a/src/modules/komStatic.ts b/src/modules/komStatic.ts index 1a31932..4e5eb34 100644 --- a/src/modules/komStatic.ts +++ b/src/modules/komStatic.ts @@ -1,8 +1,10 @@ // Sekce pro komandy, který jenom pošlou nějaký hovno bez a nevyžadují argumenty +import { Modul } from "../utils/types"; + const pomoc: [string[], {}] = require("../../res/pomoc.json"); -module.exports = { +const exp: Modul = { more_komandy: { vole: "coe voe more gadzo", @@ -33,3 +35,5 @@ module.exports = { navrh: "tadi mas prostor https://navrhy.denim3001.deadfish.cz/" } }; + +module.exports = exp; diff --git a/src/modules/muzika.ts b/src/modules/muzika.ts index a79babb..46eb3be 100644 --- a/src/modules/muzika.ts +++ b/src/modules/muzika.ts @@ -1,12 +1,12 @@ // Tady bude muzika, vole -import { Message } from "discord.js"; import * as ytdl from "ytdl-core"; +import { Modul } from "../utils/types"; import { joinVoice, play } from "../utils/utils"; -module.exports = { +const exp: Modul = { more_komandy: { - zahraj: async (mes: Message, url: string) => { + zahraj: async (mes, url) => { if (!ytdl.validateURL(url)) return "tuto neni validni youtube url a to je zatim jedini co hodlam hrat"; const kanel = mes.member?.voice.channel; if (!kanel) return "nejsi ve vojsu ty kkt"; @@ -15,3 +15,5 @@ module.exports = { } } }; + +module.exports = exp; diff --git a/src/modules/onReady.ts b/src/modules/onReady.ts index 177fb62..a32e77a 100644 --- a/src/modules/onReady.ts +++ b/src/modules/onReady.ts @@ -1,8 +1,9 @@ -// Až se bot načte tak to napíše do konzole a jestli pošel, tak to oznámí adminovi +// Až se bot načte tak to napíše do konzole a jestli pošel, tak to oznámí adminům import { Client } from "discord.js"; +import { Modul } from "../utils/types"; -module.exports = { +const exp: Modul = { on_ready: async () => { console.log("A jedeš!"); if (process.argv.length < 3) return; @@ -13,3 +14,5 @@ module.exports = { if (channel && channel.isText()) channel.send("pošel jsem magoří"); } }; + +module.exports = exp; diff --git a/src/modules/spink.ts b/src/modules/spink.ts index 23ae6c4..3067839 100644 --- a/src/modules/spink.ts +++ b/src/modules/spink.ts @@ -3,7 +3,7 @@ import { GuildMember, Message, Role, VoiceState } from "discord.js"; import fetch from "node-fetch"; import { emouty } from "../utils/emotes"; -import { Spinkackar } from "../utils/types"; +import { Modul, Spinkackar } from "../utils/types"; import { formatCas, formatter } from "../utils/utils"; let spinkacky: Record; @@ -41,12 +41,12 @@ const handleSpink = async (act: "spinkacek" | "vstavacek", member: GuildMember) return ok.startsWith("OK") ? ok : false; }; -module.exports = { +const exp: Modul = { more_komandy: { spinkacek: { als: ["spink", "spoink", "spinkake", "spoinkacek", "gn", "<:spinkacek:761652251966046208>", "<:gn:887124590583746590>"], - run: async (mes: Message) => { + run: async (mes) => { if (mes.author.bot) return `až někdy${emouty.kapp}`; if (await handleSpink("spinkacek", mes.member!)) { mes.react(emouty.spinkacek); @@ -62,7 +62,7 @@ module.exports = { vstavacek: { als: ["vstavcacek", "gm", "unspinkacek"], - run: async (mes: Message) => { + run: async (mes) => { if (mes.author.bot) return emouty.sjeta; const odpoved = await handleSpink("vstavacek", mes.member!); @@ -80,7 +80,7 @@ module.exports = { } }, - pgn: (mes: Message, kdy: string) => { + pgn: (mes, kdy) => { if (mes.author.bot) return emouty.sjeta; const ted = new Date(); @@ -116,7 +116,7 @@ module.exports = { ps: { als: ["poslednispink"], - run: (mes: Message) => { + run: (mes) => { const cas = Number(new Date()) - Number(new Date(spinkacky[mes.author.id].timeup)); return `uz jsi vzhuru ${formatCas(cas / 1000)}`; } @@ -178,3 +178,5 @@ module.exports = { } } }; + +module.exports = exp; diff --git a/src/modules/status.ts b/src/modules/status.ts index 918c60c..551a519 100644 --- a/src/modules/status.ts +++ b/src/modules/status.ts @@ -1,7 +1,7 @@ // Trekování statusů všech lidí o kterých bot ví import { Client, Guild, Presence, User } from "discord.js"; import fetch from "node-fetch"; -import { FakePresence, StatusyINaFounu, UserChange, ZmenovejObjekt } from "../utils/types"; +import { FakePresence, Modul, StatusyINaFounu, UserChange, ZmenovejObjekt } from "../utils/types"; const role = { online: "Online", idle: "Idle", dnd: "DND", offline: "Offline" }; const statusy = { Offline: "0", Online: "1", Idle: "2", DND: "3", OnlinePhone: "11", IdlePhone: "12", DNDPhone: "13" }; @@ -55,7 +55,7 @@ if (!process.env.ignorePresence) ziju(); const getRole = (status: StatusyINaFounu, server: Guild) => server.roles.cache.find(role => role.name === `Status${status}`); -module.exports = { +const exp: Modul = { // Změna rolí podle statusu a odeslání statusu on_presenceUpdate: (bef: Presence, aft: Presence) => { @@ -108,3 +108,5 @@ module.exports = { prepSend([{ ch: "user", user: aft }]); } }; + +module.exports = exp; diff --git a/src/modules/vojs.ts b/src/modules/vojs.ts index a16d75c..fdd4d8b 100644 --- a/src/modules/vojs.ts +++ b/src/modules/vojs.ts @@ -1,9 +1,9 @@ // Cokoliv co má něco společnýho s vojsem import { getVoiceConnection, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice"; -import { GuildMember, Message, VoiceChannel } from "discord.js"; +import { GuildMember, VoiceChannel } from "discord.js"; import { emouty } from "../utils/emotes"; -import { MuzikaFace } from "../utils/types"; +import { Modul, MuzikaFace } from "../utils/types"; import { handlePrevVoice, joinVoice, play } from "../utils/utils"; const timeouty: Record = {}; @@ -57,14 +57,14 @@ const vytahnout = (member: GuildMember, patro: number) => { setTimeout(() => vytahnout(member, patro), 1e3); }; -module.exports = { +const exp: Modul = { more_komandy: { - wojs: (mes: Message) => `vojs se pise s normalnim v ti kriple ${mes.author}`, + wojs: (mes) => `vojs se pise s normalnim v ti kriple ${mes.author}`, vojs: { cd: 1800, - run: (mes: Message, arg: string) => { + run: (mes, arg) => { const channel = mes.member?.voice.channel; if (!channel) return `di si tam sam ne ty gadzo ${mes.author}`; @@ -89,7 +89,7 @@ module.exports = { vypadni: { als: ["odejdi", "disconnect", "leave", "odpoj", "votpoj", "vodpoj", "vodprejskni", "tahni"], - run: (mes: Message) => { + run: (mes) => { const vojs = getVoiceConnection(mes.guildId!); if (!vojs) return 'nejsem ve vojsu'; @@ -100,7 +100,7 @@ module.exports = { vytah: { als: ["vitah"], - run: (mes: Message, arg: string) => { + run: (mes, arg) => { if (!mes.member?.voice.channel) return `nejsi ve vojsu ty gadzo ${mes.author}`; if (!arg) return `napis do jakiho patra ${mes.author}`; @@ -110,7 +110,7 @@ module.exports = { krkacek: { als: ["krk", "grg", "grgnisi", "krknisi", "grgacek"], - run: async (mes: Message) => { + run: async (mes) => { const channel = mes.member?.voice.channel; if (!channel) return "***grrrrrrrrg***"; @@ -122,7 +122,7 @@ module.exports = { cas: { cd: 5, - run: async (mes: Message) => { + run: async (mes) => { const date = new Date(); const h = date.getHours(); const m = date.getMinutes(); @@ -163,3 +163,5 @@ module.exports = { } } }; + +module.exports = exp; diff --git a/src/modules/zbytek.ts b/src/modules/zbytek.ts index b2a0252..6b1d724 100644 --- a/src/modules/zbytek.ts +++ b/src/modules/zbytek.ts @@ -4,18 +4,19 @@ import { getVoiceConnections } from "@discordjs/voice"; import { Client, Message } from "discord.js"; import { emouty } from "../utils/emotes"; import { createServer } from "http"; +import { Modul } from "../utils/types"; let spim = false; -module.exports = { +const exp: Modul = { more_komandy: { - debug_log: (_: any, arg: string) => { + debug_log: (_, arg) => { console.log("log: ", arg); return "je to v konzoli"; }, - update: (mes: Message) => { + update: (mes) => { if (mes.channel.id != process.env.adminChannel && mes.author.id != process.env.adminID) return "nato nemas prava kokote"; getVoiceConnections().forEach(con => con.disconnect()); @@ -43,7 +44,7 @@ module.exports = { mes.client.user?.setStatus("invisible"); spim = true; } - else return; + else return false; } return true; } @@ -75,3 +76,5 @@ module.exports = { }).listen(1298); } }; + +module.exports = exp; diff --git a/src/utils/types.ts b/src/utils/types.ts index e3b36f2..02601c6 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,13 +1,13 @@ -import { Client, ClientPresenceStatusData, Message, MessageOptions, User } from "discord.js"; +import { Client, ClientEvents, ClientPresenceStatusData, Message, MessageOptions, User } from "discord.js"; -type OutputRunFunkce = string | MessageOptions | undefined; +type OutputRunFunkce = string | MessageOptions | void; type RunFunkce = (message: Message, argumenty: string) => OutputRunFunkce | Promise; interface KomandRaw { als?: string[]; cd?: number; - run: RunFunkce; + run: string | RunFunkce; }; export type ListenerFunkce = (...args: any[]) => void; @@ -19,10 +19,12 @@ interface SuperObject { fun: SuperListenerFunkce; } +export type EventSOn = `on_${keyof ClientEvents}` | `super_on_${keyof ClientEvents}`; + export type Modul = { - more_komandy: Record; - client: Client; -} & Record; + more_komandy?: Record; + client?: Client; +} & { [key in EventSOn]?: ListenerFunkce | SuperObject | SuperListenerFunkce; }; export interface Komand { run: RunFunkce | string;