Přidání ignorace dalšího druhu zprávy při záloze

This commit is contained in:
Histmy 2024-11-16 11:01:49 +01:00
parent 7bd90284a5
commit 17d0f71427

View File

@ -1,171 +1,171 @@
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
import { APIEmbed, CommandInteractionOption, Message, TextBasedChannel } from "discord.js"; import { APIEmbed, CommandInteractionOption, Message, TextBasedChannel } from "discord.js";
import { CClient, Modul } from "../utils/types"; import { CClient, Modul } from "../utils/types";
import { bazenek, log, strankovani } from "../utils/utils"; import { bazenek, log, strankovani } from "../utils/utils";
const zpracovatZpravu = (mes: Message) => new Promise<void>(async (res, rej) => { const zpracovatZpravu = (mes: Message) => new Promise<void>(async (res, rej) => {
if ([6, 7, 8, 9, 10, 11, 18, 23].includes(mes.type)) return res(); if ([6, 7, 8, 9, 10, 11, 18, 23, 46].includes(mes.type)) return res();
if (mes.type != 0 && mes.type != 20 && mes.type != 19) throw new Error("Neznámej message type " + mes.type.toString()); if (mes.type != 0 && mes.type != 20 && mes.type != 19) throw new Error("Neznamej message type " + mes.type.toString());
const reference = mes.reference?.messageId; const reference = mes.reference?.messageId;
const attachments: { name: string; file: string; }[] = []; const attachments: { name: string; file: string; }[] = [];
if (mes.attachments.size != 0) { if (mes.attachments.size != 0) {
for (const [, attachment] of mes.attachments) { for (const [, attachment] of mes.attachments) {
attachments.push({ name: attachment.name, file: attachment.id }); attachments.push({ name: attachment.name, file: attachment.id });
} }
} }
const sql = `INSERT INTO Zpravy (ID, Content, Author ${reference ? ", Reference" : ""} , Attachments) VALUES (${mes.id}, ?, ${mes.author.id} ${reference ? `,${reference}` : ""} , ?)`; const sql = `INSERT INTO Zpravy (ID, Content, Author ${reference ? ", Reference" : ""} , Attachments) VALUES (${mes.id}, ?, ${mes.author.id} ${reference ? `,${reference}` : ""} , ?)`;
bazenek.query({ sql, values: [mes.content, JSON.stringify(attachments)] }, err => { bazenek.query({ sql, values: [mes.content, JSON.stringify(attachments)] }, err => {
if (err) rej(err); if (err) rej(err);
res(); res();
}); });
}); });
function utect(string: string) { function utect(string: string) {
return string return string
.replace(/~/g, "~~") .replace(/~/g, "~~")
.replace(/%/g, "~%") .replace(/%/g, "~%")
.replace(/_/g, "~_"); .replace(/_/g, "~_");
} }
const porovnat = (filtr: CommandInteractionOption): string[] => { const porovnat = (filtr: CommandInteractionOption): string[] => {
switch (filtr.name) { switch (filtr.name) {
case "kontent": return [`Content LIKE ? ESCAPE "~"`, `%${utect(`${filtr.value}`)}%`]; case "kontent": return [`Content LIKE ? ESCAPE "~"`, `%${utect(`${filtr.value}`)}%`];
case "regex": return [`Content REGEXP ?`, `${filtr.value}`]; case "regex": return [`Content REGEXP ?`, `${filtr.value}`];
case "uzivatel": return [`Author=?`, `${filtr.user!.id}`]; case "uzivatel": return [`Author=?`, `${filtr.user!.id}`];
case "od": case "od":
case "do": { case "do": {
const datum = new Date(filtr.value as string); const datum = new Date(filtr.value as string);
const cislo = (Number(datum) - 1420070400000) * 4194304; const cislo = (Number(datum) - 1420070400000) * 4194304;
return [`ID ${filtr.name == "od" ? ">" : "<"} ${cislo}`]; return [`ID ${filtr.name == "od" ? ">" : "<"} ${cislo}`];
} }
case "obrasek": return [`${filtr.value ? "" : " NOT "}Attachments REGEXP "\.(png|jpe?g|gif|webp)"`]; case "obrasek": return [`${filtr.value ? "" : " NOT "}Attachments REGEXP "\.(png|jpe?g|gif|webp)"`];
case "obrazekOdkaz": return [`${filtr.value ? "" : " NOT "}Content REGEXP "\.(png|jpe?g|gif|webp)"`]; case "obrazekOdkaz": return [`${filtr.value ? "" : " NOT "}Content REGEXP "\.(png|jpe?g|gif|webp)"`];
case "link": return [`${filtr.value ? "" : " NOT "}Content REGEXP "https?://"`]; case "link": return [`${filtr.value ? "" : " NOT "}Content REGEXP "https?://"`];
case "ma_soubor": return [`${filtr.value ? "" : " NOT "}Attachments != "[]"`]; case "ma_soubor": return [`${filtr.value ? "" : " NOT "}Attachments != "[]"`];
case "nazev_souboru": return [`Attachments LIKE ? ESCAPE "~"`, `%${utect(`${filtr.value}`)}%`]; case "nazev_souboru": return [`Attachments LIKE ? ESCAPE "~"`, `%${utect(`${filtr.value}`)}%`];
} }
return [""]; return [""];
}; };
const exp: Modul = { const exp: Modul = {
more_komandy: { more_komandy: {
vyhledej: { vyhledej: {
slashRun: async int => { slashRun: async int => {
const moznosti = int.options; const moznosti = int.options;
const filtry = moznosti.data; const filtry = moznosti.data;
if (!filtry.length) return "co takhle tam alespon jeden filtr"; if (!filtry.length) return "co takhle tam alespon jeden filtr";
// Nelgel kombinace a tak // Nelgel kombinace a tak
if (moznosti.get("kontent") && moznosti.get("regex")) return "kontent a regex nejde kombynovat"; if (moznosti.get("kontent") && moznosti.get("regex")) return "kontent a regex nejde kombynovat";
const od = moznosti.get("od"); const od = moznosti.get("od");
if (od && isNaN(Number(new Date(od.value as string)))) return "od neny platni datum"; if (od && isNaN(Number(new Date(od.value as string)))) return "od neny platni datum";
const dod = moznosti.get("do"); const dod = moznosti.get("do");
if (dod && isNaN(Number(new Date(dod.value as string)))) return "do neny platni datum"; if (dod && isNaN(Number(new Date(dod.value as string)))) return "do neny platni datum";
const parsedFiltry: string[] = []; const parsedFiltry: string[] = [];
const mista: string[] = []; const mista: string[] = [];
let seradit = "DESC"; let seradit = "DESC";
for (const filtr of filtry) { for (const filtr of filtry) {
if (filtr.name != "seradit") { if (filtr.name != "seradit") {
const [parsed, misto] = porovnat(filtr); const [parsed, misto] = porovnat(filtr);
parsedFiltry.push(parsed); parsedFiltry.push(parsed);
if (misto) mista.push(misto); if (misto) mista.push(misto);
} else { } else {
seradit = filtr.value as string; seradit = filtr.value as string;
} }
} }
const odpoved = await int.reply("hledam"); const odpoved = await int.reply("hledam");
const sql = `SELECT CONVERT(ID, CHAR) as idcko, SUBSTRING(Content, 1, 20) AS kontent FROM Zpravy WHERE ${parsedFiltry.join(" AND ")} ORDER BY ID ${seradit}`; const sql = `SELECT CONVERT(ID, CHAR) as idcko, SUBSTRING(Content, 1, 20) AS kontent FROM Zpravy WHERE ${parsedFiltry.join(" AND ")} ORDER BY ID ${seradit}`;
bazenek.query({ sql, values: mista }, (err, res?: { idcko: string; kontent: string; }[]) => { bazenek.query({ sql, values: mista }, (err, res?: { idcko: string; kontent: string; }[]) => {
if (!int.channel?.isSendable()) return int.reply("PIČOVINA"); if (!int.channel?.isSendable()) return int.reply("PIČOVINA");
if (err) return odpoved.edit(`chyba: ${err}`); if (err) return odpoved.edit(`chyba: ${err}`);
if (!res || !res.length) return odpoved.edit("nic takoviho sem nenasel"); if (!res || !res.length) return odpoved.edit("nic takoviho sem nenasel");
odpoved.edit("nasel sem tuto:"); odpoved.edit("nasel sem tuto:");
let celkovadylka = 0; let celkovadylka = 0;
const stranky: string[][] = [[]]; const stranky: string[][] = [[]];
let aktualniStranka = 0; let aktualniStranka = 0;
for (const row of res) { for (const row of res) {
const vlozka = `[${row.kontent}] https://discord.com/channels/555779161067749446/555779161067749448/${row.idcko}`; const vlozka = `[${row.kontent}] https://discord.com/channels/555779161067749446/555779161067749448/${row.idcko}`;
const dylka = vlozka.length + 3; const dylka = vlozka.length + 3;
celkovadylka += dylka; celkovadylka += dylka;
if (celkovadylka > 1024 || stranky[aktualniStranka].length == 20) { if (celkovadylka > 1024 || stranky[aktualniStranka].length == 20) {
aktualniStranka++; aktualniStranka++;
celkovadylka = dylka; celkovadylka = dylka;
} }
const stranka = stranky[aktualniStranka] ??= []; const stranka = stranky[aktualniStranka] ??= [];
stranka.push(vlozka); stranka.push(vlozka);
} }
const zacatekNazvu = "visltki"; const zacatekNazvu = "visltki";
const embed: APIEmbed = { const embed: APIEmbed = {
color: Math.random() * 16777215, color: Math.random() * 16777215,
fields: [{ name: zacatekNazvu, value: `${stranky[0].join("\n• ")}` }] fields: [{ name: zacatekNazvu, value: `${stranky[0].join("\n• ")}` }]
}; };
if (stranky.length == 1) return int.channel?.send({ embeds: [embed] }); if (stranky.length == 1) return int.channel?.send({ embeds: [embed] });
embed.fields![0].name += ` (1/${stranky.length})`; embed.fields![0].name += ` (1/${stranky.length})`;
strankovani(int.channel!, embed, zacatekNazvu, stranky); strankovani(int.channel!, embed, zacatekNazvu, stranky);
}); });
} }
} }
}, },
on_ready: () => { on_ready: () => {
const client = module.exports.client as CClient; const client = module.exports.client as CClient;
if (process.env.noGeneralSync) return; if (process.env.noGeneralSync) return;
client.channels.fetch("555779161067749448") client.channels.fetch("555779161067749448")
.then(async ch => { .then(async ch => {
const channel = ch as TextBasedChannel; const channel = ch as TextBasedChannel;
let posledni: string | undefined; let posledni: string | undefined;
while (true) { while (true) {
const msgs = await (channel.messages.fetch(posledni ? { before: posledni } : undefined)); const msgs = await (channel.messages.fetch(posledni ? { before: posledni } : undefined));
for (const [, mes] of msgs) { for (const [, mes] of msgs) {
try { try {
await zpracovatZpravu(mes); await zpracovatZpravu(mes);
} catch (e) { } catch (e) {
if (!(e instanceof Error && e.message.includes("ER_DUP_ENTRY"))) if (!(e instanceof Error && e.message.includes("ER_DUP_ENTRY")))
log(e); log(e);
log("general sync ukoncen"); log("general sync ukoncen");
return; return;
} }
} }
posledni = msgs.last()!.id; posledni = msgs.last()!.id;
// Radši // Radši
await new Promise(res => setTimeout(res, 500)); await new Promise(res => setTimeout(res, 500));
} }
}); });
}, },
on_messageCreate: mes => { on_messageCreate: mes => {
if (!process.env.noGeneralSync && mes.channelId == "555779161067749448") if (!process.env.noGeneralSync && mes.channelId == "555779161067749448")
zpracovatZpravu(mes); zpracovatZpravu(mes);
} }
}; };
module.exports = exp; module.exports = exp;