Advertisement
VeNoM_1

Untitled

Jan 20th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.43 KB | Source Code | 0 0
  1. const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
  2. const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType } = require('@discordjs/voice');
  3. const ytdl = require('ytdl-core');
  4. const { Player } = require('discord-player');
  5.  
  6. const token = 'MTE5ODIwNjMyNDU2ODIyNzkyMQ.GHjA_M.N7iRxo0t2X4ExL8zq_fjKMaDQfyq8Ag-d91b6g';
  7. const clientId = '1198206324568227921';
  8. const guildId = '1127487023415046284';
  9. const commands = [
  10.   {
  11.     name: 'play',
  12.     description: 'Play a song',
  13.     options: [
  14.       {
  15.         name: 'song',
  16.         type: 3,
  17.         description: 'The YouTube URL or search query for the song',
  18.         required: true,
  19.       },
  20.     ],
  21.   },
  22. ];
  23.  
  24. const client = new Client({
  25.   intents: [
  26.     GatewayIntentBits.Guilds,
  27.     GatewayIntentBits.GuildMessages,
  28.     GatewayIntentBits.MessageContent,
  29.     GatewayIntentBits.GuildVoiceStates,
  30.   ],
  31. });
  32.  
  33. const rest = new REST({ version: '10' }).setToken(token);
  34.  
  35. (async () => {
  36.   try {
  37.     console.log('Started refreshing application (/) commands.');
  38.  
  39.     await rest.put(
  40.       Routes.applicationGuildCommands(clientId, guildId),
  41.       { body: commands },
  42.     );
  43.  
  44.     console.log('Successfully reloaded application (/) commands.');
  45.   } catch (error) {
  46.     console.error(error);
  47.   }
  48. })();
  49.  
  50. const queue = new Map();
  51.  
  52. client.on('interactionCreate', async interaction => {
  53.   if (!interaction.isCommand()) return;
  54.  
  55.   const { commandName, options } = interaction;
  56.  
  57.   if (commandName === 'play') {
  58.     const songQuery = options.getString('song');
  59.     const voiceChannel = interaction.member.voice.channel;
  60.  
  61.     if (!voiceChannel) {
  62.       return interaction.reply('You must be in a voice channel to use this command.');
  63.     }
  64.  
  65.     const serverQueue = queue.get(interaction.guild.id) || {
  66.       voiceChannel,
  67.       connection: null,
  68.       songs: [],
  69.       player: createAudioPlayer(),
  70.     };
  71.  
  72.     if (!serverQueue.connection) {
  73.       const connection = joinVoiceChannel({
  74.         channelId: voiceChannel.id,
  75.         guildId: interaction.guild.id,
  76.         adapterCreator: interaction.guild.voiceAdapterCreator,
  77.         selfDeaf: false,
  78.       });
  79.  
  80.       serverQueue.connection = connection;
  81.       connection.subscribe(serverQueue.player);
  82.     }
  83.  
  84.     try {
  85.       const info = await ytdl.getInfo(songQuery);
  86.       const song = {
  87.         title: info.videoDetails.title,
  88.         url: info.videoDetails.video_url,
  89.       };
  90.  
  91.       serverQueue.songs.push(song);
  92.  
  93.       if (!serverQueue) {
  94.         play(interaction.guild, serverQueue.songs[0]);
  95.       }
  96.  
  97.       interaction.reply(`Added to queue: ${song.title}`);
  98.     } catch (error) {
  99.       console.error(error);
  100.       interaction.reply('Error occurred while processing the song.');
  101.     }
  102.  
  103.     queue.set(interaction.guild.id, serverQueue);
  104.   }
  105. });
  106.  
  107. function play(guild, song) {
  108.   const serverQueue = queue.get(guild.id);
  109.  
  110.   if (!song) {
  111.     serverQueue.voiceChannel.leave();
  112.     queue.delete(guild.id);
  113.     return;
  114.   }
  115.  
  116.   const stream = ytdl(song.url, { filter: 'audioonly' });
  117.   const resource = createAudioResource(stream, { inputType: StreamType.Opus });
  118.  
  119.   play(resource);
  120.  
  121.   serverQueue.player.on('finish', () => {
  122.     serverQueue.songs.shift();
  123.     play(guild, serverQueue.songs[0]);
  124.   });
  125.  
  126.   serverQueue.player.on('error', error => {
  127.     console.error(error);
  128.     serverQueue.voiceChannel.leave();
  129.     queue.delete(guild.id);
  130.   });
  131. }
  132.  
  133.  
  134. client.login(token);
Tags: discord.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement