Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
- const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType } = require('@discordjs/voice');
- const ytdl = require('ytdl-core');
- const { Player } = require('discord-player');
- const token = 'MTE5ODIwNjMyNDU2ODIyNzkyMQ.GHjA_M.N7iRxo0t2X4ExL8zq_fjKMaDQfyq8Ag-d91b6g';
- const clientId = '1198206324568227921';
- const guildId = '1127487023415046284';
- const commands = [
- {
- name: 'play',
- description: 'Play a song',
- options: [
- {
- name: 'song',
- type: 3,
- description: 'The YouTube URL or search query for the song',
- required: true,
- },
- ],
- },
- ];
- const client = new Client({
- intents: [
- GatewayIntentBits.Guilds,
- GatewayIntentBits.GuildMessages,
- GatewayIntentBits.MessageContent,
- GatewayIntentBits.GuildVoiceStates,
- ],
- });
- const rest = new REST({ version: '10' }).setToken(token);
- (async () => {
- try {
- console.log('Started refreshing application (/) commands.');
- await rest.put(
- Routes.applicationGuildCommands(clientId, guildId),
- { body: commands },
- );
- console.log('Successfully reloaded application (/) commands.');
- } catch (error) {
- console.error(error);
- }
- })();
- const queue = new Map();
- client.on('interactionCreate', async interaction => {
- if (!interaction.isCommand()) return;
- const { commandName, options } = interaction;
- if (commandName === 'play') {
- const songQuery = options.getString('song');
- const voiceChannel = interaction.member.voice.channel;
- if (!voiceChannel) {
- return interaction.reply('You must be in a voice channel to use this command.');
- }
- const serverQueue = queue.get(interaction.guild.id) || {
- voiceChannel,
- connection: null,
- songs: [],
- player: createAudioPlayer(),
- };
- if (!serverQueue.connection) {
- const connection = joinVoiceChannel({
- channelId: voiceChannel.id,
- guildId: interaction.guild.id,
- adapterCreator: interaction.guild.voiceAdapterCreator,
- selfDeaf: false,
- });
- serverQueue.connection = connection;
- connection.subscribe(serverQueue.player);
- }
- try {
- const info = await ytdl.getInfo(songQuery);
- const song = {
- title: info.videoDetails.title,
- url: info.videoDetails.video_url,
- };
- serverQueue.songs.push(song);
- if (!serverQueue) {
- play(interaction.guild, serverQueue.songs[0]);
- }
- interaction.reply(`Added to queue: ${song.title}`);
- } catch (error) {
- console.error(error);
- interaction.reply('Error occurred while processing the song.');
- }
- queue.set(interaction.guild.id, serverQueue);
- }
- });
- function play(guild, song) {
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- const stream = ytdl(song.url, { filter: 'audioonly' });
- const resource = createAudioResource(stream, { inputType: StreamType.Opus });
- play(resource);
- serverQueue.player.on('finish', () => {
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0]);
- });
- serverQueue.player.on('error', error => {
- console.error(error);
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- });
- }
- client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement