2021-06-13 21:47:04 +05:00
|
|
|
import { StageChannel, VoiceChannel } from "discord.js";
|
2021-06-13 17:03:20 +05:00
|
|
|
import { TimeData } from "../types/types";
|
|
|
|
|
|
|
|
class Util {
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Utils
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
throw new Error("Cannot instantiate static class");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates duration string
|
|
|
|
* @param {object} durObj The duration object
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-06-22 15:24:05 +05:00
|
|
|
static durationString(durObj: Record<string, number>) {
|
2021-06-13 17:03:20 +05:00
|
|
|
return Object.values(durObj)
|
|
|
|
.map((m) => (isNaN(m) ? 0 : m))
|
|
|
|
.join(":");
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Parses milliseconds to consumable time object
|
|
|
|
* @param {number} milliseconds The time in ms
|
|
|
|
* @returns {TimeData}
|
|
|
|
*/
|
2021-06-13 17:03:20 +05:00
|
|
|
static parseMS(milliseconds: number) {
|
|
|
|
const round = milliseconds > 0 ? Math.floor : Math.ceil;
|
|
|
|
|
|
|
|
return {
|
|
|
|
days: round(milliseconds / 86400000),
|
|
|
|
hours: round(milliseconds / 3600000) % 24,
|
|
|
|
minutes: round(milliseconds / 60000) % 60,
|
|
|
|
seconds: round(milliseconds / 1000) % 60
|
|
|
|
} as TimeData;
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Builds time code
|
|
|
|
* @param {TimeData} duration The duration object
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-06-13 17:03:20 +05:00
|
|
|
static buildTimeCode(duration: TimeData) {
|
|
|
|
const items = Object.keys(duration);
|
|
|
|
const required = ["days", "hours", "minutes", "seconds"];
|
|
|
|
|
|
|
|
const parsed = items.filter((x) => required.includes(x)).map((m) => (duration[m as keyof TimeData] > 0 ? duration[m as keyof TimeData] : ""));
|
|
|
|
const final = parsed
|
|
|
|
.filter((x) => !!x)
|
|
|
|
.map((x) => x.toString().padStart(2, "0"))
|
|
|
|
.join(":");
|
|
|
|
return final.length <= 3 ? `0:${final.padStart(2, "0") || 0}` : final;
|
|
|
|
}
|
2021-06-13 17:43:36 +05:00
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Picks last item of the given array
|
|
|
|
* @param {any[]} arr The array
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2021-06-22 15:24:05 +05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-06-13 17:43:36 +05:00
|
|
|
static last<T = any>(arr: T[]): T {
|
|
|
|
if (!Array.isArray(arr)) return;
|
|
|
|
return arr[arr.length - 1];
|
|
|
|
}
|
2021-06-13 21:47:04 +05:00
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Checks if the voice channel is empty
|
|
|
|
* @param {VoiceChannel|StageChannel} channel The voice channel
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2021-06-13 21:47:04 +05:00
|
|
|
static isVoiceEmpty(channel: VoiceChannel | StageChannel) {
|
|
|
|
return channel.members.filter((member) => !member.user.bot).size === 0;
|
|
|
|
}
|
2021-06-14 18:50:36 +05:00
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Safer require
|
|
|
|
* @param {string} id Node require id
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
2021-06-14 18:50:36 +05:00
|
|
|
static require(id: string) {
|
|
|
|
try {
|
|
|
|
return require(id);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 18:22:03 +05:00
|
|
|
|
2021-06-20 19:22:09 +05:00
|
|
|
/**
|
|
|
|
* Asynchronous timeout
|
|
|
|
* @param {number} time The time in ms to wait
|
|
|
|
* @returns {Promise<unknown>}
|
|
|
|
*/
|
2021-06-17 18:22:03 +05:00
|
|
|
static wait(time: number) {
|
2021-07-03 17:46:18 +05:00
|
|
|
return new Promise((r) => setTimeout(r, time));
|
2021-06-17 18:22:03 +05:00
|
|
|
}
|
2021-06-22 15:24:05 +05:00
|
|
|
|
|
|
|
static get noop() {
|
2021-06-23 00:11:50 +05:00
|
|
|
return () => {}; // eslint-disable-line @typescript-eslint/no-empty-function
|
2021-06-22 15:24:05 +05:00
|
|
|
}
|
2021-06-13 17:03:20 +05:00
|
|
|
}
|
|
|
|
|
2021-06-23 00:11:50 +05:00
|
|
|
export { Util };
|