From 75c2c3630ab21cb9e1b9693e574b4772e87a26e2 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 11 Jan 2022 22:34:52 +0200 Subject: [PATCH] fixed missing util functionality and wrong import --- src/Util.js | 58 ++++++++++++++++++++++++- src/middleware/shard/ShardingManager.js | 2 +- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Util.js b/src/Util.js index 68557bb..e304ebf 100644 --- a/src/Util.js +++ b/src/Util.js @@ -4,6 +4,8 @@ const fs = require('fs'); const fetch = require('node-fetch'); const { Util: DiscordUtil } = require('discord.js'); +const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k); + class Util { constructor() { @@ -68,6 +70,9 @@ class Util { return DiscordUtil.escapeMarkdown(text, options); } + /** + * Markdown formatting characters + */ static get formattingPatterns() { return [ ['\\*{1,3}([^*]*)\\*{1,3}', '$1'], @@ -77,6 +82,12 @@ class Util { ]; } + /** + * Strips markdown from given text + * @static + * @param {string} content + * @returns {string} + */ static removeMarkdown(content) { if (!content) throw new Error('Missing content'); this.formattingPatterns.forEach(([pattern, replacer]) => { @@ -87,7 +98,6 @@ class Util { /** * Sanitise user given regex; escapes unauthorised characters - * * @static * @param {string} input * @param {string[]} [allowed=['?', '\\', '(', ')', '|']] @@ -100,10 +110,18 @@ class Util { return input.replace(reg, '\\$&'); } + /** + * RegEx characters + */ static get regChars() { return ['.', '+', '*', '?', '\\[', '\\]', '^', '$', '(', ')', '{', '}', '|', '\\\\', '-']; } + /** + * Escape RegEx characters, prefix them with a backslash + * @param {string} string String representation of the regular expression + * @returns {string} Sanitised RegEx string + */ static escapeRegex(string) { if(typeof string !== 'string') { throw new Error("Invalid type sent to escapeRegex."); @@ -114,6 +132,11 @@ class Util { .replace(/-/gu, '\\x2d'); } + /** + * Convert seconds to a human readable string representation + * @param {int} seconds + * @returns {string} + */ static duration(seconds) { const { plural } = this; let s = 0, @@ -142,6 +165,39 @@ class Util { return moment().format("YYYY-MM-DD HH:mm:ss"); } + static makePlainError(err) { + return { + name: err.name, + message: err.message, + stack: err.stack + }; + } + + static mergeDefault(def, given) { + if (!given) return def; + for (const key in def) { + if (!has(given, key) || given[key] === undefined) { + given[key] = def[key]; + } else if (given[key] === Object(given[key])) { + given[key] = Util.mergeDefault(def[key], given[key]); + } + } + return given; + } + + //Shard Managing + static fetchRecommendedShards(token, guildsPerShard = 1000) { + if (!token) throw new Error("[util] Token missing."); + return fetch("https://discord.com/api/v7/gateway/bot", { + method: 'GET', + headers: { Authorization: `Bot ${token.replace(/^Bot\s*/iu, '')}` } + }).then((res) => { + if (res.ok) return res.json(); + throw res; + }) + .then((data) => data.shards * (1000 / guildsPerShard)); + } + } module.exports = Util; \ No newline at end of file diff --git a/src/middleware/shard/ShardingManager.js b/src/middleware/shard/ShardingManager.js index b23992c..a4ba79d 100644 --- a/src/middleware/shard/ShardingManager.js +++ b/src/middleware/shard/ShardingManager.js @@ -2,7 +2,7 @@ const { EventEmitter } = require('events'); const { Collection } = require('@discordjs/collection'); -const { Util } = require('discord.js'); +const Util = require('./../../Util.js'); const fs = require('fs'); const path = require('path');