From 55e2acd5f56506daa2e2bfb26354a71a203a4369 Mon Sep 17 00:00:00 2001 From: Histmy <59661489+Histmy@users.noreply.github.com> Date: Sun, 20 Aug 2023 18:09:32 +0200 Subject: [PATCH] nabidni, paging, rm traces of zaznamy.dlog --- package-lock.json | 4 +-- package.json | 2 +- src/app.ts | 41 +++++++++------------- src/modules/custom.ts | 25 ++++++++++---- src/modules/zbytek.ts | 53 ++++++++++++++++------------ src/utils/utils.ts | 80 ++++++++++++++++++++++++++++++++++++++++++- start.sh | 1 - 7 files changed, 149 insertions(+), 57 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27f11a5..c0bb2fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "denim_3001", - "version": "3001.52.1", + "version": "3001.53.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "denim_3001", - "version": "3001.52.1", + "version": "3001.53.0", "license": "ISC", "dependencies": { "@discordjs/voice": "^0.16.0", diff --git a/package.json b/package.json index 9d3368e..85352d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "denim_3001", - "version": "3001.52.1", + "version": "3001.53.0", "description": "Toto je velmi kvalitní bot.", "repository": { "url": "https://github.com/Histmy/Denim-Bot/" diff --git a/src/app.ts b/src/app.ts index 20f7dee..734e99c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ -import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, Client, CommandInteraction, ComponentType, GatewayIntentBits, Message, Partials } from "discord.js"; +import { ChannelType, Client, CommandInteraction, GatewayIntentBits, Message, Partials } from "discord.js"; import { readdirSync } from "fs"; import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, SRecord, SuperListenerFunkce, CClient, HelpServer, KomandRaw } from "./utils/types"; -import { adminLog, formatCas, oddiakritikovat, log, rand, prefix } from "./utils/utils.js"; +import { adminLog, formatCas, oddiakritikovat, log, rand, prefix, nabidni } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; @@ -290,31 +290,24 @@ client.on("messageCreate", async mes => { if (!slova.length) return void mes.channel.send("co to znamena ti gadzovko"); const nabidka = slova.sort().slice(0, 5); - const radek = new ActionRowBuilder(); - nabidka.forEach((cmd, i) => { - const nazev = cmd.length > 80 ? `${cmd.slice(0, 77)}...` : cmd; - radek.addComponents(new ButtonBuilder({ customId: `${i}`, label: nazev, style: ButtonStyle.Primary })); - }); - const zprava = await mes.channel.send({ content: "nemnel sy na misli:", components: [radek] }); - const collector = mes.channel.createMessageComponentCollector({ filter: i => i.message.id == zprava.id && mes.author.id == i.user.id, time: 18e4 }); - collector.on("collect", i => { - const lookupId = Number(i.customId); - if (isNaN(lookupId)) return void mes.channel.send("neco nefunguje idk"); - const komand = nabidka[lookupId]; - const cmdName = client.aliasy[komand] ?? komand; - const cmd = client.komandy[cmdName]; - radek.components.forEach(btn => btn.setDisabled()); - i.update({ content: `ok vole ${emouty.d3k}`, components: [radek] }); + nabidni({ + nabidka, + channel: mes.channel, + zpravec: { content: "nemnel sy na misli:" }, + onCollect: (i, lookupId, radek) => { + if (i.member?.user.id != mes.author.id) return; + const komand = nabidka[lookupId]; + const cmdName = client.aliasy[komand] ?? komand; + const cmd = client.komandy[cmdName]; + radek.components.forEach(btn => btn.setDisabled()); + i.update({ content: `ok vole ${emouty.d3k}`, components: [radek] }); - if (mes.channel.type == ChannelType.DM && cmd?.DMUnsafe) return void mes.channel.send("tuten komand bohuzel v sz nefunkuje"); + if (mes.channel.type == ChannelType.DM && cmd?.DMUnsafe) return void mes.channel.send("tuten komand bohuzel v sz nefunkuje"); - runKomand(mes, cmd, cmdName, celArgs); - }); - collector.on("end", c => { - if (c.size) return; - radek.components.forEach(btn => btn.setDisabled()); - zprava.edit({ content: "pozde", components: [radek] }); + runKomand(mes, cmd, cmdName, celArgs); + }, + konecnaZprava: "pozde" }); }); diff --git a/src/modules/custom.ts b/src/modules/custom.ts index 4791e61..63af466 100644 --- a/src/modules/custom.ts +++ b/src/modules/custom.ts @@ -1,7 +1,7 @@ import { CClient, HelpServer, Komand, KomandNaExport, Modul, SRecord } from "../utils/types"; import { join } from "path"; import { existsSync, readFileSync, writeFileSync } from "fs"; -import { oddiakritikovat, prefix } from "../utils/utils"; +import { oddiakritikovat, prefix, strankovani } from "../utils/utils"; import { APIEmbed } from "discord.js"; let client: CClient; @@ -251,24 +251,35 @@ const exp: Modul = { } }, - aliasy: (_, arg) => { + aliasy: (mes, arg) => { const name = arg.toLowerCase(); const cmdName = client.aliasy[name] ?? name; - const list: string[] = []; + let celkovadylka = 0; + const stranky: string[][] = []; for (const key in client.aliasy) { const val = client.aliasy[key]; - if (val == cmdName) list.push(key); + if (val != cmdName) continue; + + celkovadylka += key.length + 3; + if (stranky[Math.floor(celkovadylka / 1024)]?.length == 20) celkovadylka += 1024 - (celkovadylka % 1024); + const stranka = stranky[Math.floor(celkovadylka / 1024)] ??= []; + stranka.push(key); } - if (!list.length) return `"${name}" bud nen9 komand ani ajilas nebo nema ajilasi vubec`; + if (!celkovadylka) return `"${name}" bud nen9 komand ani ajilas nebo nema ajilasi vubec`; + const zacatekNazvu = `alijasy pro **${cmdName}**:`; const embed: APIEmbed = { color: 13697024, - fields: [{ name: `alijasy pro **${cmdName}**:`, value: `• ${list.join("\n• ")}` }] + fields: [{ name: zacatekNazvu, value: `• ${stranky[0].join("\n• ")}` }] }; - return { embeds: [embed] }; + if (celkovadylka <= 1024) return { embeds: [embed] }; + + embed.fields![0].name += ` (1/${stranky.length})`; + + strankovani(mes.channel, embed, zacatekNazvu, stranky); } }, diff --git a/src/modules/zbytek.ts b/src/modules/zbytek.ts index 1285b40..58c0db0 100644 --- a/src/modules/zbytek.ts +++ b/src/modules/zbytek.ts @@ -5,16 +5,13 @@ import { Client, Message, Presence, TextChannel, User } from "discord.js"; import { createServer } from "http"; import { Modul } from "../utils/types"; import fetch from "node-fetch"; -import { adminLog, formatCas, log, sendDM } from "../utils/utils"; +import { adminLog, formatCas, log, nabidni, sendDM } from "../utils/utils"; import { emouty } from "../utils/emotes"; -import { appendFileSync } from "fs"; const zpravy = new Map>(); const dobaOmezení = 20_000; let client: Client; -let restartPozadavky: { id: string, kdy: number; }[] = []; - const exp: Modul = { more_komandy: { @@ -48,22 +45,39 @@ const exp: Modul = { run: mes => { const sudo = mes.channel.id == process.env.adminChannel || mes.author.id == process.env.adminID; - restartPozadavky = restartPozadavky.filter(pozadavek => { - return pozadavek.kdy > Date.now() - 6e4; - }); - - if (!sudo) { - if (!restartPozadavky.find(el => el.id == mes.author.id)) restartPozadavky.push({ id: mes.author.id, kdy: Date.now() }); - if (restartPozadavky.length == 1) - return "jelikoz nesi hlava zpravne namazana musi se s tebou zhotnout jeste 2 hajzlaty"; - if (restartPozadavky.length == 2) - return `heste jeden`; + function udelat() { + getVoiceConnections().forEach(con => con.destroy()); + client.user?.setStatus("invisible"); + mes.react("👋") + .then(() => process.exit(69)); } - getVoiceConnections().forEach(con => con.destroy()); - client.user?.setStatus("invisible"); - mes.react("👋") - .then(() => process.exit(69)); + if (!sudo) { + const souhlasatori: string[] = [mes.author.id]; + + nabidni({ + channel: mes.channel, + nabidka: ["souhlasim"], + zpravec: { content: "jelikoz nesi hlava zpravne namazana musi se z tebou zhotnout jeste 2 hajzlaty do 60 se kund" }, + onCollect: i => { + if (souhlasatori.includes(i.member!.user.id)) return i.reply({ + ephemeral: true, + content: "ti us sy na to klyknul ti suraku" + }); + + souhlasatori.push(i.member!.user.id); + + if (souhlasatori.length == 3) udelat(); + else i.update({ content: "heste jeden" }); + }, + konecnaZprava: "restart se nekona, ste kundi so se neumy dohodnout", + timeout: 6e4, + stejne: true + }); + return; + } + + udelat(); } }, @@ -117,9 +131,6 @@ const exp: Modul = { // Nastavení clienta client = module.exports.client; - // Záznam - appendFileSync("zaznamy.dlog", `ziju${Date.now().toString(36)}\n`); - // OG onReady log("A jedeš!"); if (process.argv.length >= 4) adminLog(client, "pošel jsem"); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 4b42507..629e399 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,4 +1,4 @@ -import { ChannelType, Client, User } from "discord.js"; +import { APIEmbed, ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CacheType, ChannelType, Client, Collection, ComponentType, MessageCreateOptions, TextBasedChannel, User } from "discord.js"; import { existsSync } from "fs"; if (!existsSync("config.json")) throw new Error("config.json neexistuje"); @@ -97,3 +97,81 @@ export async function sendDM(user: User, txt: string) { export const rand = (max: number) => Math.floor(Math.random() * max); export const prefix = process.env.prefix || "more"; + +type Nastaveni = { + nabidka: (string | (({ val: string; } | { emoji: string; }) & { disabled?: boolean; }))[]; + channel: TextBasedChannel; + zpravec: MessageCreateOptions; + onCollect: (i: ButtonInteraction, id: number, radek: ActionRowBuilder) => void; + timeout?: number; +} & ({ + onEnd: (c: Collection>) => void; +} | { + konecnaZprava: string; + stejne?: boolean; +}); + +export async function nabidni(nastaveni: Nastaveni) { + const radek = new ActionRowBuilder(); + nastaveni.nabidka.forEach((moznost, i) => { + const component = new ButtonBuilder({ customId: `${i}`, style: ButtonStyle.Primary }); + + const ejtoString = typeof moznost == "string"; + + if (ejtoString || "val" in moznost) { + const value = ejtoString ? moznost : moznost.val; + const nazev = value.length > 80 ? `${value.slice(0, 77)}...` : value; + component.setLabel(nazev); + } else { + component.setEmoji(moznost.emoji); + } + if (!ejtoString && moznost.disabled) component.setDisabled(); + radek.addComponents(component); + }); + + const zprava = await nastaveni.channel.send({ ...nastaveni.zpravec, components: [radek] }); + const collector = nastaveni.channel.createMessageComponentCollector({ filter: i => i.message.id == zprava.id, time: nastaveni.timeout ?? 18e4 }); + + collector.on("collect", i => { + const lookupId = Number(i.customId); + if (isNaN(lookupId)) return void nastaveni.channel.send("neco nefunguje idk"); + + nastaveni.onCollect(i, lookupId, radek); + }); + + collector.on("end", c => { + if ("onEnd" in nastaveni) + nastaveni.onEnd(c); + else if (!c.size || nastaveni.stejne) { + radek.components.forEach(btn => btn.setDisabled()); + zprava.edit({ content: nastaveni.konecnaZprava, components: [radek] }); + } + }); +} + +export const strankovani = (channel: TextBasedChannel, embed: APIEmbed, zacatekNazvu: string, stranky: string[][]) => { + let aktualniStranka = 0; + nabidni({ + channel: channel, + nabidka: [{ emoji: "⬅️", disabled: true }, { emoji: "➡️" }], + zpravec: { embeds: [embed] }, + onCollect: (i, id, radek) => { + // Výpočet nový stánky + aktualniStranka += (!id) ? -1 : 1; + + // Úprava aktivnosti tlačítek + radek.components[0].setDisabled(!aktualniStranka); + radek.components[1].setDisabled(aktualniStranka + 1 == stranky.length); + + // Úprava embedu + embed.fields![0] = { + name: `${zacatekNazvu} (${aktualniStranka + 1}/${stranky.length})`, + value: `• ${stranky[aktualniStranka].join("\n• ")}` + }; + + // Odeslání + i.update({ embeds: [embed], components: [radek] }); + }, + onEnd: () => { } + }); +}; diff --git a/start.sh b/start.sh index 8dd4688..289c0d4 100644 --- a/start.sh +++ b/start.sh @@ -23,7 +23,6 @@ while true; do echo Poslo to dostatecne malo, restartuju else echo Poslo to moc, seru na to - echo osel$(date +"%s") >>zaznamy.dlog break fi i=$(($i + 1))