chore: merge dev to main #172

Merged
Anri merged 11 commits from dev into main 2024-09-18 18:25:04 +02:00
23 changed files with 1108 additions and 190 deletions

1037
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -25,6 +25,7 @@
"discord-player-youtubei": "^1.3.1",
"discord.js": "^14.16.2",
"mediaplex": "^0.0.9",
"puppeteer": "^23.4.0",
"sqlite3": "^5.1.7",
"typescript": "^5.6.2",
"uuid": "^10.0.0"

View file

@ -10,9 +10,9 @@ 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"),
);
const buttons_categories = (await readdir(__dirname, { withFileTypes: true }))
.filter((element) => element.isDirectory())
.map((element) => element.name);
await Promise.all(
// For each categorie

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,20 +17,20 @@ 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]);
if (page - 1 == 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 {
page--;
}
// 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

@ -24,7 +24,7 @@ export default {
// Retrieve Pages
const pageMax = Number(/(\d+)(?!.*\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;
} else {
page--;

View file

@ -8,9 +8,9 @@ import { removeExtension } from "../utils/misc";
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"),
);
const command_categories = (await readdir(__dirname, { withFileTypes: true }))
.filter((element) => element.isDirectory())
.map((element) => element.name);
const commands = (
await Promise.all(
@ -70,7 +70,7 @@ export default async (client: Client) => {
);
// Send global commands to Discord
const globalCommands = commands.filter((c) => c.scope().length == 0);
const globalCommands = commands.filter((c) => c.scope().length === 0);
return await rest.put(Routes.applicationCommands(client.user?.id as string), {
body: globalCommands.map((c) => c.data.toJSON()),
});

View file

@ -74,14 +74,14 @@ export default {
allChannel?.then(async (channelGuild) => {
// Retrieve category to archive
const catToArchive = channelGuild
.filter((chan) => chan?.type == ChannelType.GuildCategory)
.filter((chan) => chan?.name == desiredCat);
.filter((chan) => chan?.type === ChannelType.GuildCategory)
.filter((chan) => chan?.name === desiredCat);
// Create/Retrieve the archive category
const catArchivedName = "archive - " + desiredCat;
const catArchivedMap = channelGuild
.filter((chan) => chan?.type == ChannelType.GuildCategory)
.filter((chan) => chan?.name == catArchivedName);
.filter((chan) => chan?.type === ChannelType.GuildCategory)
.filter((chan) => chan?.name === catArchivedName);
let catArchived: NonThreadGuildBasedChannel | null | undefined;
if (catArchivedMap.size > 0) {
@ -94,11 +94,11 @@ export default {
}
const allChannelDesired = channelGuild
.filter((chan) => chan?.type == 0)
.filter((chan) => chan?.parentId == catToArchive.map((cat) => cat?.id)[0]);
.filter((chan) => chan?.type === 0)
.filter((chan) => chan?.parentId === catToArchive.map((cat) => cat?.id)[0]);
// If no channels in the source category
if (allChannelDesired.size == 0) {
if (allChannelDesired.size === 0) {
return interaction.reply({
embeds: [
new EmbedBuilder()

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

@ -67,21 +67,21 @@ export default {
const allChannel = interaction.guild?.channels.fetch();
allChannel?.then((channel_guild) => {
const cat_to_prep = channel_guild
.filter((chan) => chan?.type == ChannelType.GuildCategory)
.filter((chan) => chan?.name == desired_cat);
.filter((chan) => chan?.type === ChannelType.GuildCategory)
.filter((chan) => chan?.name === desired_cat);
const cat_to_prep_id = cat_to_prep.map((cat) => cat?.id);
const cat_to_prep_name = cat_to_prep.map((cat) => cat?.name);
// console.log(cat_to_prep);
const all_channel_desired = channel_guild
.filter((chan) => chan?.type == 0)
.filter((chan) => chan?.parentId == cat_to_prep_id[0]);
.filter((chan) => chan?.type === 0)
.filter((chan) => chan?.parentId === cat_to_prep_id[0]);
const all_channel_desired_name = all_channel_desired.map((c_d) => c_d?.name);
let desc = "";
const general = "général";
if (all_channel_desired_name.filter((cdn) => cdn == general).length == 0) {
if (all_channel_desired_name.filter((cdn) => cdn === general).length === 0) {
interaction.guild?.channels.create({
name: general,
type: 0,
@ -91,7 +91,7 @@ export default {
}
const info = "informations";
if (all_channel_desired_name.filter((cdn) => cdn == info).length == 0) {
if (all_channel_desired_name.filter((cdn) => cdn === info).length === 0) {
interaction.guild?.channels.create({
name: info,
type: 0,
@ -101,7 +101,7 @@ export default {
desc += "`" + info + "` " + loc.get("c_prep5") + "\n";
}
if (desc == "") {
if (desc === "") {
desc = loc.get("c_prep6");
}

View file

@ -205,7 +205,7 @@ export default {
let user = interaction.options.getUser(
loc_default?.get(`c_${filename}_sub2_opt1_name`) as string,
);
if (user == null) {
if (user === null) {
user = interaction.user;
}

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,11 +81,11 @@ export default {
let data = null;
await interaction.deferReply();
const player = useMainPlayer() as Player;
const player = useMainPlayer();
const queue = useQueue(interaction.guildId!);
if (request) {
if (
interaction.options.getSubcommand() ==
interaction.options.getSubcommand() ===
loc_default?.get(`c_${filename}_sub2_name`)?.toLowerCase()
) {
// Romanized
@ -111,7 +111,7 @@ export default {
}
if (
interaction.options.getSubcommand() ==
interaction.options.getSubcommand() ===
loc_default?.get(`c_${filename}_sub3_name`)?.toLowerCase()
) {
if (queue === null) {

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

@ -1,12 +1,37 @@
import { Player, PlayerEvents, useMainPlayer } from "discord-player";
import { PlayerEvents, useMainPlayer } from "discord-player";
import { Client } from "discord.js";
import { readdir } from "fs/promises";
import { splitFilenameExtensions } from "../utils/misc";
/** Load all the events */
export default async (client: Client) => {
const events_categories = (await readdir(__dirname)).filter(
(element) => !element.endsWith(".js") && !element.endsWith(".ts"),
);
export default async (client: Client, isDev: boolean) => {
const events_categories = (await readdir(__dirname, { withFileTypes: true }))
.filter((element) => element.isDirectory())
.map((element) => element.name);
const player = useMainPlayer();
if (isDev) {
player.on("debug", async (message) => {
console.log(`General player debug event: ${message}`);
});
player.events.on("debug", async (_, message) => {
console.log(`Player debug event: ${message}`);
});
}
player.events.on("error", (_, error) => {
// Emitted when the player queue encounters error
console.error(`General player error event: ${error.message}`);
console.error(error);
});
player.events.on("playerError", (_, error) => {
// Emitted when the audio player errors while streaming audio track
console.error(`Player error event: ${error.message}`);
console.error(error);
});
events_categories.forEach(async (event_category) => {
// Retrieve events
@ -20,16 +45,12 @@ export default async (client: Client) => {
);
// Remove extension
// TODO: use utils functions
const event_type_ext = event_file.split(".");
const ext = event_type_ext.pop();
const { file: event_type, ext } = splitFilenameExtensions(event_file)!;
if (!(ext === "js" || ext === "ts")) {
throw `Unknown file in ${event_category}: ${event_file}`;
}
const event_type = event_type_ext.join(".");
if (event_category == "player") {
const player = useMainPlayer() as Player;
if (event_category === "player") {
if (once) {
// eslint-disable-next-line
return player.events.once(event_type as keyof PlayerEvents, (...args: any[]) => {

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";
@ -82,7 +82,7 @@ export default async (message: Message, client: Client) => {
// If message doesn't exist or empty
if (
!quoted_message ||
(!quoted_message.content && quoted_message.attachments.size == 0)
(!quoted_message.content && quoted_message.attachments.size === 0)
) {
return;
}
@ -143,10 +143,10 @@ export default async (message: Message, client: Client) => {
}
let author = "Auteur";
if (message.author == quoted_post?.author) {
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

@ -12,7 +12,7 @@ export const run = async (isDev: boolean) => {
// Client Discord.JS
const client_name = "Client";
await loadClient()
await loadClient(isDev)
.then(async (client) => {
if (isDev) {
// Attach debugging listeners
@ -21,7 +21,7 @@ export const run = async (isDev: boolean) => {
// Events Discord.JS and Player
const events_name = "Events";
await loadEvents(client)
await loadEvents(client, isDev)
.then(() => console.log(logStart(events_name, true)))
.catch((err) => {
console.error(err);

View file

@ -5,9 +5,12 @@ import { Database } from "sqlite3";
import "../modules/client";
import { loadLocales } from "./locales";
import { YoutubeiExtractor } from "discord-player-youtubei";
import { YoutubeWebTokenRoutine } from "./music";
/** Creation of the client and definition of its properties */
export default async () => {
export default async (isDev: boolean) => {
const activities = isDev ? [] : [{ name: "/help", type: ActivityType.Watching }];
const client: Client = new Client({
shards: "auto",
intents: [
@ -17,7 +20,7 @@ export default async () => {
GatewayIntentBits.GuildVoiceStates,
],
presence: {
activities: [{ name: "/help", type: ActivityType.Watching }],
activities,
},
});
@ -42,6 +45,9 @@ export default async () => {
list: new Collection(),
};
// Generate tokens
const tokens = await YoutubeWebTokenRoutine(true);
const player = Player.singleton(client, {
skipFFmpeg: true,
ytdlOptions: {
@ -50,7 +56,12 @@ export default async () => {
},
});
await player.extractors.loadDefault((ext) => ext !== "YouTubeExtractor");
await player.extractors.register(YoutubeiExtractor, {});
await player.extractors.register(YoutubeiExtractor, {
streamOptions: {
useClient: "WEB",
},
trustedTokens: tokens,
});
console.log("Translations progression :");
client.locales = await loadLocales(client.config.default_lang);

View file

@ -28,27 +28,41 @@ export const getFilename = (path: string) => {
return removeExtension(filename_with_ext);
};
/**
* Split a filename and his extension
* @param filename string of the filename
* @returns Object with filename and extension splitted
*/
export const splitFilenameExtensions = (filename: string) => {
if (filename.length === 0) {
return undefined;
}
// 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 };
};
/**
* 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) => {
const array = filename.split(".");
array.pop();
return array.join(".");
};
/**
* Get extension from a filename
* @param filename string of the filename
* @returns string of the extension if it exists
*/
export const getExtension = (filename: string) => {
const array = filename.split(".");
return array.pop();
return splitFilenameExtensions(filename)!.file;
};
/**
@ -57,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,7 +1,8 @@
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";
export const embedListQueue = (
client: Client,
@ -14,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;
@ -25,10 +26,10 @@ export const embedListQueue = (
embed.setFooter({ text: `${printRepeatMode(queue.repeatMode, loc)}` });
tracks.slice((page - 1) * limit_fields, page * limit_fields).forEach((t, idx) => {
const now_playing = idx == 0 && page == 1;
const now_playing = idx === 0 && page === 1;
const name = now_playing
? loc.get("c_queue10")
: (idx == 1 && page == 1) || (idx == 0 && page > 1)
: (idx === 1 && page === 1) || (idx === 0 && page > 1)
? loc.get("c_queue11")
: "\u200b";
const idx_track = now_playing ? "" : `${idx + limit_fields * (page - 1)}. `;
@ -57,3 +58,16 @@ const printRepeatMode = (mode: QueueRepeatMode, loc: Map<string, string>) => {
break;
}
};
export const YoutubeWebTokenRoutine = async (first: boolean) => {
// Repeat in one week : 7 * 24 * 60 * 60 * 1000
setTimeout(YoutubeWebTokenRoutine, 604800000, false);
const tokens = await generateTrustedToken();
if (first) {
return tokens;
}
const instance = YoutubeiExtractor.getInstance();
instance?.setTrustedTokens(tokens);
};

View file

@ -166,7 +166,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR
}
}
if (option == OptionReminder.DirectMessage || !channelOk || !guildOk) {
if (option === OptionReminder.DirectMessage || !channelOk || !guildOk) {
// Direct message
const user = client.users.cache.get(info.userId);
if (user !== undefined) {
@ -182,7 +182,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR
});
// Mention everybody if needed
if (option == OptionReminder.Mention) {
if (option === OptionReminder.Mention) {
(info.message?.match(/<@\d+>/g) ?? []).forEach((mention) => {
content += " " + mention;
});