2023-01-17 23:11:22 +01:00
|
|
|
import { GuildMember } from "discord.js";
|
2022-07-27 13:00:23 +02:00
|
|
|
|
2022-07-20 23:47:41 +02:00
|
|
|
/**
|
2022-07-22 11:46:47 +02:00
|
|
|
* Log module status.
|
2022-07-20 23:47:41 +02:00
|
|
|
* @param {string} name Module name
|
|
|
|
* @param {boolean} status Module status
|
|
|
|
* @returns String
|
|
|
|
*/
|
|
|
|
export const logStart = (name: string, status: boolean) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
// TODO Handle precision about the error if status is false
|
|
|
|
return `> ${name}\t${status === true ? "✅" : "❌"}`;
|
2022-07-20 23:47:41 +02:00
|
|
|
};
|
2022-07-22 11:46:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filename without path and extension.
|
|
|
|
* @param path __filename
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
export const getFilename = (path: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const path_list = path.split("/");
|
2022-07-22 11:46:47 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Check if filename exist
|
|
|
|
const filename_with_ext = path_list.pop();
|
|
|
|
if (filename_with_ext === undefined) {
|
|
|
|
throw new Error(`Filename error: don't exist in ${path}`);
|
|
|
|
}
|
2022-07-22 11:46:47 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return removeExtension(filename_with_ext);
|
2022-07-22 11:46:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove extension from a filename.
|
|
|
|
* @param filename string of the filename with an extension
|
|
|
|
* @returns string of the filename without an extension
|
|
|
|
*/
|
|
|
|
export const removeExtension = (filename: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const array = filename.split(".");
|
|
|
|
array.pop();
|
2022-07-22 11:46:47 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return array.join(".");
|
2022-07-22 11:46:47 +02:00
|
|
|
};
|
2022-07-27 13:00:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get extension from a filename.
|
|
|
|
* @param filename string of the filename
|
|
|
|
* @returns string of the extension if it exists
|
|
|
|
*/
|
|
|
|
export const getExtension = (filename: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
const array = filename.split(".");
|
2022-07-27 13:00:23 +02:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return array.pop();
|
2022-07-27 13:00:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define if a media is a media based on file extension.
|
|
|
|
* @param filename string of the filename
|
|
|
|
* @returns true is file is a media
|
|
|
|
*/
|
|
|
|
export const isImage = (filename: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
return Boolean(getExtension(filename)?.match(/jpg|jpeg|png|webp|gif/));
|
2022-07-27 13:00:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String with pseudo and nickname if available.
|
|
|
|
* @param member Member
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
export const userWithNickname = (member: GuildMember) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
if (!member) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (member.nickname) {
|
|
|
|
return `${member.nickname} (${member.user.tag})`;
|
|
|
|
} else {
|
|
|
|
return member.user.tag;
|
|
|
|
}
|
2022-07-27 13:00:23 +02:00
|
|
|
};
|
2023-01-17 12:15:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the text into backtick text, preserving mentions and links
|
|
|
|
* @param text Text
|
|
|
|
* @returns Formatted text
|
|
|
|
*/
|
|
|
|
export const cleanCodeBlock = (text: string) => {
|
2023-01-17 23:11:22 +01:00
|
|
|
text = `\`${text.trim()}\``;
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Keep mentions
|
|
|
|
text = text.replace(/(<@\d+>)/g, function (mention: string) {
|
|
|
|
return `\`${mention}\``;
|
|
|
|
});
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Keep links
|
|
|
|
text = text.replace(
|
|
|
|
/(http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)/g,
|
|
|
|
function (url: string) {
|
|
|
|
return `\`${url}\``;
|
|
|
|
}
|
|
|
|
);
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
// Fix issues
|
|
|
|
text = text.replace("``", "");
|
2023-01-17 12:15:14 +01:00
|
|
|
|
2023-01-17 23:11:22 +01:00
|
|
|
return text;
|
2023-01-17 12:15:14 +01:00
|
|
|
};
|
2023-02-17 23:06:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the emoji URL as png, only works with one-composed emoji code
|
|
|
|
* @param emoji Emoji
|
|
|
|
* @returns URL of emoji as png
|
|
|
|
*/
|
|
|
|
export const emojiPng = (emoji: string) =>
|
|
|
|
`https://cdn.jsdelivr.net/gh/twitter/twemoji/assets/72x72/${emoji
|
|
|
|
.codePointAt(0)
|
|
|
|
?.toString(16)}.png`;
|