2022-08-28 21:53:54 +05:00
|
|
|
const { SlashCommandBuilder, ActionRowBuilder, SelectMenuBuilder, } = require("discord.js"),
|
2022-08-02 17:18:47 +05:00
|
|
|
{ joinVoiceChannel, createAudioResource, createAudioPlayer, getVoiceConnection, AudioPlayerStatus } = require("@discordjs/voice");
|
|
|
|
const BaseCommand = require("../../base/BaseCommand"),
|
|
|
|
fs = require("fs");
|
|
|
|
|
|
|
|
class Clips extends BaseCommand {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../base/JaBa")} client
|
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
super({
|
|
|
|
command: new SlashCommandBuilder()
|
|
|
|
.setName("clips")
|
|
|
|
.setDescription(client.translate("music/clips:DESCRIPTION")),
|
|
|
|
aliases: [],
|
|
|
|
dirname: __dirname,
|
|
|
|
guildOnly: true,
|
|
|
|
ownerOnly: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../../base/JaBa")} client
|
|
|
|
*/
|
|
|
|
async onLoad() {
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../../base/JaBa")} client
|
|
|
|
* @param {import("discord.js").ChatInputCommandInteraction} interaction
|
2022-08-09 23:48:33 +05:00
|
|
|
* @param {Object} data
|
2022-08-02 17:18:47 +05:00
|
|
|
*/
|
|
|
|
async execute(client, interaction) {
|
|
|
|
fs.readdir("./clips", async function (err, files) {
|
2022-08-28 21:53:54 +05:00
|
|
|
await interaction.deferReply();
|
|
|
|
|
2022-09-13 22:19:51 +05:00
|
|
|
if (err) {
|
|
|
|
interaction.editReply({
|
|
|
|
content: "```js\n" + err + "```"
|
|
|
|
});
|
|
|
|
return console.log("Unable to read directory: " + err);
|
|
|
|
}
|
2022-08-02 17:18:47 +05:00
|
|
|
|
|
|
|
const clips = files.map(file => {
|
|
|
|
const fileName = file.substring(0, file.length - 4);
|
|
|
|
return {
|
|
|
|
label: fileName,
|
|
|
|
value: fileName
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const row = new ActionRowBuilder()
|
|
|
|
.addComponents(
|
|
|
|
new SelectMenuBuilder()
|
|
|
|
.setCustomId("clips_select")
|
|
|
|
.setPlaceholder(client.translate("common:NOTHING_SELECTED"))
|
|
|
|
.addOptions(clips)
|
|
|
|
);
|
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
await interaction.editReply({
|
2022-08-02 17:18:47 +05:00
|
|
|
content: interaction.translate("music/clips:AVAILABLE_CLIPS"),
|
2022-08-28 21:53:54 +05:00
|
|
|
components: [row]
|
2022-08-02 17:18:47 +05:00
|
|
|
});
|
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
const filter = i => i.user.id === interaction.user.id;
|
|
|
|
const collector = interaction.channel.createMessageComponentCollector({ filter, idle: (15 * 1000) });
|
2022-08-02 17:18:47 +05:00
|
|
|
|
2022-08-06 20:47:47 +05:00
|
|
|
collector.on("collect", async i => {
|
2022-08-28 21:53:54 +05:00
|
|
|
if (i.isSelectMenu() && i.customId === "clips_select") {
|
|
|
|
const clip = i?.values[0];
|
|
|
|
const voice = i.member.voice.channel;
|
|
|
|
if (!voice) return i.update({ content: interaction.translate("music/play:NO_VOICE_CHANNEL"), components: [] });
|
|
|
|
const queue = client.player.getQueue(i.guild.id);
|
|
|
|
if (queue) return i.update({ content: interaction.translate("music/clips:ACTIVE_QUEUE"), components: [] });
|
|
|
|
if (getVoiceConnection(i.guild.id)) return i.update({ content: interaction.translate("music/clips:ACTIVE_CLIP"), components: [] });
|
|
|
|
if (!fs.existsSync(`./clips/${clip}.mp3`)) return i.update({ content: interaction.translate("music/clips:NO_FILE", { file: clip }), components: [] });
|
2022-08-02 17:18:47 +05:00
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
try {
|
|
|
|
const connection = joinVoiceChannel({
|
|
|
|
channelId: voice.id,
|
|
|
|
guildId: interaction.guild.id,
|
|
|
|
adapterCreator: interaction.guild.voiceAdapterCreator
|
|
|
|
});
|
2022-08-02 17:18:47 +05:00
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
const resource = createAudioResource(fs.createReadStream(`./clips/${clip}.mp3`));
|
|
|
|
const player = createAudioPlayer()
|
|
|
|
.on("error", err => {
|
|
|
|
connection.destroy();
|
|
|
|
console.error(err.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
player.play(resource);
|
|
|
|
connection.subscribe(player);
|
|
|
|
|
|
|
|
player.on(AudioPlayerStatus.Idle, () => {
|
2022-08-02 17:18:47 +05:00
|
|
|
connection.destroy();
|
|
|
|
});
|
2022-08-28 21:53:54 +05:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2022-08-02 17:18:47 +05:00
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
await interaction.editReply({
|
|
|
|
content: interaction.translate("music/clips:PLAYING", {
|
|
|
|
clip
|
|
|
|
}),
|
|
|
|
components: []
|
2022-08-02 17:18:47 +05:00
|
|
|
});
|
|
|
|
}
|
2022-08-28 21:53:54 +05:00
|
|
|
});
|
2022-08-02 17:18:47 +05:00
|
|
|
|
2022-08-28 21:53:54 +05:00
|
|
|
collector.on("end", () => {
|
|
|
|
return interaction.editReply({
|
2022-08-02 17:18:47 +05:00
|
|
|
components: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Clips;
|