diff --git a/src/modules/anketa.ts b/src/modules/anketa.ts index 5b67b5e..65161cd 100644 --- a/src/modules/anketa.ts +++ b/src/modules/anketa.ts @@ -1,7 +1,6 @@ // Modul na komand "anketa" -import { ActionRowBuilder, APIEmbed, ButtonBuilder, ButtonStyle, Message, MessageComponentInteraction } from "discord.js"; -import { emouty } from "../utils/emotes"; +import { ActionRowBuilder, APIEmbed, APIEmbedField, ButtonBuilder, ButtonStyle, Message, MessageComponentInteraction } from "discord.js"; import { Modul } from "../utils/types"; import { formatter } from "../utils/utils"; @@ -9,83 +8,92 @@ const exp: Modul = { more_komandy: { anketa: { - arg: "nastavení asi IDK", + arg: "([nastaveni]) moznosti...", run: async (mes, arg) => { - const args = arg.toLocaleLowerCase().split(" "); const settings = { time: 60, - immediateShow: true + immediateShow: true, + listNames: false }; - switch (args[0]) { - case "custom": - case "vlastni": - return `tuto jeste neni implemenovany ${emouty.sjeta}`; - - case "start": - case "zapnout": { - const temp = args.slice(1).join(" "); - const moznosti = temp.split(temp.indexOf("|") > -1 ? "|" : ",").reduce((acc, c) => { - if (c.length) acc.push(c.trim()); - return acc; - }, []); - if (moznosti.length < 2) return "zadej alespo%n dve moznosti"; - - const konec = new Date(); - konec.setSeconds(konec.getSeconds() + settings.time); - - const embed: APIEmbed = { - title: "Hlasování", - description: settings.immediateShow ? `Končí ${formatter(konec)}` : `Výsledky se zobrazí ${formatter(konec)}`, - fields: [] - }; - const radek = new ActionRowBuilder(); - const odpovedi: Record = {}; - - function prepocitat(): void; - function prepocitat(int: MessageComponentInteraction): void; - function prepocitat(int?: MessageComponentInteraction) { - const celkem = Object.keys(odpovedi).length; - embed.fields?.forEach((f, i) => { - const hovn = Object.values(odpovedi).filter(n => n == i); - const dylka = hovn.length; - const p = Math.floor(dylka / celkem * 10) || 0; - f.value = `${"█".repeat(p)}${"░".repeat(10 - p)} ${Math.round(dylka / celkem * 100) || 0}% (${dylka})`; - }); - int?.update({ embeds: [embed] }); - } - - moznosti.forEach((h, i) => { - if (settings.immediateShow) embed.fields?.push({ name: h, value: "" }); - radek.addComponents(new ButtonBuilder({ customId: `${i}`, label: h, style: ButtonStyle.Primary })); - }); - prepocitat(); - const zprava = await mes.channel.send({ embeds: [embed], components: [radek] }); - const collector = mes.channel.createMessageComponentCollector({ filter: i => i.message.id == zprava.id, time: settings.time * 1000 }); - - collector.on("collect", d => { - if (!(d.message instanceof Message)) return; - const i = Number(d.customId); - const prev = odpovedi[d.user.id]; - if (prev == i) return void d.deferUpdate(); - odpovedi[d.user.id] = i; - if (!settings.immediateShow) return void d.deferUpdate(); - prepocitat(d); - }); - - collector.on("end", () => { - embed.description = `Skončilo ${formatter(konec)}`; - if (!settings.immediateShow) prepocitat(); - zprava.edit({ components: [], embeds: [embed] }); - }); - break; + let huuuuuu = -1; + if (arg.startsWith("[")) { + huuuuuu = arg.indexOf("]"); + const hovn = arg.slice(1, huuuuuu); + const f = hovn.split(" "); + for (const s of f) { + const t = Number(s); + if (!Number.isNaN(t)) settings.time = t; + else if (s == "jmena") settings.listNames = true; + else if (s == "neprubezny") settings.immediateShow = false; + else return `"${s}" neznam znam jenom jmena a neprubezny`; } - - default: - return `takovi to "${args[0]}" sem moc nepochitil`; } + + const moznosti = arg.slice(huuuuuu + 1).split(arg.includes("|") ? "|" : ",").reduce((acc, c) => { + if (c.length) acc.push(c.trim()); + return acc; + }, []); + if (moznosti.length < 2) return "zadej alespo%n dve moznosti"; + + const konec = new Date(); + konec.setSeconds(konec.getSeconds() + settings.time); + + const embed: APIEmbed = { + title: "Hlasování", + description: settings.immediateShow ? `Končí ${formatter(konec)}` : `Výsledky se zobrazí ${formatter(konec)}` + }; + const x5fxd55: APIEmbedField[] = []; + const radek = new ActionRowBuilder(); + const odpovedi: Record = {}; + + function prepocitat(): void; + function prepocitat(int: MessageComponentInteraction): void; + function prepocitat(int?: MessageComponentInteraction) { + const celkem = Object.keys(odpovedi).length; + x5fxd55.forEach((f, i) => { + const hovn = Object.values(odpovedi).filter(n => n == i); + const dylka = hovn.length; + const p = Math.floor(dylka / celkem * 10) || 0; + const people = Object.keys(odpovedi).reduce((pr, cur) => { + if (odpovedi[cur] == i) pr.push(cur); + return pr; + }, []); + const kindo = people.map(w => mes.guild?.members.cache.get(w)?.displayName); + f.value = `${"█".repeat(p)}${"░".repeat(10 - p)} ${Math.round(dylka / celkem * 100) || 0}% (${dylka}) ${settings.listNames ? " (" + kindo.join(", ") + ")" : ""}`; + }); + int?.update({ embeds: [embed] }); + } + + moznosti.forEach((h, i) => { + x5fxd55.push({ name: h, value: "" }); + radek.addComponents(new ButtonBuilder({ customId: `${i}`, label: h, style: ButtonStyle.Primary })); + }); + prepocitat(); + if (settings.immediateShow) embed.fields = x5fxd55; + const zprava = await mes.channel.send({ embeds: [embed], components: [radek] }); + const collector = mes.channel.createMessageComponentCollector({ filter: i => i.message.id == zprava.id, time: settings.time * 1000 }); + + collector.on("collect", d => { + if (!(d.message instanceof Message)) return; + const i = Number(d.customId); + const prev = odpovedi[d.user.id]; + if (prev == i) return void d.deferUpdate(); + odpovedi[d.user.id] = i; + if (!settings.immediateShow) return void d.deferUpdate(); + prepocitat(); + d.update({ embeds: [embed] }); + }); + + collector.on("end", () => { + embed.description = `Skončilo ${formatter(konec)}`; + prepocitat(); + embed.fields = x5fxd55; + zprava.edit({ components: [], embeds: [embed] }); + }); } + } } }; diff --git a/start.sh b/start.sh index f757b5d..1524606 100644 --- a/start.sh +++ b/start.sh @@ -8,7 +8,7 @@ do git pull npm i --omit=dev tsc >/dev/null - unset $neco + unset neco else echo exit code byl $? novejcas=$(date +"%s") diff --git a/test/status.test.js b/test/status.test.js index 122b6c7..f8792c5 100644 --- a/test/status.test.js +++ b/test/status.test.js @@ -1,16 +1,16 @@ process.env.ignorePresence = "J"; const { statusOnFoun } = require("../out/utils/utils.js"); -const get = (a, b) => statusOnFoun(null, { status: a, clientStatus: b })[1]; +const stat = (a, b) => statusOnFoun(null, { status: a, clientStatus: b })[1]; test("Zobrazovani spravne statusu", () => { - expect(get("online", { desktop: "online" })).toBe("Online"); - expect(get("online", { mobile: "online" })).toBe("OnlinePhone"); - expect(get("online", { web: "idle", desktop: "online" })).toBe("Online"); - expect(get("online", { desktop: "online", mobile: "idle" })).toBe("Online"); - expect(get("online", { web: "idle", mobile: "online" })).toBe("OnlinePhone"); - expect(get("idle", { web: "idle" })).toBe("Idle"); - expect(get("dnd", { web: "dnd" })).toBe("DND"); - expect(get("offline", {})).toBe("Offline"); - expect(get("offline", null)).toBe("Offline"); + expect(stat("online", { desktop: "online" })).toBe("Online"); + expect(stat("online", { mobile: "online" })).toBe("OnlinePhone"); + expect(stat("online", { web: "idle", desktop: "online" })).toBe("Online"); + expect(stat("online", { desktop: "online", mobile: "idle" })).toBe("Online"); + expect(stat("online", { web: "idle", mobile: "online" })).toBe("OnlinePhone"); + expect(stat("idle", { web: "idle" })).toBe("Idle"); + expect(stat("dnd", { web: "dnd" })).toBe("DND"); + expect(stat("offline", {})).toBe("Offline"); + expect(stat("offline", null)).toBe("Offline"); });