betterfetch, absolutní genialita

schvalně mě zajímá, jak moc k hovnu to stejně bude
This commit is contained in:
Histmy 2025-08-13 13:07:08 +02:00
parent edbd1f14b4
commit 0eff5800a4
Signed by: Histmy
GPG Key ID: AC2E43C463D8F329
9 changed files with 34 additions and 18 deletions

View File

@ -5,7 +5,7 @@ import { join } from "path";
import { Priority, novejPlay } from "../utils/voice"; import { Priority, novejPlay } from "../utils/voice";
import { lidiCoMajDenimPremium } from "../utils/denim-Spravce"; import { lidiCoMajDenimPremium } from "../utils/denim-Spravce";
import { exec } from "child_process"; import { exec } from "child_process";
import { log, messageReply } from "../utils/utils"; import { betterFetch, log, messageReply } from "../utils/utils";
const kmenovaCesta = join(__dirname, `../../zvuky/priVstupu`); const kmenovaCesta = join(__dirname, `../../zvuky/priVstupu`);
const formaty = ["mp3", "wav", "ogg"]; const formaty = ["mp3", "wav", "ogg"];
@ -34,7 +34,7 @@ const exp: Modul = {
const typ = soubor.name.slice(-3); const typ = soubor.name.slice(-3);
const docasnaCesta = `${kmenovaCesta}/temp${Math.round(Math.random() * 1000)}.${typ}`; const docasnaCesta = `${kmenovaCesta}/temp${Math.round(Math.random() * 1000)}.${typ}`;
const odpoved = await fetch(soubor.url).then(r => r.arrayBuffer()); const odpoved = await betterFetch(soubor.url).then(r => r.arrayBuffer());
writeFileSync(docasnaCesta, new DataView(odpoved)); writeFileSync(docasnaCesta, new DataView(odpoved));
// Zjistit running-time // Zjistit running-time

View File

@ -1,6 +1,6 @@
import { TextChannel } from "discord.js"; import { TextChannel } from "discord.js";
import { CClient, Modul } from "../utils/types"; import { CClient, Modul } from "../utils/types";
import { log, sendWithoutReply } from "../utils/utils"; import { betterFetch, log, sendWithoutReply } from "../utils/utils";
let client: CClient; let client: CClient;
@ -10,7 +10,7 @@ let posledniNazev: string;
let posledniOdkaz: string; let posledniOdkaz: string;
async function zkontrolovatNovinky() { async function zkontrolovatNovinky() {
const stranka = await fetch("https://histmy.eu/BUM/clanky") const stranka = await betterFetch("https://histmy.eu/BUM/clanky")
.then(r => r.text()) .then(r => r.text())
.catch(e => log("chyba pri accessovani BUM stránky", e)); .catch(e => log("chyba pri accessovani BUM stránky", e));

View File

@ -6,7 +6,7 @@ import { search, validate, video_basic_info } from "play-dl";
import ytdlko from "@distube/ytdl-core"; import ytdlko from "@distube/ytdl-core";
import { emouty } from "../utils/emotes"; import { emouty } from "../utils/emotes";
import { Modul } from "../utils/types"; import { Modul } from "../utils/types";
import { adminLog, log, messageReply } from "../utils/utils"; import { adminLog, betterFetch, log, messageReply } from "../utils/utils";
import { getConn, Hratelny, novejJoin, novejPlay, Priority, stopPlayer } from "../utils/voice"; import { getConn, Hratelny, novejJoin, novejPlay, Priority, stopPlayer } from "../utils/voice";
import { array, record, safeParse, string } from "valibot"; import { array, record, safeParse, string } from "valibot";
@ -216,7 +216,7 @@ const exp: Modul = {
const nameSplited = kju[0].name.split("-"); const nameSplited = kju[0].name.split("-");
const query = nameSplited[1] ?? kju[0].name; const query = nameSplited[1] ?? kju[0].name;
const res = await fetch(`https://search.karaoketexty.cz/index.php?q=${query}`) const res = await betterFetch(`https://search.karaoketexty.cz/index.php?q=${query}`)
.then(res => res.text()) .then(res => res.text())
.catch(log); .catch(log);
@ -238,7 +238,7 @@ const exp: Modul = {
if (!vysledek) return "KaraokeTexty nic nenasli"; if (!vysledek) return "KaraokeTexty nic nenasli";
const res2 = await fetch(`https://www.karaoketexty.cz/texty-pisni/h/h-${vysledek.id}`) const res2 = await betterFetch(`https://www.karaoketexty.cz/texty-pisni/h/h-${vysledek.id}`)
.then(res => res.text()) .then(res => res.text())
.catch(log); .catch(log);

View File

@ -1,7 +1,7 @@
// Trekování statusů všech lidí o kterých bot ví // Trekování statusů všech lidí o kterých bot ví
import { Client, Guild, Presence } from "discord.js"; import { Client, Guild, Presence } from "discord.js";
import { FakePresence, Modul, SRecord, StatusyINaFounu, UserChange, ZmenovejObjekt } from "../utils/types"; import { FakePresence, Modul, SRecord, StatusyINaFounu, UserChange, ZmenovejObjekt } from "../utils/types";
import { adminLog, log } from "../utils/utils"; import { adminLog, betterFetch, log } from "../utils/utils";
const statusy = { Offline: "0", Online: "1", Idle: "2", DND: "3", OnlinePhone: "11", IdlePhone: "12", DNDPhone: "13" }; const statusy = { Offline: "0", Online: "1", Idle: "2", DND: "3", OnlinePhone: "11", IdlePhone: "12", DNDPhone: "13" };
const prepSend = (zmeny: UserChange[]) => { const prepSend = (zmeny: UserChange[]) => {
@ -25,7 +25,7 @@ const prepSend = (zmeny: UserChange[]) => {
const poslatData = (data: SRecord<unknown>) => { const poslatData = (data: SRecord<unknown>) => {
data.pwd = process.env.statPass; data.pwd = process.env.statPass;
fetch("http://deadfish.cz:4629/endpoint-pro-denimka/", { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) betterFetch("http://deadfish.cz:4629/endpoint-pro-denimka/", { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) })
.catch(err => { .catch(err => {
log("err:", err); log("err:", err);
const client: Client<boolean> = module.exports.client; const client: Client<boolean> = module.exports.client;

View File

@ -1,6 +1,6 @@
import { GuildScheduledEventEntityType, GuildScheduledEventPrivacyLevel, TextChannel } from "discord.js"; import { GuildScheduledEventEntityType, GuildScheduledEventPrivacyLevel, TextChannel } from "discord.js";
import { CClient, Modul } from "../utils/types"; import { CClient, Modul } from "../utils/types";
import { log, sendWithoutReply } from "../utils/utils"; import { betterFetch, log, sendWithoutReply } from "../utils/utils";
import { RawData, WebSocket } from "ws"; import { RawData, WebSocket } from "ws";
import { emouty } from "../utils/emotes"; import { emouty } from "../utils/emotes";
@ -50,7 +50,7 @@ async function udelejOneplayRquest(cesta: string, body: string) {
throw new Error("token more"); throw new Error("token more");
} }
const res = await fetch(`https://http.cms.jyxo.cz/api/v3/${cesta}`, { const res = await betterFetch(`https://http.cms.jyxo.cz/api/v3/${cesta}`, {
method: "POST", method: "POST",
body, body,
headers: { Authorization: token } headers: { Authorization: token }

View File

@ -4,7 +4,7 @@ import { getVoiceConnections } from "@discordjs/voice";
import { Client } from "discord.js"; import { Client } from "discord.js";
import { createServer } from "http"; import { createServer } from "http";
import { Modul } from "../utils/types"; import { Modul } from "../utils/types";
import { adminLog, formatCas, log, nabidni, messageReply, sendDM, sendWithoutReply } from "../utils/utils"; import { adminLog, formatCas, log, nabidni, messageReply, sendDM, sendWithoutReply, betterFetch } from "../utils/utils";
import { emouty } from "../utils/emotes"; import { emouty } from "../utils/emotes";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
@ -28,7 +28,7 @@ const exp: Modul = {
run: async mes => { run: async mes => {
const sudo = mes.channel.id == process.env.adminChannel || mes.author.id == process.env.adminID; const sudo = mes.channel.id == process.env.adminChannel || mes.author.id == process.env.adminID;
if (!sudo) { if (!sudo) {
const verze = await fetch("https://gitea.deadfish.cz/Histmy/Denim-Bot/raw/branch/main/package.json") const verze = await betterFetch("https://gitea.deadfish.cz/Histmy/Denim-Bot/raw/branch/main/package.json")
.then(r => r.json()) .then(r => r.json())
.then(d => d.version) .then(d => d.version)
.catch(e => { .catch(e => {

View File

@ -1,5 +1,5 @@
import { array, safeParse, string } from "valibot"; import { array, safeParse, string } from "valibot";
import { adminLog, log } from "./utils"; import { adminLog, betterFetch, log } from "./utils";
import { Client } from "discord.js"; import { Client } from "discord.js";
let client: Client; let client: Client;
@ -20,7 +20,7 @@ function naplanovat() {
} }
async function sync() { async function sync() {
const res = await fetch("https://util.deadfish.cz/denim+/subscriptions.json") const res = await betterFetch("https://util.deadfish.cz/denim+/subscriptions.json")
.then(r => r.json()) .then(r => r.json())
.catch(e => log("chyba pri ziskavani denim- predplatitelu", e)); .catch(e => log("chyba pri ziskavani denim- predplatitelu", e));

View File

@ -5,7 +5,7 @@ import { SRecord } from "./types";
import { join } from "path"; import { join } from "path";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { TypedEmitter } from "tiny-typed-emitter"; import { TypedEmitter } from "tiny-typed-emitter";
import { log } from "./utils"; import { betterFetch, log } from "./utils";
const basePath = join(__dirname, "../../res/sachy"); const basePath = join(__dirname, "../../res/sachy");
const sessions: SRecord<Session> = {}; const sessions: SRecord<Session> = {};
@ -106,13 +106,13 @@ function loginErr(res: ServerResponse) {
async function overeni(url: URL, session: Session, res: ServerResponse) { async function overeni(url: URL, session: Session, res: ServerResponse) {
const code = url.searchParams.get("code"); const code = url.searchParams.get("code");
const data = await fetch("https://discord.com/api/v10/oauth2/token", { method: "post", body: `client_id=1064269692165963826&client_secret=8HHKODmb1HTLWoJGVyoinSU_9323dEla&grant_type=authorization_code&code=${code}&redirect_uri=http%3A%2F%2F${encodeURIComponent(sachyDomena)}%2Foauth`, headers: { "Content-type": "application/x-www-form-urlencoded" } }) const data = await betterFetch("https://discord.com/api/v10/oauth2/token", { method: "post", body: `client_id=1064269692165963826&client_secret=8HHKODmb1HTLWoJGVyoinSU_9323dEla&grant_type=authorization_code&code=${code}&redirect_uri=http%3A%2F%2F${encodeURIComponent(sachyDomena)}%2Foauth`, headers: { "Content-type": "application/x-www-form-urlencoded" } })
.then(r => r.json()) .then(r => r.json())
.catch(err => log("sachy jednotka:", err)); .catch(err => log("sachy jednotka:", err));
if (!data?.access_token) return loginErr(res); if (!data?.access_token) return loginErr(res);
const uzivatel = await fetch("https://discord.com/api/v10/users/@me", { headers: { authorization: `Bearer ${data.access_token}` } }) const uzivatel = await betterFetch("https://discord.com/api/v10/users/@me", { headers: { authorization: `Bearer ${data.access_token}` } })
.then(r => r.json()) .then(r => r.json())
.catch(err => log("sachy dva:", err)); .catch(err => log("sachy dva:", err));

View File

@ -285,3 +285,19 @@ export const areStatusesSame = (object1?: ClientPresenceStatusData | null, objec
} }
return true; return true;
}; };
export async function betterFetch(url: string, options?: RequestInit) {
const maxRetry = 5;
const retryWait = 3_000;
for (let currentAttempt = 1; currentAttempt < maxRetry; currentAttempt++) {
try {
return await fetch(url, options);
} catch (err) {
log(`Chyba při stahování ${url}, pokus číslo ${currentAttempt}, čekám ${retryWait / 1000} sekund`);
await new Promise(r => setTimeout(r, retryWait));
}
}
throw new Error(`Nepodařilo se stáhnout ${url} ani na ${maxRetry} pokusů`);
}