134 lines
4.1 KiB
JavaScript
134 lines
4.1 KiB
JavaScript
const { Client, Interaction, SlashCommandBuilder, VoiceChannel } = require('discord.js');
|
|
const { playFromQueue, queue} = require('../../playback');
|
|
const ytdl = require('ytdl-core');
|
|
const { default: axios } = require('axios');
|
|
const { setStopped, cancelTimer } = require('../../playback')
|
|
require('dotenv').config();
|
|
var isDisabled = true
|
|
|
|
const API_KEY = process.env.YoutubeTOKEN
|
|
/**
|
|
*
|
|
* @param {Client} client
|
|
* @param {Interaction} interaction
|
|
*/
|
|
|
|
|
|
async function addToQueue(interaction, link, songName) {
|
|
const voiceChannel = interaction.member.voice.channel;
|
|
const textChannel = interaction.channel;
|
|
const getMember = interaction.member.displayName;
|
|
|
|
const wasQueueEmpty = queue.length === 0;
|
|
queue.push({interaction, getMember, link, songName})
|
|
cancelTimer()
|
|
if (wasQueueEmpty) {
|
|
await playFromQueue(voiceChannel, textChannel, getMember, link, songName)
|
|
} else {
|
|
//console.log(queue)
|
|
interaction.reply(`Added to queue: ${songName}`)
|
|
}
|
|
}
|
|
|
|
async function searchYoutubeSong(input) {
|
|
try {
|
|
const response = await axios.get('https://www.googleapis.com/youtube/v3/search', {
|
|
params: {
|
|
part: 'snippet',
|
|
q: input,
|
|
key: API_KEY,
|
|
maxResults: 1,
|
|
type: 'video',
|
|
}
|
|
});
|
|
|
|
if (response.data.items && response.data.items.length > 0) {
|
|
const video = response.data.items[0];
|
|
const videoId = video.id.videoId;
|
|
const videoTitle = video.snippet.title;
|
|
const videoUrl = `https://www.youtube.com/watch?v=${videoId}`;
|
|
return { videoUrl, videoTitle}
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
console.error('Error searching Youtube: ', error)
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
async function handlePlayCommand(interaction, input) {
|
|
const voiceChannel = interaction.member.voice.channel;
|
|
|
|
if (!voiceChannel) {
|
|
return interaction.reply({
|
|
content: 'You need to be in a voice channel to use this command.',
|
|
ephemeral: true
|
|
});
|
|
}
|
|
|
|
try {
|
|
let link = '';
|
|
let songName = input;
|
|
|
|
if (ytdl.validateURL(input)) {
|
|
// If it's a valid YouTube URL, extract the song information
|
|
const videoInfo = await ytdl.getBasicInfo(input);
|
|
songName = videoInfo.videoDetails.title;
|
|
link = input;
|
|
} else {
|
|
// If it's a search query, find the video
|
|
const searchResult = await searchYoutubeSong(input);
|
|
if (searchResult) {
|
|
link = searchResult.videoUrl;
|
|
songName = searchResult.videoTitle;
|
|
} else {
|
|
return interaction.reply({
|
|
content: 'No results found for your query.',
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Add the song to the queue and play if needed
|
|
await addToQueue(interaction, link, songName);
|
|
|
|
} catch (error) {
|
|
console.error('Error handling play command:', error);
|
|
await interaction.reply({
|
|
content: 'An error occurred while processing your request.',
|
|
ephemeral: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('bplay')
|
|
.setDescription('Plays a song')
|
|
.addStringOption(option =>
|
|
option.setName('query')
|
|
.setDescription('The song name or URL to play')
|
|
.setRequired(true)
|
|
),
|
|
|
|
async execute(interaction) {
|
|
try {
|
|
if (isDisabled) return interaction.reply({ content: 'The command is Disabled... Try again later', ephemeral: true})
|
|
const query = interaction.options.getString('query');
|
|
setStopped(false)
|
|
await handlePlayCommand(interaction, query);
|
|
} catch (error) {
|
|
console.error('Error executing play command:', error);
|
|
await interaction.reply({
|
|
content: 'An error occurred while processing your request.',
|
|
ephemeral: true
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
|