Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Music bot by Laa & ia1q & Ess

const Discord = require("discord.js");
const { prefix, token } = require("./config.json");
const ytdl = require("ytdl-core");

const client = new Discord.Client();

const queue = new Map();

client.once("ready", () => {
  console.log("Ready!");
});

client.once("reconnecting", () => {
  console.log("Reconnecting!");
});

client.once("disconnect", () => {
  console.log("Disconnect!");
});

client.on("message", async message => {
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const serverQueue = queue.get(message.guild.id);

  if (message.content.startsWith(`${prefix}play`)) {
    execute(message, serverQueue);
    return;
  } else if (message.content.startsWith(`${prefix}skip`)) {
    skip(message, serverQueue);
    return;
  } else if (message.content.startsWith(`${prefix}stop`)) {
    stop(message, serverQueue);
    return;
  } else {
    message.channel.send("You need to enter a valid command!");
  }
});

async function execute(message, serverQueue) {
  const args = message.content.split(" ");

  const voiceChannel = message.member.voice.channel;
  if (!voiceChannel)
    return message.channel.send(
      "You need to be in a voice channel to play music!"
    );
  const permissions = voiceChannel.permissionsFor(message.client.user);
  if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
    return message.channel.send(
      "I need the permissions to join and speak in your voice channel!"
    );
  }

  const songInfo = await ytdl.getInfo(args[1]);
  const song = {
    title: songInfo.title,
    url: songInfo.video_url
  };

  if (!serverQueue) {
    const queueContruct = {
      textChannel: message.channel,
      voiceChannel: voiceChannel,
      connection: null,
      songs: [],
      volume: 5,
      playing: true
    };

    queue.set(message.guild.id, queueContruct);

    queueContruct.songs.push(song);

    try {
      var connection = await voiceChannel.join();
      queueContruct.connection = connection;
      play(message.guild, queueContruct.songs[0]);
    } catch (err) {
      console.log(err);
      queue.delete(message.guild.id);
      return message.channel.send(err);
    }
  } else {
    serverQueue.songs.push(song);
    return message.channel.send(`${song.title} has been added to the queue!`);
  }
}

function skip(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
  if (!serverQueue)
    return message.channel.send("There is no song that I could skip!");
  serverQueue.connection.dispatcher.end();
}

function stop(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
  serverQueue.songs = [];
  serverQueue.connection.dispatcher.end();
}

function play(guild, song) {
  const serverQueue = queue.get(guild.id);
  if (!song) {
    serverQueue.voiceChannel.leave();
    queue.delete(guild.id);
    return;
  }

  const dispatcher = serverQueue.connection
    .play(ytdl(song.url))
    .on("finish", () => {
      serverQueue.songs.shift();
      play(guild, serverQueue.songs[0]);
    })
    .on("error", error => console.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}

client.login(token);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript create new array with default values by one line 
Javascript :: dll node cration 
Javascript :: reversing string 
Javascript :: how to define array in react js 
Javascript :: if condition in jasper expression editor 
Javascript :: javascript babel run 
Javascript :: import lodash react 
Javascript :: Add React Router to React Redux CRUD App 
Javascript :: jq query online tutorial 
Javascript :: change string to object in html 
Javascript :: Check for particular values in the response body 
Javascript :: Make a ReactNative component take the height and width of the current window 
Javascript :: how to access res.locals in express 
Javascript :: websocket servcer connectrion refused nodejs 
Javascript :: tab pane full calendar not showing 
Javascript :: repeat a block as many times as a nember jsx 
Javascript :: useLinkClickHandler 
Javascript :: typeof regex 
Javascript :: closing all open files vscode 
Javascript :: server starter code in js 
Javascript :: how to escape double quotes in json 
Javascript :: graphql get item by id from strapi react 
Javascript :: firebase recaptcha using react js 
Javascript :: node get request filepath 
Javascript :: js decrypt online 
Javascript :: js dom after selectors 
Javascript :: jquery crud table example 
Javascript :: change teh value of a slider p5js 
Javascript :: serverless web app with react netlify 
Javascript :: JavaScript Normalized and UnNnormalized URL 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =