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 { 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(

View file

@ -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(

View file

@ -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(

View file

@ -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 {

View file

@ -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 = (

View file

@ -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,
),
],

View file

@ -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 (

View file

@ -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,

View file

@ -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);

View file

@ -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())) {

View file

@ -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}`;
}

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 { 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,

View file

@ -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/),
);
};
/**

View file

@ -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;