diff --git a/src/bot.js b/src/bot.js index 3a89b31..97c431b 100644 --- a/src/bot.js +++ b/src/bot.js @@ -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); diff --git a/src/helpers/utils.js b/src/helpers/utils.js new file mode 100644 index 0000000..a38d2be --- /dev/null +++ b/src/helpers/utils.js @@ -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;