jkfdkjlfdslkjfdkjsdfjk ksd

This commit is contained in:
Histmy 2022-10-15 17:33:24 +02:00
parent 2e37399d86
commit 159bd31323
3 changed files with 89 additions and 81 deletions

View File

@ -1,7 +1,6 @@
// Modul na komand "anketa" // Modul na komand "anketa"
import { ActionRowBuilder, APIEmbed, ButtonBuilder, ButtonStyle, Message, MessageComponentInteraction } from "discord.js"; import { ActionRowBuilder, APIEmbed, APIEmbedField, ButtonBuilder, ButtonStyle, Message, MessageComponentInteraction } from "discord.js";
import { emouty } from "../utils/emotes";
import { Modul } from "../utils/types"; import { Modul } from "../utils/types";
import { formatter } from "../utils/utils"; import { formatter } from "../utils/utils";
@ -9,83 +8,92 @@ const exp: Modul = {
more_komandy: { more_komandy: {
anketa: { anketa: {
arg: "nastavení asi IDK", arg: "([nastaveni]) moznosti...",
run: async (mes, arg) => { run: async (mes, arg) => {
const args = arg.toLocaleLowerCase().split(" ");
const settings = { const settings = {
time: 60, time: 60,
immediateShow: true immediateShow: true,
listNames: false
}; };
switch (args[0]) { let huuuuuu = -1;
case "custom": if (arg.startsWith("[")) {
case "vlastni": huuuuuu = arg.indexOf("]");
return `tuto jeste neni implemenovany ${emouty.sjeta}`; const hovn = arg.slice(1, huuuuuu);
const f = hovn.split(" ");
case "start": for (const s of f) {
case "zapnout": { const t = Number(s);
const temp = args.slice(1).join(" "); if (!Number.isNaN(t)) settings.time = t;
const moznosti = temp.split(temp.indexOf("|") > -1 ? "|" : ",").reduce<string[]>((acc, c) => { else if (s == "jmena") settings.listNames = true;
if (c.length) acc.push(c.trim()); else if (s == "neprubezny") settings.immediateShow = false;
return acc; else return `"${s}" neznam znam jenom jmena a neprubezny`;
}, []);
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<ButtonBuilder>();
const odpovedi: Record<string, number> = {};
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;
} }
default:
return `takovi to "${args[0]}" sem moc nepochitil`;
} }
const moznosti = arg.slice(huuuuuu + 1).split(arg.includes("|") ? "|" : ",").reduce<string[]>((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<ButtonBuilder>();
const odpovedi: Record<string, number> = {};
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<string[]>((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] });
});
} }
} }
} }
}; };

View File

@ -8,7 +8,7 @@ do
git pull git pull
npm i --omit=dev npm i --omit=dev
tsc >/dev/null tsc >/dev/null
unset $neco unset neco
else else
echo exit code byl $? echo exit code byl $?
novejcas=$(date +"%s") novejcas=$(date +"%s")

View File

@ -1,16 +1,16 @@
process.env.ignorePresence = "J"; process.env.ignorePresence = "J";
const { statusOnFoun } = require("../out/utils/utils.js"); 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", () => { test("Zobrazovani spravne statusu", () => {
expect(get("online", { desktop: "online" })).toBe("Online"); expect(stat("online", { desktop: "online" })).toBe("Online");
expect(get("online", { mobile: "online" })).toBe("OnlinePhone"); expect(stat("online", { mobile: "online" })).toBe("OnlinePhone");
expect(get("online", { web: "idle", desktop: "online" })).toBe("Online"); expect(stat("online", { web: "idle", desktop: "online" })).toBe("Online");
expect(get("online", { desktop: "online", mobile: "idle" })).toBe("Online"); expect(stat("online", { desktop: "online", mobile: "idle" })).toBe("Online");
expect(get("online", { web: "idle", mobile: "online" })).toBe("OnlinePhone"); expect(stat("online", { web: "idle", mobile: "online" })).toBe("OnlinePhone");
expect(get("idle", { web: "idle" })).toBe("Idle"); expect(stat("idle", { web: "idle" })).toBe("Idle");
expect(get("dnd", { web: "dnd" })).toBe("DND"); expect(stat("dnd", { web: "dnd" })).toBe("DND");
expect(get("offline", {})).toBe("Offline"); expect(stat("offline", {})).toBe("Offline");
expect(get("offline", null)).toBe("Offline"); expect(stat("offline", null)).toBe("Offline");
}); });