cleanup code
All checks were successful
Lint and Format Check / lint-and-format (pull_request) Successful in 24s

This commit is contained in:
Mylloon 2024-09-18 18:06:26 +02:00
parent a6b52c9f78
commit 504961d16e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
14 changed files with 62 additions and 57 deletions

View file

@ -1,5 +1,5 @@
import { readdir } from "fs/promises"; import { readdir } from "fs/promises";
import { removeExtension } from "../utils/misc"; import { removeExtension, splitFilenameExtensions } from "../utils/misc";
import { import {
ChatInputCommandInteraction, ChatInputCommandInteraction,
Client, Client,
@ -11,7 +11,7 @@ import { getLocale } from "../utils/locales";
export default async (client: Client) => { export default async (client: Client) => {
// Dossier des buttons // Dossier des buttons
const buttons_categories = (await readdir(__dirname)).filter( const buttons_categories = (await readdir(__dirname)).filter(
(element) => !element.endsWith(".js") && !element.endsWith(".ts"), (element) => splitFilenameExtensions(element)?.ext === undefined,
); );
await Promise.all( await Promise.all(

View file

@ -4,7 +4,6 @@ import {
ButtonStyle, ButtonStyle,
Client, Client,
MessageComponentInteraction, MessageComponentInteraction,
User,
} from "discord.js"; } from "discord.js";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
@ -18,11 +17,11 @@ export default {
}, },
interaction: async (interaction: MessageComponentInteraction, client: Client) => { interaction: async (interaction: MessageComponentInteraction, client: Client) => {
const loc = getLocale(client, interaction.locale); const loc = getLocale(client, interaction.locale);
const embed_desc = interaction.message.embeds.at(0)?.description as string; const embed_desc = interaction.message.embeds.at(0)?.description;
// Retrieve Pages // Retrieve Pages
const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc!)?.[0]);
let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc)?.[0]); let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc!)?.[0]);
if (page + 1 > pageMax) { if (page + 1 > pageMax) {
page = 1; page = 1;
} else { } else {
@ -30,8 +29,8 @@ export default {
} }
// Retrieve user // Retrieve user
const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc!)?.[0];
const user = client.users.cache.get(userId) as User; const user = client.users.cache.get(userId!)!;
// Fetch list // Fetch list
const list = await embedListReminders( const list = await embedListReminders(

View file

@ -4,7 +4,6 @@ import {
ButtonStyle, ButtonStyle,
Client, Client,
MessageComponentInteraction, MessageComponentInteraction,
User,
} from "discord.js"; } from "discord.js";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
@ -18,11 +17,11 @@ export default {
}, },
interaction: async (interaction: MessageComponentInteraction, client: Client) => { interaction: async (interaction: MessageComponentInteraction, client: Client) => {
const loc = getLocale(client, interaction.locale); const loc = getLocale(client, interaction.locale);
const embed_desc = interaction.message.embeds.at(0)?.description as string; const embed_desc = interaction.message.embeds.at(0)?.description;
// Retrieve Pages // Retrieve Pages
const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc!)?.[0]);
let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc)?.[0]); let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc!)?.[0]);
if (page - 1 === 0) { if (page - 1 === 0) {
page = pageMax; page = pageMax;
} else { } else {
@ -30,8 +29,8 @@ export default {
} }
// Retrieve user // Retrieve user
const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc!)?.[0];
const user = client.users.cache.get(userId) as User; const user = client.users.cache.get(userId!)!;
// Fetch list // Fetch list
const list = await embedListReminders( const list = await embedListReminders(

View file

@ -19,11 +19,11 @@ export default {
}, },
interaction: async (interaction: MessageComponentInteraction, client: Client) => { interaction: async (interaction: MessageComponentInteraction, client: Client) => {
const loc = getLocale(client, interaction.locale); const loc = getLocale(client, interaction.locale);
const embed_desc = interaction.message.embeds.at(0)?.author?.name as string; const embed_desc = interaction.message.embeds.at(0)?.author?.name;
// Retrieve Pages // Retrieve Pages
const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc!)?.[0]);
let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc)?.[0]); let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc!)?.[0]);
if (page + 1 > pageMax) { if (page + 1 > pageMax) {
page = 1; page = 1;
} else { } else {

View file

@ -2,14 +2,14 @@ import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9"; import { Routes } from "discord-api-types/v9";
import { Client } from "discord.js"; import { Client } from "discord.js";
import { readdir } from "fs/promises"; import { readdir } from "fs/promises";
import { removeExtension } from "../utils/misc"; import { removeExtension, splitFilenameExtensions } from "../utils/misc";
/** Load all the commands */ /** Load all the commands */
export default async (client: Client) => { export default async (client: Client) => {
const rest = new REST({ version: "10" }).setToken(client.token!); const rest = new REST({ version: "10" }).setToken(client.token!);
const command_categories = (await readdir(__dirname)).filter( const command_categories = (await readdir(__dirname)).filter(
(element) => !element.endsWith(".js") && !element.endsWith(".ts"), (element) => splitFilenameExtensions(element)?.ext === undefined,
); );
const commands = ( const commands = (

View file

@ -1,5 +1,4 @@
import { SlashCommandBuilder } from "@discordjs/builders"; import { SlashCommandBuilder } from "@discordjs/builders";
import { Locale } from "discord-api-types/v9";
import { ChatInputCommandInteraction, Client, Colors, EmbedBuilder } from "discord.js"; import { ChatInputCommandInteraction, Client, Colors, EmbedBuilder } from "discord.js";
import "../../modules/string"; import "../../modules/string";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
@ -93,7 +92,7 @@ export default {
.setDescription( .setDescription(
// Loads the description // Loads the description
// according to the user's locals // according to the user's locals
command.data.description_localizations?.[interaction.locale as Locale] ?? command.data.description_localizations?.[interaction.locale] ??
command.data.description, command.data.description,
), ),
], ],

View file

@ -1,5 +1,5 @@
import { SlashCommandBuilder } from "@discordjs/builders"; import { SlashCommandBuilder } from "@discordjs/builders";
import { Player, useMainPlayer, useQueue } from "discord-player"; import { useMainPlayer, useQueue } from "discord-player";
import { ChatInputCommandInteraction, Client, EmbedBuilder } from "discord.js"; import { ChatInputCommandInteraction, Client, EmbedBuilder } from "discord.js";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
@ -81,7 +81,7 @@ export default {
let data = null; let data = null;
await interaction.deferReply(); await interaction.deferReply();
const player = useMainPlayer() as Player; const player = useMainPlayer();
const queue = useQueue(interaction.guildId!); const queue = useQueue(interaction.guildId!);
if (request) { if (request) {
if ( if (

View file

@ -1,12 +1,10 @@
import { SlashCommandBuilder } from "@discordjs/builders"; import { SlashCommandBuilder } from "@discordjs/builders";
import { Player, SearchResult, useMainPlayer, useQueue } from "discord-player"; import { SearchResult, useMainPlayer, useQueue } from "discord-player";
import { import {
AutocompleteInteraction, AutocompleteInteraction,
ChatInputCommandInteraction, ChatInputCommandInteraction,
Client, Client,
EmbedBuilder, EmbedBuilder,
GuildResolvable,
VoiceBasedChannel,
} from "discord.js"; } from "discord.js";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
import { Metadata } from "../../utils/metadata"; import { Metadata } from "../../utils/metadata";
@ -71,7 +69,7 @@ export default {
loc_default?.get(`c_${filename}_opt1_name`) as string, loc_default?.get(`c_${filename}_opt1_name`) as string,
); );
const player = useMainPlayer() as Player; const player = useMainPlayer();
if (!query) { if (!query) {
// Now playing // Now playing
@ -97,7 +95,7 @@ export default {
return await interaction.reply({ embeds: [embed] }); return await interaction.reply({ embeds: [embed] });
} }
const queue = player.nodes.create(interaction.guild as GuildResolvable, { const queue = player.nodes.create(interaction.guild!, {
defaultFFmpegFilters: ["silenceremove"], defaultFFmpegFilters: ["silenceremove"],
metadata: { metadata: {
channel: interaction.channel, channel: interaction.channel,
@ -106,7 +104,7 @@ export default {
// Verify vc connection // Verify vc connection
try { try {
if (!queue.connection) await queue.connect(member.voice.channel as VoiceBasedChannel); if (!queue.connection) await queue.connect(member.voice.channel!);
} catch { } catch {
queue.delete(); queue.delete();
return await interaction.reply({ return await interaction.reply({
@ -155,7 +153,7 @@ export default {
const loc_default = interaction.client.locales.get(interaction.client.config.default_lang); const loc_default = interaction.client.locales.get(interaction.client.config.default_lang);
const filename = getFilename(__filename); const filename = getFilename(__filename);
const player = useMainPlayer() as Player; const player = useMainPlayer();
const query = interaction.options.getString( const query = interaction.options.getString(
loc_default?.get(`c_${filename}_opt1_name`) as string, loc_default?.get(`c_${filename}_opt1_name`) as string,
true, true,

View file

@ -153,7 +153,7 @@ export default {
case loc_default?.get(`c_${filename}_sub3_name`)?.toLowerCase(): { case loc_default?.get(`c_${filename}_sub3_name`)?.toLowerCase(): {
const id = interaction.options.getNumber( const id = interaction.options.getNumber(
loc_default?.get(`c_${filename}_sub3_opt1_name`) as string, loc_default?.get(`c_${filename}_sub3_opt1_name`) as string,
) as number; )!;
const track = queue.removeTrack(id - 1); const track = queue.removeTrack(id - 1);

View file

@ -1,8 +1,7 @@
import { SlashCommandBuilder } from "@discordjs/builders"; import { SlashCommandBuilder } from "@discordjs/builders";
import { Player, useMainPlayer } from "discord-player"; import { useMainPlayer } from "discord-player";
import { ChatInputCommandInteraction, Client, GuildResolvable } from "discord.js"; import { ChatInputCommandInteraction, Client } from "discord.js";
import { getLocale, getLocalizations } from "../../utils/locales"; import { getLocale, getLocalizations } from "../../utils/locales";
import { Metadata } from "../../utils/metadata";
import { getFilename } from "../../utils/misc"; import { getFilename } from "../../utils/misc";
export default { export default {
@ -25,11 +24,11 @@ export default {
interaction: async (interaction: ChatInputCommandInteraction, client: Client) => { interaction: async (interaction: ChatInputCommandInteraction, client: Client) => {
const loc = getLocale(client, interaction.locale); const loc = getLocale(client, interaction.locale);
const player = useMainPlayer() as Player; const player = useMainPlayer();
const queue = player.nodes.create(interaction.guild as GuildResolvable, { const queue = player.nodes.create(interaction.guild!, {
metadata: { metadata: {
channel: interaction.channel, channel: interaction.channel,
} as Metadata, },
}); });
if (!(queue.connection || queue.node.isPlaying())) { if (!(queue.connection || queue.node.isPlaying())) {

View file

@ -6,7 +6,7 @@ import { splitFilenameExtensions } from "../utils/misc";
/** Load all the events */ /** Load all the events */
export default async (client: Client, isDev: boolean) => { export default async (client: Client, isDev: boolean) => {
const events_categories = (await readdir(__dirname)).filter( const events_categories = (await readdir(__dirname)).filter(
(element) => !element.endsWith(".js") && !element.endsWith(".ts"), (element) => splitFilenameExtensions(element)?.ext === undefined,
); );
const player = useMainPlayer(); const player = useMainPlayer();
@ -45,7 +45,7 @@ export default async (client: Client, isDev: boolean) => {
); );
// Remove extension // Remove extension
const { file: event_type, ext } = splitFilenameExtensions(event_file); const { file: event_type, ext } = splitFilenameExtensions(event_file)!;
if (!(ext === "js" || ext === "ts")) { if (!(ext === "js" || ext === "ts")) {
throw `Unknown file in ${event_category}: ${event_file}`; throw `Unknown file in ${event_category}: ${event_file}`;
} }

View file

@ -1,4 +1,4 @@
import { Client, EmbedBuilder, GuildMember, Message, TextBasedChannel } from "discord.js"; import { Client, EmbedBuilder, Message, TextBasedChannel } from "discord.js";
import { getLocale } from "../../utils/locales"; import { getLocale } from "../../utils/locales";
import { isImage, userWithNickname } from "../../utils/misc"; import { isImage, userWithNickname } from "../../utils/misc";
import { showDate } from "../../utils/time"; import { showDate } from "../../utils/time";
@ -146,7 +146,7 @@ export default async (message: Message, client: Client) => {
if (message.author === quoted_post?.author) { if (message.author === quoted_post?.author) {
author += " & Citateur"; author += " & Citateur";
} else { } else {
footer += `\nCité par ${userWithNickname(message.member as GuildMember) ?? "?"} le ${showDate( footer += `\nCité par ${userWithNickname(message.member!) ?? "?"} le ${showDate(
client.config.default_lang, client.config.default_lang,
loc, loc,
message.createdAt, message.createdAt,

View file

@ -34,10 +34,26 @@ export const getFilename = (path: string) => {
* @returns Object with filename and extension splitted * @returns Object with filename and extension splitted
*/ */
export const splitFilenameExtensions = (filename: string) => { export const splitFilenameExtensions = (filename: string) => {
const array = filename.split("."); if (filename.length === 0) {
const ext = array.pop(); return undefined;
}
return { file: array.join("."), ext }; // Check if the filename starts with a dot and has no other dots
if (filename.startsWith(".") && filename.indexOf(".", 1) === -1) {
return { file: filename, ext: undefined };
}
const lastDotIndex = filename.lastIndexOf(".");
// If there's no dot or the dot is at the start, treat the whole string as the filename
if (lastDotIndex <= 0) {
return { file: filename, ext: undefined };
}
const file = filename.slice(0, lastDotIndex);
const ext = filename.slice(lastDotIndex + 1);
return { file, ext };
}; };
/** /**
@ -46,16 +62,7 @@ export const splitFilenameExtensions = (filename: string) => {
* @returns string of the filename without an extension * @returns string of the filename without an extension
*/ */
export const removeExtension = (filename: string) => { export const removeExtension = (filename: string) => {
return splitFilenameExtensions(filename).file; return splitFilenameExtensions(filename)!.file;
};
/**
* Get extension from a filename
* @param filename string of the filename
* @returns string of the extension if it exists
*/
export const getExtension = (filename: string) => {
return splitFilenameExtensions(filename).ext;
}; };
/** /**
@ -64,7 +71,11 @@ export const getExtension = (filename: string) => {
* @returns true is file is a media * @returns true is file is a media
*/ */
export const isImage = (filename: string) => { export const isImage = (filename: string) => {
return Boolean(getExtension(filename)?.match(/jpg|jpeg|png|webp|gif/)); return Boolean(
splitFilenameExtensions(filename)
?.ext?.toLowerCase()
.match(/jpg|jpeg|png|webp|gif/),
);
}; };
/** /**

View file

@ -1,5 +1,5 @@
import { EmbedBuilder } from "@discordjs/builders"; import { EmbedBuilder } from "@discordjs/builders";
import { GuildQueue, QueueRepeatMode, Track } from "discord-player"; import { GuildQueue, QueueRepeatMode } from "discord-player";
import { Client } from "discord.js"; import { Client } from "discord.js";
import { getLocale } from "./locales"; import { getLocale } from "./locales";
import { generateTrustedToken, YoutubeiExtractor } from "discord-player-youtubei"; import { generateTrustedToken, YoutubeiExtractor } from "discord-player-youtubei";
@ -15,7 +15,7 @@ export const embedListQueue = (
const tracks = queue.tracks.toArray(); const tracks = queue.tracks.toArray();
// Add the current song at the top of the list // Add the current song at the top of the list
tracks.unshift(queue.history.currentTrack as Track); tracks.unshift(queue.history.currentTrack!);
// Limit of discord is 25 // Limit of discord is 25
const limit_fields = 25; const limit_fields = 25;