Facebook
From Katsuu, 1 Year ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 58
  1. const Discord = require('discord.js');
  2. const ytdl = require('ytdl-core');
  3.  
  4. const client = new Discord.Client({
  5.     intents: [
  6.         Discord.GatewayIntentBits.Guilds,
  7.         Discord.GatewayIntentBits.GuildMessages,
  8.         Discord.GatewayIntentBits.MessageContent
  9.     ]
  10. })
  11. const token = 'MTA1NDUwMTA1NTI1OTQ4MDEzNg.GGOpkG.xRaiMaqm5n7Ma6Y01blmlOksAGqpZmVbwr50c8';
  12. const prefix = '!'; // prefix dla poleceń bota
  13.  
  14.  
  15. client.on('ready', () => {
  16.   console.log('Bot jest gotowy do działania!');
  17. });
  18.  
  19. client.on('messageCreate', async message => {
  20.   if (!message.content.startsWith(prefix) || message.author.bot) return; // ignorowanie poleceń od innych botów i poleceń bez prefixa
  21.  
  22.   const args = message.content.slice(prefix.length).trim().split(/ +/);
  23.   const command = args.shift().toLowerCase();
  24.  
  25.   if (command === 'play') {
  26.     const voiceChannel = message.member.voice.channel;
  27.  
  28.     if (!voiceChannel) return message.reply('Nie jesteś podłączony do kanału głosowego!');
  29.  
  30.     const permissions = voiceChannel.permissionsFor(message.client.user);
  31.  
  32.     if (!permissions.has('Connect') || !permissions.has('Speak')) {
  33.       return message.reply('Nie posiadam odpowiednich uprawnień, aby połączyć się i rozmawiać na kanale głosowym!');
  34.     }
  35.  
  36.     if (!args.length) return message.reply('Nie podałeś linku do muzyki!');
  37.  
  38.     const connection = await voiceChannel.join();
  39.     const stream = ytdl(args[0], { filter: 'audioonly' });
  40.     const dispatcher = connection.play(stream);
  41.  
  42.     dispatcher.on('finish', () => voiceChannel.leave());
  43.   } else if (command === 'stop') {
  44.     const voiceChannel = message.member.voice.channel;
  45.  
  46.     if (!voiceChannel) return message.reply('Nie jesteś podłączony do kanału głosowego!');
  47.  
  48.     voiceChannel.leave();
  49.   }
  50. });
  51.  
  52. client.login(token);