made utils file

This commit is contained in:
Aster Fialla
2026-02-21 23:05:15 -05:00
parent cc8cb6e925
commit 71803fd10c
2 changed files with 31 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ import {enums} from "./enums.js";
import {commands} from "./commands.js";
import {webhookHelper} from "./helpers/webhookHelper.js";
import * as env from 'dotenv';
import {utils} from "./helpers/utils";
env.config();
@@ -61,22 +62,14 @@ client.on(Events.Ready, () => {
let guildCount = 0;
client.on(Events.GuildCreate, () => {
guildCount++;
callback();
debouncePrintGuilds();
});
function printGuilds() {
console.log(`Serving ${client.guilds.size} guild(s)`);
}
const callback = Debounce(printGuilds, 2000);
function Debounce(func, delay) {
let timeout = null;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
}
const debouncePrintGuilds = utils.debounce(printGuilds, 2000);
try {
await client.login(token);

28
src/helpers/utils.js Normal file
View File

@@ -0,0 +1,28 @@
const u = {};
u.debounce = function(func, delay) {
let timeout = null;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), delay);
};
}
/**
* Checks if an uploaded picture is in the right format.
*
* @async
* @param {string} imageUrl - The url of the image
* @throws {Error} When loading the profile picture from a URL doesn't work, or it fails requirements.
*/
u.checkImageFormatValidity = async function (imageUrl) {
const acceptableImages = ['image/png', 'image/jpg', 'image/jpeg', 'image/webp'];
await fetch(imageUrl).then(r => r.blob()).then(blobFile => {
if (blobFile.size > 1000000 || !acceptableImages.includes(blobFile.type)) throw new Error(enums.err.PROPIC_FAILS_REQUIREMENTS);
}).catch((error) => {
throw new Error(`${enums.err.PROPIC_CANNOT_LOAD}: ${error.message}`);
});
}
export const utils = u;