JaBa/commands/Music/clip.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

const Command = require("../../base/Command"),
2022-01-08 04:14:04 +05:00
fs = require("fs"),
2022-02-09 15:59:05 +05:00
{ joinVoiceChannel, createAudioResource, createAudioPlayer, getVoiceConnection, AudioPlayerStatus } = require("@discordjs/voice");
2022-01-04 02:18:28 +05:00
class Clip extends Command {
constructor(client) {
super(client, {
name: "clip",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: [],
memberPermissions: [],
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS"],
nsfw: false,
ownerOnly: false,
cooldown: 2000
});
}
async run(message, args) {
const voice = message.member.voice.channel;
const queue = this.client.player.getQueue(message);
const clip = args[0];
if (!voice) return message.error("music/play:NO_VOICE_CHANNEL");
2022-01-08 12:27:39 +05:00
if (getVoiceConnection(message.guild.id)) return message.error("music/clip:ACTIVE_CLIP");
2022-01-04 02:18:28 +05:00
if (queue) return message.error("music/clip:ACTIVE_QUEUE");
if (!clip) return message.error("music/clip:NO_ARG");
if (!fs.existsSync(`./clips/${clip}.mp3`)) return message.error("music/clip:NO_FILE", { file: clip });
try {
2022-01-08 04:14:04 +05:00
const connection = joinVoiceChannel({
channelId: voice.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
2022-01-04 02:18:28 +05:00
2022-01-08 04:14:04 +05:00
const resource = createAudioResource(fs.createReadStream(`./clips/${clip}.mp3`));
const player = createAudioPlayer()
2022-02-09 15:59:05 +05:00
.on("error", err => {
2022-01-08 04:14:04 +05:00
connection.destroy();
2022-02-09 15:59:05 +05:00
console.error(err.message);
2022-01-04 02:18:28 +05:00
});
2022-01-08 04:14:04 +05:00
player.play(resource);
connection.subscribe(player);
2022-01-08 18:13:17 +05:00
2022-02-09 15:59:05 +05:00
player.on(AudioPlayerStatus.Idle, () => {
2022-01-08 18:13:17 +05:00
connection.destroy();
2022-02-09 15:59:05 +05:00
});
2022-01-04 02:18:28 +05:00
} catch (error) {
console.error(error);
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
}
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
module.exports = Clip;