fixed missing util functionality and wrong import

This commit is contained in:
Erik 2022-01-11 22:34:52 +02:00
parent c8a03e929f
commit 75c2c3630a
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589
2 changed files with 58 additions and 2 deletions

View File

@ -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;

View File

@ -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');