All files / src/utils Tools.ts

19.23% Statements 5/26
0% Branches 0/50
0% Functions 0/5
19.23% Lines 5/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77          3x                                                                           3x             3x         3x                             3x            
import util from "node:util";
import { NestedLocalizationAdapter, TranslationFn } from "@necord/localization";
import { BaseMessageOptions, CommandInteraction, EmbedBuilder, MessageEditOptions } from "discord.js";
import ms from "parse-ms";
 
export const Timer = async (
	translate: NestedLocalizationAdapter | TranslationFn,
	type: "normal" | "details",
	number: number,
	translateInfo: string,
): Promise<string> => {
	const time = ms(number);
	const days =
		translate instanceof NestedLocalizationAdapter
			? (translate as NestedLocalizationAdapter).getTranslation("Tools/Tools:Timer:Days", translateInfo)
			: (translate as TranslationFn)("Tools/Tools:Timer:Days");
	const hours =
		translate instanceof NestedLocalizationAdapter
			? (translate as NestedLocalizationAdapter).getTranslation("Tools/Tools:Timer:Hours", translateInfo)
			: (translate as TranslationFn)("Tools/Tools:Timer:Hours");
	const minutes =
		translate instanceof NestedLocalizationAdapter
			? (translate as NestedLocalizationAdapter).getTranslation("Tools/Tools:Timer:Minutes", translateInfo)
			: (translate as TranslationFn)("Tools/Tools:Timer:Minutes");
	const seconds =
		translate instanceof NestedLocalizationAdapter
			? (translate as NestedLocalizationAdapter).getTranslation("Tools/Tools:Timer:Seconds", translateInfo)
			: (translate as TranslationFn)("Tools/Tools:Timer:Seconds");
	switch (type) {
		case "normal":
			return ` ${time.hours ? (time.hours > 10 ? time.hours : `0${time.hours}`) : ""}${time.hours ? ":" : ""}${
				time.minutes ? (time.minutes >= 10 ? time.minutes : `0${time.minutes}`) : "00"
			}:${time.seconds ? (time.seconds > 10 ? time.seconds : `0${time.seconds}`) : ""}`;
		case "details":
			return ` 
      ${days}${time.hours ? (time.days > 10 ? time.days : `0${time.days}`) : ""}${time.days ? ":" : ""}
      ${hours}${time.hours ? (time.hours > 10 ? time.hours : `0${time.hours}`) : ""}${time.hours ? ":" : ""}
      ${minutes}${time.minutes ? (time.minutes >= 10 ? time.minutes : `0${time.minutes}`) : "00"}
      ${seconds}${time.seconds ? (time.seconds > 10 ? time.seconds : `0${time.seconds}`) : ""}
      `;
	}
};
 
export const messageOptions = (content: string | EmbedBuilder | BaseMessageOptions): BaseMessageOptions => {
	const options: BaseMessageOptions | MessageEditOptions =
		typeof content === "string" ? { content } : content instanceof EmbedBuilder ? { embeds: [content] } : content;
 
	return options;
};
 
export const WAIT = async (time: number): Promise<void> => {
	const wait = util.promisify(setTimeout);
	return wait(time);
};
 
export const isValidURL = (string: string): URL | boolean => {
	const args = string.split(" ");
	let url: URL | boolean;
	for (const arg of args) {
		try {
			url = new URL(arg);
			url = url.protocol === "http:" || url.protocol === "https:";
			break;
		} catch (_) {
			url = false;
		}
	}
	return url;
};
 
export const formatArray = (array: string[]) => {
	return new Intl.ListFormat("pt-BR", {
		style: "short",
		type: "conjunction",
	}).format(array);
};