diff --git a/src/buttons/loader.ts b/src/buttons/loader.ts index 4db3d9f..c93897c 100644 --- a/src/buttons/loader.ts +++ b/src/buttons/loader.ts @@ -1,5 +1,5 @@ import { readdir } from "fs/promises"; -import { removeExtension } from "../utils/misc"; +import { removeExtension, splitFilenameExtensions } from "../utils/misc"; import { ChatInputCommandInteraction, Client, @@ -11,7 +11,7 @@ import { getLocale } from "../utils/locales"; export default async (client: Client) => { // Dossier des buttons const buttons_categories = (await readdir(__dirname)).filter( - (element) => !element.endsWith(".js") && !element.endsWith(".ts"), + (element) => splitFilenameExtensions(element)?.ext === undefined, ); await Promise.all( diff --git a/src/buttons/misc/reminderList-next.ts b/src/buttons/misc/reminderList-next.ts index 60a870b..077cce1 100644 --- a/src/buttons/misc/reminderList-next.ts +++ b/src/buttons/misc/reminderList-next.ts @@ -4,7 +4,6 @@ import { ButtonStyle, Client, MessageComponentInteraction, - User, } from "discord.js"; import { v4 as uuidv4 } from "uuid"; import { getLocale } from "../../utils/locales"; @@ -18,11 +17,11 @@ export default { }, interaction: async (interaction: MessageComponentInteraction, client: Client) => { 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 - const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); - let page = Number(/(?!• \s+)\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]); if (page + 1 > pageMax) { page = 1; } else { @@ -30,8 +29,8 @@ export default { } // Retrieve user - const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; - const user = client.users.cache.get(userId) as User; + const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc!)?.[0]; + const user = client.users.cache.get(userId!)!; // Fetch list const list = await embedListReminders( diff --git a/src/buttons/misc/reminderList-prec.ts b/src/buttons/misc/reminderList-prec.ts index dd093e9..984527a 100644 --- a/src/buttons/misc/reminderList-prec.ts +++ b/src/buttons/misc/reminderList-prec.ts @@ -4,7 +4,6 @@ import { ButtonStyle, Client, MessageComponentInteraction, - User, } from "discord.js"; import { v4 as uuidv4 } from "uuid"; import { getLocale } from "../../utils/locales"; @@ -18,11 +17,11 @@ export default { }, interaction: async (interaction: MessageComponentInteraction, client: Client) => { 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 - const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); - let page = Number(/(?!• \s+)\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]); if (page - 1 === 0) { page = pageMax; } else { @@ -30,8 +29,8 @@ export default { } // Retrieve user - const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; - const user = client.users.cache.get(userId) as User; + const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc!)?.[0]; + const user = client.users.cache.get(userId!)!; // Fetch list const list = await embedListReminders( diff --git a/src/buttons/music/queueList-next.ts b/src/buttons/music/queueList-next.ts index a160587..2ed0a53 100644 --- a/src/buttons/music/queueList-next.ts +++ b/src/buttons/music/queueList-next.ts @@ -19,11 +19,11 @@ export default { }, interaction: async (interaction: MessageComponentInteraction, client: Client) => { 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 - const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); - let page = Number(/(?!• \s+)\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]); if (page + 1 > pageMax) { page = 1; } else { diff --git a/src/commands/loader.ts b/src/commands/loader.ts index 33e17aa..419bef4 100644 --- a/src/commands/loader.ts +++ b/src/commands/loader.ts @@ -2,14 +2,14 @@ import { REST } from "@discordjs/rest"; import { Routes } from "discord-api-types/v9"; import { Client } from "discord.js"; import { readdir } from "fs/promises"; -import { removeExtension } from "../utils/misc"; +import { removeExtension, splitFilenameExtensions } from "../utils/misc"; /** Load all the commands */ export default async (client: Client) => { const rest = new REST({ version: "10" }).setToken(client.token!); const command_categories = (await readdir(__dirname)).filter( - (element) => !element.endsWith(".js") && !element.endsWith(".ts"), + (element) => splitFilenameExtensions(element)?.ext === undefined, ); const commands = ( diff --git a/src/commands/misc/help.ts b/src/commands/misc/help.ts index bd1238b..ce9ebac 100644 --- a/src/commands/misc/help.ts +++ b/src/commands/misc/help.ts @@ -1,5 +1,4 @@ import { SlashCommandBuilder } from "@discordjs/builders"; -import { Locale } from "discord-api-types/v9"; import { ChatInputCommandInteraction, Client, Colors, EmbedBuilder } from "discord.js"; import "../../modules/string"; import { getLocale, getLocalizations } from "../../utils/locales"; @@ -93,7 +92,7 @@ export default { .setDescription( // Loads the description // according to the user's locals - command.data.description_localizations?.[interaction.locale as Locale] ?? + command.data.description_localizations?.[interaction.locale] ?? command.data.description, ), ], diff --git a/src/commands/music/lyrics.ts b/src/commands/music/lyrics.ts index a0b34d5..27088c2 100644 --- a/src/commands/music/lyrics.ts +++ b/src/commands/music/lyrics.ts @@ -1,5 +1,5 @@ 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 { getLocale, getLocalizations } from "../../utils/locales"; import { getFilename } from "../../utils/misc"; @@ -81,7 +81,7 @@ export default { let data = null; await interaction.deferReply(); - const player = useMainPlayer() as Player; + const player = useMainPlayer(); const queue = useQueue(interaction.guildId!); if (request) { if ( diff --git a/src/commands/music/play.ts b/src/commands/music/play.ts index bda4646..ed5ab35 100644 --- a/src/commands/music/play.ts +++ b/src/commands/music/play.ts @@ -1,12 +1,10 @@ import { SlashCommandBuilder } from "@discordjs/builders"; -import { Player, SearchResult, useMainPlayer, useQueue } from "discord-player"; +import { SearchResult, useMainPlayer, useQueue } from "discord-player"; import { AutocompleteInteraction, ChatInputCommandInteraction, Client, EmbedBuilder, - GuildResolvable, - VoiceBasedChannel, } from "discord.js"; import { getLocale, getLocalizations } from "../../utils/locales"; import { Metadata } from "../../utils/metadata"; @@ -71,7 +69,7 @@ export default { loc_default?.get(`c_${filename}_opt1_name`) as string, ); - const player = useMainPlayer() as Player; + const player = useMainPlayer(); if (!query) { // Now playing @@ -97,7 +95,7 @@ export default { return await interaction.reply({ embeds: [embed] }); } - const queue = player.nodes.create(interaction.guild as GuildResolvable, { + const queue = player.nodes.create(interaction.guild!, { defaultFFmpegFilters: ["silenceremove"], metadata: { channel: interaction.channel, @@ -106,7 +104,7 @@ export default { // Verify vc connection try { - if (!queue.connection) await queue.connect(member.voice.channel as VoiceBasedChannel); + if (!queue.connection) await queue.connect(member.voice.channel!); } catch { queue.delete(); return await interaction.reply({ @@ -155,7 +153,7 @@ export default { const loc_default = interaction.client.locales.get(interaction.client.config.default_lang); const filename = getFilename(__filename); - const player = useMainPlayer() as Player; + const player = useMainPlayer(); const query = interaction.options.getString( loc_default?.get(`c_${filename}_opt1_name`) as string, true, diff --git a/src/commands/music/queue.ts b/src/commands/music/queue.ts index 2ba005e..5a761c4 100644 --- a/src/commands/music/queue.ts +++ b/src/commands/music/queue.ts @@ -153,7 +153,7 @@ export default { case loc_default?.get(`c_${filename}_sub3_name`)?.toLowerCase(): { const id = interaction.options.getNumber( loc_default?.get(`c_${filename}_sub3_opt1_name`) as string, - ) as number; + )!; const track = queue.removeTrack(id - 1); diff --git a/src/commands/music/stop.ts b/src/commands/music/stop.ts index eed0062..ac8e9ca 100644 --- a/src/commands/music/stop.ts +++ b/src/commands/music/stop.ts @@ -1,8 +1,7 @@ import { SlashCommandBuilder } from "@discordjs/builders"; -import { Player, useMainPlayer } from "discord-player"; -import { ChatInputCommandInteraction, Client, GuildResolvable } from "discord.js"; +import { useMainPlayer } from "discord-player"; +import { ChatInputCommandInteraction, Client } from "discord.js"; import { getLocale, getLocalizations } from "../../utils/locales"; -import { Metadata } from "../../utils/metadata"; import { getFilename } from "../../utils/misc"; export default { @@ -25,11 +24,11 @@ export default { interaction: async (interaction: ChatInputCommandInteraction, client: Client) => { const loc = getLocale(client, interaction.locale); - const player = useMainPlayer() as Player; - const queue = player.nodes.create(interaction.guild as GuildResolvable, { + const player = useMainPlayer(); + const queue = player.nodes.create(interaction.guild!, { metadata: { channel: interaction.channel, - } as Metadata, + }, }); if (!(queue.connection || queue.node.isPlaying())) { diff --git a/src/events/loader.ts b/src/events/loader.ts index 58c966b..733784c 100644 --- a/src/events/loader.ts +++ b/src/events/loader.ts @@ -6,7 +6,7 @@ import { splitFilenameExtensions } from "../utils/misc"; /** Load all the events */ export default async (client: Client, isDev: boolean) => { const events_categories = (await readdir(__dirname)).filter( - (element) => !element.endsWith(".js") && !element.endsWith(".ts"), + (element) => splitFilenameExtensions(element)?.ext === undefined, ); const player = useMainPlayer(); @@ -45,7 +45,7 @@ export default async (client: Client, isDev: boolean) => { ); // Remove extension - const { file: event_type, ext } = splitFilenameExtensions(event_file); + const { file: event_type, ext } = splitFilenameExtensions(event_file)!; if (!(ext === "js" || ext === "ts")) { throw `Unknown file in ${event_category}: ${event_file}`; } diff --git a/src/events/message/messageCreate.ts b/src/events/message/messageCreate.ts index 1803a51..c76e35c 100644 --- a/src/events/message/messageCreate.ts +++ b/src/events/message/messageCreate.ts @@ -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 { isImage, userWithNickname } from "../../utils/misc"; import { showDate } from "../../utils/time"; @@ -146,7 +146,7 @@ export default async (message: Message, client: Client) => { if (message.author === quoted_post?.author) { author += " & Citateur"; } else { - footer += `\nCité par ${userWithNickname(message.member as GuildMember) ?? "?"} le ${showDate( + footer += `\nCité par ${userWithNickname(message.member!) ?? "?"} le ${showDate( client.config.default_lang, loc, message.createdAt, diff --git a/src/utils/misc.ts b/src/utils/misc.ts index b89db42..e2a7fd2 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -34,10 +34,26 @@ export const getFilename = (path: string) => { * @returns Object with filename and extension splitted */ export const splitFilenameExtensions = (filename: string) => { - const array = filename.split("."); - const ext = array.pop(); + if (filename.length === 0) { + 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 */ export const removeExtension = (filename: string) => { - 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; + return splitFilenameExtensions(filename)!.file; }; /** @@ -64,7 +71,11 @@ export const getExtension = (filename: string) => { * @returns true is file is a media */ 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/), + ); }; /** diff --git a/src/utils/music.ts b/src/utils/music.ts index 592ae8a..da84263 100644 --- a/src/utils/music.ts +++ b/src/utils/music.ts @@ -1,5 +1,5 @@ import { EmbedBuilder } from "@discordjs/builders"; -import { GuildQueue, QueueRepeatMode, Track } from "discord-player"; +import { GuildQueue, QueueRepeatMode } from "discord-player"; import { Client } from "discord.js"; import { getLocale } from "./locales"; import { generateTrustedToken, YoutubeiExtractor } from "discord-player-youtubei"; @@ -15,7 +15,7 @@ export const embedListQueue = ( const tracks = queue.tracks.toArray(); // 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 const limit_fields = 25;