Skip to content

Instantly share code, notes, and snippets.

@sr229
Created August 2, 2016 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sr229/473f6956de725f6f0518c26645d3dec3 to your computer and use it in GitHub Desktop.
Save sr229/473f6956de725f6f0518c26645d3dec3 to your computer and use it in GitHub Desktop.
full music bot written in eris. at this moment it only supports playing in one guild at a time, but this can be fixed very easily.
'use strict';
const Eris = require("eris");
const ytdl = require('ytdl-core');
var bot = new Eris.CommandClient("token", {}, {
description: "The Quantum MusicBot.",
owner: "Gus",
prefix: "q!"
});
var connections = [];
var queue = [];
var now = {};
function playSomething() {
let temp = queue.shift();
if (connections[now.vc]) {
if (connections[now.vc].playing) {
queue.unshift(temp);
return;
}
}
now = temp;
bot.joinVoiceChannel(now.vc).catch((err) => {
bot.createMessage(now.txt, "Error joining voice channel: " + err.message);
}).then((connection) => {
connections[connection.channelID] = connection;
if(connection.playing) {
return "already playing, something went wrong";
}
let stream = ytdl(now.url, { audioonly: true });
connection.playStream(stream);
bot.createMessage(now.txt, `Playing **${now.name}**`);
connection.once("end", () => {
connection.stopPlaying();
if (queue.length > 0) {
playSomething();
}
});
});
}
var playCmd = bot.registerCommand("play", (msg, args) => {
if(args.length === 0) {
return "Invalid input";
}
if(msg.member.voiceState === null) {
return "You are not in a voice channel.";
}
var url = args[0].replace('<','').replace('>','');
try {
ytdl.getInfo(url, (err, info) => {
if (!err) {
queue.push({name: info.title, url: url, requestedBy: msg.member.id, vc: msg.member.voiceState.channelID, txt: msg.channel.id});
bot.createMessage(msg.channel.id, "Added song to queue!");
playSomething();
} else {
return "url is invalid or cannot be played";
}
});
} catch (err) {
return "that url is invalid, sorry";
}
}, {
description: "add a song to the queue",
fullDescription: "add a song to the queue",
usage: "<url>",
serverOnly: true
});
var stopCmd = bot.registerCommand("stop", (msg, args) => {
if(msg.member.voiceState === null) {
return "You are not in a voice channel.";
}
try {
connections[msg.member.voiceState.channelID].stopPlaying();
connections[msg.member.voiceState.channelID].disconnect();
delete connections[msg.member.voiceState.channelID];
queue = [];
return "all good ;)";
} catch (err) {
return "something went wrong";
}
},{
description: "stop the music",
fullDescription: "stop the music",
serverOnly: true
});
bot.registerCommand("list", (msg, args) => {
return queue.length > 0 ? queue.map((q, i) => `${i+1}. ${q.name}`).join('\n') : "Queue is empty!";
}, {
description: "get list of songs",
serverOnly: true
});
bot.registerCommand("playing", (msg, args) => {
return `Now Playing: **${now.name}**`;
}, {
description: "get currently playing song",
serverOnly: true
});
bot.registerCommand("ping", "Pong!", {
description: "Pong!",
fullDescription: "ping pong ping pong"
});
bot.on("ready", () => {
console.log("Ready!");
});
bot.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment