summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
author8cy <[email protected]>2020-07-23 23:24:17 -0700
committer8cy <[email protected]>2020-07-23 23:24:17 -0700
commitbb511abc03bb66848947e37a999502b813c77269 (patch)
tree612c010fc8317e1cdf11471a18aad0270819d33e /src/utils
parentfix: if clear amount equal or over 100, round down to 99 (diff)
downloaddep-core-bb511abc03bb66848947e37a999502b813c77269.tar.xz
dep-core-bb511abc03bb66848947e37a999502b813c77269.zip
goodbye old uwufier :cry:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/Canvas.ts185
-rw-r--r--src/utils/Util.ts189
-rw-r--r--src/utils/gameDigHelper.ts47
-rw-r--r--src/utils/genCmdURL.ts1
-rw-r--r--src/utils/simpleFormat.ts9
-rw-r--r--src/utils/stripWebhookURL.ts12
-rw-r--r--src/utils/truncateText.ts6
-rw-r--r--src/utils/wait.ts6
-rw-r--r--src/utils/winPercentage.ts16
9 files changed, 0 insertions, 471 deletions
diff --git a/src/utils/Canvas.ts b/src/utils/Canvas.ts
deleted file mode 100644
index 23ae558..0000000
--- a/src/utils/Canvas.ts
+++ /dev/null
@@ -1,185 +0,0 @@
-// TODO: all this
-//@ts-nocheck
-import { createCanvas } from 'canvas'
-
-module.exports = class CanvasUtil {
- static greyscale(ctx, x, y, width, height) {
- const data = ctx.getImageData(x, y, width, height);
- for (let i = 0; i < data.data.length; i += 4) {
- const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
- data.data[i] = brightness;
- data.data[i + 1] = brightness;
- data.data[i + 2] = brightness;
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static invert(ctx, x, y, width, height) {
- const data = ctx.getImageData(x, y, width, height);
- for (let i = 0; i < data.data.length; i += 4) {
- data.data[i] = 255 - data.data[i];
- data.data[i + 1] = 255 - data.data[i + 1];
- data.data[i + 2] = 255 - data.data[i + 2];
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static silhouette(ctx, x, y, width, height) {
- const data = ctx.getImageData(x, y, width, height);
- for (let i = 0; i < data.data.length; i += 4) {
- data.data[i] = 0;
- data.data[i + 1] = 0;
- data.data[i + 2] = 0;
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static sepia(ctx, x, y, width, height) {
- const data = ctx.getImageData(x, y, width, height);
- for (let i = 0; i < data.data.length; i += 4) {
- const brightness = (0.34 * data.data[i]) + (0.5 * data.data[i + 1]) + (0.16 * data.data[i + 2]);
- data.data[i] = brightness + 100;
- data.data[i + 1] = brightness + 50;
- data.data[i + 2] = brightness;
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static contrast(ctx, x, y, width, height) {
- const data = ctx.getImageData(x, y, width, height);
- const factor = (259 / 100) + 1;
- const intercept = 128 * (1 - factor);
- for (let i = 0; i < data.data.length; i += 4) {
- data.data[i] = (data.data[i] * factor) + intercept;
- data.data[i + 1] = (data.data[i + 1] * factor) + intercept;
- data.data[i + 2] = (data.data[i + 2] * factor) + intercept;
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static distort(ctx, amplitude, x, y, width, height, strideLevel = 4) {
- const data = ctx.getImageData(x, y, width, height);
- const temp = ctx.getImageData(x, y, width, height);
- const stride = width * strideLevel;
- for (let i = 0; i < width; i++) {
- for (let j = 0; j < height; j++) {
- const xs = Math.round(amplitude * Math.sin(2 * Math.PI * 3 * (j / height)));
- const ys = Math.round(amplitude * Math.cos(2 * Math.PI * 3 * (i / width)));
- const dest = (j * stride) + (i * strideLevel);
- const src = ((j + ys) * stride) + ((i + xs) * strideLevel);
- data.data[dest] = temp.data[src];
- data.data[dest + 1] = temp.data[src + 1];
- data.data[dest + 2] = temp.data[src + 2];
- }
- }
- ctx.putImageData(data, x, y);
- return ctx;
- }
-
- static hasAlpha(image) {
- const canvas = createCanvas(image.width, image.height);
- const ctx = canvas.getContext('2d');
- ctx.drawImage(image, 0, 0);
- const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
- let hasAlphaPixels = false;
- for (let i = 3; i < data.data.length; i += 4) {
- if (data.data[i] < 255) {
- hasAlphaPixels = true;
- break;
- }
- }
- return hasAlphaPixels;
- }
-
- static drawImageWithTint(ctx, image, color, x, y, width, height) {
- const { fillStyle, globalAlpha } = ctx;
- ctx.fillStyle = color;
- ctx.drawImage(image, x, y, width, height);
- ctx.globalAlpha = 0.5;
- ctx.fillRect(x, y, width, height);
- ctx.fillStyle = fillStyle;
- ctx.globalAlpha = globalAlpha;
- }
-
- static shortenText(ctx, text, maxWidth) {
- let shorten = false;
- while (ctx.measureText(text).width > maxWidth) {
- if (!shorten) shorten = true;
- text = text.substr(0, text.length - 1);
- }
- return shorten ? `${text}...` : text;
- }
-
- static wrapText(ctx, text, maxWidth) {
- return new Promise(resolve => {
- if (ctx.measureText(text).width < maxWidth) return resolve([text]);
- if (ctx.measureText('W').width > maxWidth) return resolve(null);
- const words = text.split(' ');
- const lines = [];
- let line = '';
- while (words.length > 0) {
- let split = false;
- while (ctx.measureText(words[0]).width >= maxWidth) {
- const temp = words[0];
- words[0] = temp.slice(0, -1);
- if (split) {
- words[1] = `${temp.slice(-1)}${words[1]}`;
- } else {
- split = true;
- words.splice(1, 0, temp.slice(-1));
- }
- }
- if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) {
- line += `${words.shift()} `;
- } else {
- lines.push(line.trim());
- line = '';
- }
- if (words.length === 0) lines.push(line.trim());
- }
- return resolve(lines);
- });
- }
-
- static centerImage(base, data) {
- const dataRatio = data.width / data.height;
- const baseRatio = base.width / base.height;
- let { width, height } = data;
- let x = 0;
- let y = 0;
- if (baseRatio < dataRatio) {
- height = data.height;
- width = base.width * (height / base.height);
- x = (data.width - width) / 2;
- y = 0;
- } else if (baseRatio > dataRatio) {
- width = data.width;
- height = base.height * (width / base.width);
- x = 0;
- y = (data.height - height) / 2;
- }
- return { x, y, width, height };
- }
-
- static centerImagePart(data, maxWidth, maxHeight, widthOffset, heightOffest) {
- let { width, height } = data;
- if (width > maxWidth) {
- const ratio = maxWidth / width;
- width = maxWidth;
- height *= ratio;
- }
- if (height > maxHeight) {
- const ratio = maxHeight / height;
- height = maxHeight;
- width *= ratio;
- }
- const x = widthOffset + ((maxWidth / 2) - (width / 2));
- const y = heightOffest + ((maxHeight / 2) - (height / 2));
- return { x, y, width, height };
- }
-}; \ No newline at end of file
diff --git a/src/utils/Util.ts b/src/utils/Util.ts
deleted file mode 100644
index 4ad20a3..0000000
--- a/src/utils/Util.ts
+++ /dev/null
@@ -1,189 +0,0 @@
-import crypto from 'crypto';
-const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea', 'ya', 'hai', 'si', 'sí', 'oui', 'はい', 'correct'];
-const no = ['no', 'n', 'nah', 'nope', 'nop', 'iie', 'いいえ', 'non', 'fuck off'];
-
-module.exports = class Util {
- static delay(ms: number) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
-
- // TODO: maybe infer this
- static shuffle(array: any) {
- const arr = array.slice(0);
- for (let i = arr.length - 1; i >= 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- const temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- return arr;
- }
-
- static list(arr: any[], conj = 'and') {
- const len = arr.length;
- if (len === 0) return '';
- if (len === 1) return arr[0];
- return `${arr.slice(0, -1).join(', ')}${len > 1 ? `${len > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`;
- }
-
- static shorten(text: string, maxLen = 2000) {
- return text.length > maxLen ? `${text.substr(0, maxLen - 3)}...` : text;
- }
-
- static randomRange(min: number, max: number) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
-
- static trimArray(arr: string[], maxLen = 10) {
- if (arr.length > maxLen) {
- const len = arr.length - maxLen;
- arr = arr.slice(0, maxLen);
- arr.push(`${len} more...`);
- }
- return arr;
- }
-
- static removeDuplicates(arr: string | any[]) {
- if (arr.length === 0 || arr.length === 1) return arr;
- const newArr: any[] = [];
- for (let i = 0; i < arr.length; i++) {
- if (newArr.includes(arr[i])) continue;
- newArr.push(arr[i]);
- }
- return newArr;
- }
-
- static sortByName(arr: any[], prop: string | number) {
- return arr.sort((a, b) => {
- if (prop) return a[prop].toLowerCase() > b[prop].toLowerCase() ? 1 : -1;
- return a.toLowerCase() > b.toLowerCase() ? 1 : -1;
- });
- }
-
- static firstUpperCase(text: string, split = ' ') {
- return text.split(split).map(word => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(' ');
- }
-
- static formatNumber(number: string, minimumFractionDigits = 0) {
- return Number.parseFloat(number).toLocaleString(undefined, {
- minimumFractionDigits,
- maximumFractionDigits: 2
- });
- }
-
- //TODO: maybe infer this
- static base64(text: any, mode = 'encode') {
- if (mode === 'encode') return Buffer.from(text).toString('base64');
- if (mode === 'decode') return Buffer.from(text, 'base64').toString('utf8') || null;
- throw new TypeError(`${mode} is not a supported base64 mode.`);
- }
-
- //TODO: maybe infer this
- static hash(text: any, algorithm: any) {
- return crypto.createHash(algorithm).update(text).digest('hex');
- }
-
- //TODO: maybe infer this
- static streamToArray(stream: any) {
- if (!stream.readable) return Promise.resolve([]);
- return new Promise((resolve, reject) => {
- const array: any = [];
- function onData(data: any) {
- array.push(data);
- }
- function onEnd(error: any) {
- if (error) reject(error);
- else resolve(array);
- cleanup();
- }
- function onClose() {
- resolve(array);
- cleanup();
- }
- function cleanup() {
- stream.removeListener('data', onData);
- stream.removeListener('end', onEnd);
- stream.removeListener('error', onEnd);
- stream.removeListener('close', onClose);
- }
- stream.on('data', onData);
- stream.on('end', onEnd);
- stream.on('error', onEnd);
- stream.on('close', onClose);
- });
- }
-
- static percentColor(pct: number, percentColors: string | any[]) {
- let i = 1;
- for (i; i < percentColors.length - 1; i++) {
- if (pct < percentColors[i].pct) {
- break;
- }
- }
- const lower = percentColors[i - 1];
- const upper = percentColors[i];
- const range = upper.pct - lower.pct;
- const rangePct = (pct - lower.pct) / range;
- const pctLower = 1 - rangePct;
- const pctUpper = rangePct;
- const color = {
- r: Math.floor((lower.color.r * pctLower) + (upper.color.r * pctUpper)).toString(16).padStart(2, '0'),
- g: Math.floor((lower.color.g * pctLower) + (upper.color.g * pctUpper)).toString(16).padStart(2, '0'),
- b: Math.floor((lower.color.b * pctLower) + (upper.color.b * pctUpper)).toString(16).padStart(2, '0')
- };
- return `#${color.r}${color.g}${color.b}`;
- }
-
- static today(timeZone: number) {
- const now = new Date();
- now.setHours(0);
- now.setMinutes(0);
- now.setSeconds(0);
- now.setMilliseconds(0);
- if (timeZone) now.setUTCHours(now.getUTCHours() + timeZone);
- return now;
- }
-
- static tomorrow(timeZone: any) {
- const today = Util.today(timeZone);
- today.setDate(today.getDate() + 1);
- return today;
- }
-
- static embedURL(title: any, url: string, display: any) {
- return `[${title}](${url.replace(/\)/g, '%27')}${display ? ` "${display}"` : ''})`;
- }
-
- static async verify(channel, user, { time = 30000, extraYes = [], extraNo = [] } = {}) {
- const filter = res => {
- const value = res.content.toLowerCase();
- return (user ? res.author.id === user.id : true)
- && (yes.includes(value) || no.includes(value) || extraYes.includes(value) || extraNo.includes(value));
- };
- const verify = await channel.awaitMessages(filter, {
- max: 1,
- time
- });
- if (!verify.size) return 0;
- const choice = verify.first().content.toLowerCase();
- if (yes.includes(choice) || extraYes.includes(choice)) return true;
- if (no.includes(choice) || extraNo.includes(choice)) return false;
- return false;
- }
-
- static cleanAnilistHTML(html) {
- let clean = html
- .replace(/\r|\n|\f/g, '')
- .replace(/<br>/g, '\n')
- .replace(/&#039;/g, '\'')
- .replace(/&quot;/g, '"')
- .replace(/<\/?i>/g, '*')
- .replace(/<\/?b>/g, '**')
- .replace(/~!|!~/g, '||')
- .replace(/&mdash;/g, '—');
- if (clean.length > 2000) clean = `${clean.substr(0, 1995)}...`;
- const spoilers = (clean.match(/\|\|/g) || []).length;
- if (spoilers !== 0 && (spoilers && (spoilers % 2))) clean += '||';
- return clean;
- }
-}; \ No newline at end of file
diff --git a/src/utils/gameDigHelper.ts b/src/utils/gameDigHelper.ts
deleted file mode 100644
index 482c87a..0000000
--- a/src/utils/gameDigHelper.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-//@ts-ignore no types
-import ms from "ms";
-import { Util, MessageEmbed } from 'discord.js'
-
-/**
-* Quickly create an embed for a GameDig status using values found in all responses
-* @name gamedigHelper
-* @param {Object} res Result from GameDig
-* @returns {MessageEmbed}
-*/
-module.exports = (res: any) => {
- const playerCount = res.players.length
- const maxPlayers = res.maxPlayers
-
- const emb = new MessageEmbed()
- .setTitle(res.name)
- .setFooter(`Took ${ms(res.ping)} to complete.`)
- .addFields([
- {
- name: 'Connect',
- value: `${res.connect}`
- },
- {
- name: 'Online Players',
- value: `${playerCount}/${maxPlayers} (${Math.round((playerCount / maxPlayers) * 100)}%)`
- },
- {
- name: 'Map',
- value: Util.escapeMarkdown(res.map)
- },
- {
- name: 'Password Required',
- value: res.password ? 'Yes' : 'No'
- }
- ])
-
- const unconfirmedValues = new Map([
- [res.raw.secure, (secure: any) => emb.addField('Vac Secured', secure ? 'Yes' : 'No')],
- [res.raw.games, game => emb.addField('Game', Util.escapeMarkdown(game))]
- ])
-
- unconfirmedValues.forEach((val, key) => {
- if (typeof key !== 'undefined') val(key)
- })
-
- return emb
-} \ No newline at end of file
diff --git a/src/utils/genCmdURL.ts b/src/utils/genCmdURL.ts
deleted file mode 100644
index 0519de0..0000000
--- a/src/utils/genCmdURL.ts
+++ /dev/null
@@ -1 +0,0 @@
-module.exports = (cmd: { group: { name: string; }; name: any; }) => `/commands/${cmd.group.name.toLowerCase().replace(/\s/g, "-")}/${cmd.name}`; \ No newline at end of file
diff --git a/src/utils/simpleFormat.ts b/src/utils/simpleFormat.ts
deleted file mode 100644
index 4564778..0000000
--- a/src/utils/simpleFormat.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-/**
-* @name simpleFormat
-* @param {number|string} value Value to format
-* @returns {number} A number in fixed-point notation up to 2 decimal points
-*/
-module.exports = (value: string) => {
- const result = parseFloat(parseFloat(value).toFixed(2));
- return result;
-}; \ No newline at end of file
diff --git a/src/utils/stripWebhookURL.ts b/src/utils/stripWebhookURL.ts
deleted file mode 100644
index 73a227c..0000000
--- a/src/utils/stripWebhookURL.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-/**
-* Gets the ID and token for a webhook from a webhook URL from the Discord client
-* @param {string} url URL for the webhook from the Discord client
-* @returns {Object} Object with the webhook ID and token
-*/
-module.exports = (url: string) => {
- const regex = /https:\/\/discordapp\.com\/api\/webhooks\/(\d{1,})\/([\w-_]{1,})/;
- const matches = regex.exec(url);
-
- //@ts-ignore dont care if null
- return { id: matches[1], token: matches[2] };
-}; \ No newline at end of file
diff --git a/src/utils/truncateText.ts b/src/utils/truncateText.ts
deleted file mode 100644
index f2fa7d0..0000000
--- a/src/utils/truncateText.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-/**
-* @param {string} string String to truncate
-* @param {number} [number=2048] Number to truncate to
-* @returns {string} Truncated string or original if string was short enough to begin with
-*/
-module.exports = (string: any, number = 2048) => (string.length > number ? `${string.substring(0, number - 3)}...` : string); \ No newline at end of file
diff --git a/src/utils/wait.ts b/src/utils/wait.ts
deleted file mode 100644
index 4af65f1..0000000
--- a/src/utils/wait.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-/**
-* @name wait
-* @param {number} delay Delay in milliseconds to wait for
-* @returns {Promise<resolve>}
-*/
-module.exports = (delay: number) => new Promise(resolve => setTimeout(resolve, delay)); \ No newline at end of file
diff --git a/src/utils/winPercentage.ts b/src/utils/winPercentage.ts
deleted file mode 100644
index 627dab8..0000000
--- a/src/utils/winPercentage.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import config from '../config.json'
-/**
-* @name winPercentage
-* @param {number} multiplier Multiplier to calculate win percentage for
-* @param {User} user User to calculate win percentage for
-* @returns {number} User balance
-*/
-module.exports = (multiplier: any, user: any) => {
- // Load the default setting
- //let { houseEdgePercentage } = config;
-
- // If they're a crown supporter, set it to the patron percentage
- //if (config.patrons[user.id] && config.patrons[user.id].crown === true) houseEdgePercentage = 0;
-
- //return (100 - houseEdgePercentage) / multiplier;
-}; \ No newline at end of file