feat: Music support #62

Merged
Anri merged 43 commits from feat/music into main 2023-02-12 01:11:10 +01:00
3 changed files with 102 additions and 10 deletions
Showing only changes of commit 4e7645c0e1 - Show all commits

View file

@ -1,23 +1,108 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { ChatInputCommandInteraction, Client } from "discord.js";
import {
ChatInputCommandInteraction,
Client,
GuildResolvable,
VoiceBasedChannel,
} from "discord.js";
import { getLocalizations } from "../../utils/locales";
import { getFilename } from "../../utils/misc";
import { Metadata } from "../../modules/metadata";
export default {
scope: () => [],
data: (client: Client) => {
const filename = getFilename(__filename);
return new SlashCommandBuilder()
.setName(filename.toLowerCase())
.setDescription(
client.locales.get(client.config.default_lang)?.get(`c_${filename}_desc`) ?? ""
)
.setNameLocalizations(getLocalizations(client, `c_${filename}_name`, true))
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`));
const loc_default = client.locales.get(client.config.default_lang);
if (!loc_default) {
return;
}
return (
new SlashCommandBuilder()
.setName(filename.toLowerCase())
.setDescription(loc_default.get(`c_${filename}_desc`) ?? "")
.setNameLocalizations(getLocalizations(client, `c_${filename}_name`, true))
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`))
// Command option
.addStringOption((option) =>
option
.setName(loc_default.get(`c_${filename}_opt1_name`)?.toLowerCase() ?? "")
.setDescription(loc_default.get(`c_${filename}_opt1_desc`) ?? "")
.setNameLocalizations(getLocalizations(client, `c_${filename}_opt1_name`, true))
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_opt1_desc`))
)
);
},
interaction: async (interaction: ChatInputCommandInteraction, client: Client) => {
interaction.reply("coucou");
const loc_default = client.locales.get(client.config.default_lang);
const filename = getFilename(__filename);
const member = client.guilds.cache
.get(interaction.guildId ?? "")
?.members.cache.get(interaction.member?.user.id ?? "");
if (!member?.voice.channelId) {
return await interaction.reply({
content: "Tu n'es dans aucun salon vocal.",
ephemeral: true,
});
}
if (
interaction.guild?.members.me?.voice.channelId &&
member.voice.channelId !== interaction.guild.members.me.voice.channelId
) {
return await interaction.reply({
content: "Je suis déjà en vocal.",
ephemeral: true,
});
}
const query =
interaction.options.getString(loc_default?.get(`c_${filename}_opt1_name`) as string) ?? "";
const queue = client.player.createQueue(interaction.guild as GuildResolvable, {
metadata: {
channel: interaction.channel,
},
});
// Verify vc connection
try {
if (!queue.connection) await queue.connect(member.voice.channel as VoiceBasedChannel);
} catch {
queue.destroy();
return await interaction.reply({
content: "Impossible de rejoindre le salon vocal.",
ephemeral: true,
});
}
await interaction.deferReply();
const track = await client.player
.search(query, {
requestedBy: interaction.user,
})
.then((x) => x.tracks[0]);
if (!track) {
return await interaction.followUp({ content: `❌ | \`${query}\` introuvable.` });
}
// TMP
client.player.on("error", () => console.log("error"));
client.player.on("connectionError", () => console.log("error"));
client.player.on("trackStart", (q, t) =>
(q.metadata as Metadata).channel?.send(`🎶 | Joue \`${t.title}\`.`)
);
//
await queue.play(track);
return await interaction.followUp({ content: `⏱️ | Chargement de \`${track.title}\`.` });
},
};

View file

@ -79,5 +79,7 @@
"c_reminder18": "Pas de message",
"c_play_name": "play",
"c_play_desc": "Joue une chanson/playlist"
"c_play_desc": "Joue une chanson/playlist",
"c_play_opt1_name": "requête",
"c_play_opt1_desc": "Ce que vous voulez écouter"
}

5
src/modules/metadata.ts Normal file
View file

@ -0,0 +1,5 @@
import { TextBasedChannel } from "discord.js";
export type Metadata = {
channel: TextBasedChannel | null;
};