forked from PluralFlux/PluralFlux
made utils file
This commit is contained in:
13
src/bot.js
13
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);
|
||||
|
||||
28
src/helpers/utils.js
Normal file
28
src/helpers/utils.js
Normal 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;
|
||||
Reference in New Issue
Block a user