Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed May 15, 2019
0 parents commit e0f145a
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Erit Islami

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# EvoBot (Discord Music Bot)
> EvoBot is a Discord Music Bot built with discord.js & uses Command Handler from [discordjs.guide](https://discordjs.guide)
## Requirements

1. Discord Bot Token **[Guide](https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot)**
2. YouTube Data API v3 Key if you want to play youtube videos via text search **[Guide](https://developers.google.com/youtube/v3/getting-started)**
3. Node.js v10.0.0 or newer

## Installation

```
git clone https://github.com/eritislami/evobot.git
cd evobot
npm install
```

After installation finishes you can use `node index.js` to start the bot.

## Configuration

Copy or Rename `config.json.example` to `config.json` and fill out the values:

```json
{
"TOKEN": "",
"YOUTUBE_API_KEY": "",
"PREFIX": "/"
}
```

## Features & Commands

* Play music from YouTube via url
* `/play https://www.youtube.com/watch?v=GLvohMXgcBo`
* Play music from YouTube via search query
* `/play under the bridge red hot chili peppers`
* Play youtube playlists via url
* `/playlist https://www.youtube.com/watch?v=YlUKcNNmywk&list=PL5RNCwK3GIO13SR_o57bGJCEmqFAwq82c`
* Command Handler from [discordjs.guide](https://discordjs.guide/)
* Queue system
* Volume control
* Pause
* Resume
* Skip

## Contributing

1. [Fork the repository](https://github.com/eritislami/evobot/fork)
2. Clone your fork: `git clone https://github.com/your-username/evobot.git`
3. Create your feature branch: `git checkout -b my-new-feature`
4. Commit your changes: `git commit -am 'Add some feature'`
5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request

## Credits

[@iCrawl](https://github.com/iCrawl) For the queue system used in this application which was adapted from [@iCrawl/discord-music-bot](https://github.com/iCrawl/discord-music-bot)
15 changes: 15 additions & 0 deletions commands/pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
name: "pause",
description: "Pause the currently playing music",
execute(message) {
if (!message.member.voice.channel) return message.reply("You need to join a voice channel first!").catch(console.error)

const serverQueue = message.client.queue.get(message.guild.id)
if (serverQueue && serverQueue.playing) {
serverQueue.playing = false
serverQueue.connection.dispatcher.pause()
return serverQueue.textChannel.send(`${message.author} ⏸ paused the music.`).catch(console.error)
}
return message.reply("There is nothing playing.").catch(console.error)
}
}
102 changes: 102 additions & 0 deletions commands/play.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { Util } = require("discord.js"),
{ YOUTUBE_API_KEY } = require("../config.json"),
ytdl = require("ytdl-core"),
ytdlDiscord = require("ytdl-core-discord"),
YouTubeAPI = require("simple-youtube-api"),
youtube = new YouTubeAPI(YOUTUBE_API_KEY)

module.exports = {
name: "play",
description: "Plays audio from YouTube",
async execute(message, args) {
const { channel } = message.member.voice

if (!args.length) return message.reply("Usage: /play <YouTube URL | Video Name>").catch(console.error)
if (!channel) return message.reply("You need to join a voice channel first!").catch(console.error)

const permissions = channel.permissionsFor(message.client.user)
if (!permissions.has("CONNECT")) return message.reply("Cannot connect to voice channel, missing permissions")
if (!permissions.has("SPEAK")) return message.reply("I cannot speak in this voice channel, make sure I have the proper permissions!")

const search = args.join(" ")
const pattern = /^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/ig;
const url = args[0]
const urlValid = pattern.test(args[0])

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

let songInfo = null
let song = null

if (urlValid) {
try {
songInfo = await ytdl.getInfo(url)
song = {
title: Util.escapeMarkdown(songInfo.title),
url: songInfo.video_url
}
} catch (error) {
console.error(error)
}
} else {
let results = null
try {
results = await youtube.searchVideos(search, 1)
songInfo = await ytdl.getInfo(results[0].url)
song = {
title: Util.escapeMarkdown(songInfo.title),
url: songInfo.video_url
}
} catch (error) {
console.error(error)
}
}

if (serverQueue) {
serverQueue.songs.push(song)
return serverQueue.textChannel.send(`✅ **${song.title}** has been added to the queue by ${message.author}`).catch(console.error)
}

const queueConstruct = {
textChannel: message.channel,
channel,
connection: null,
songs: [],
volume: 100,
playing: true
}

message.client.queue.set(message.guild.id, queueConstruct)
queueConstruct.songs.push(song)

const play = async song => {
const queue = message.client.queue.get(message.guild.id)
if (!song) {
queue.channel.leave()
message.client.queue.delete(message.guild.id)
return queue.textChannel.send("🚫 Music queue ended.").catch(console.error)
}

const options = { filter: "audioonly", quality: "highestaudio" }
const dispatcher = queue.connection.play(await ytdlDiscord(song.url, options), { type: "opus", passes: 3 })
.on("end", () => {
queue.songs.shift()
play(queue.songs[0])
})
.on("error", error => console.error(error))
dispatcher.setVolumeLogarithmic(queue.volume / 100)
queue.textChannel.send(`🎶 Started playing: **${song.title}** ${song.url}`).catch(console.error)
}

try {
const connection = await channel.join()
queueConstruct.connection = connection
play(queueConstruct.songs[0])
} catch (error) {
console.error(`Could not join voice channel: ${error}`)
message.client.queue.delete(message.guild.id)
await channel.leave()
return message.channel.send(`Could not join the channel: ${error}`).catch(console.error)
}
}
}
98 changes: 98 additions & 0 deletions commands/playlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const { YOUTUBE_API_KEY } = require("../config.json"),
ytdlDiscord = require("ytdl-core-discord"),
YouTubeAPI = require("simple-youtube-api"),
youtube = new YouTubeAPI(YOUTUBE_API_KEY)

module.exports = {
name: "playlist",
description: "Play a playlist from youtube",
async execute(message, args) {
const { channel } = message.member.voice

if (!args.length) return message.reply("Usage: /playlist <YouTube Playlist URL>").catch(console.error)
if (!channel) return message.reply("You need to join a voice channel first!").catch(console.error)

const permissions = channel.permissionsFor(message.client.user)
if (!permissions.has("CONNECT")) return message.reply("Cannot connect to voice channel, missing permissions")
if (!permissions.has("SPEAK")) return message.reply("I cannot speak in this voice channel, make sure I have the proper permissions!")

const pattern = /^.*(youtu.be\/|list=)([^#\&\?]*).*/ig;
const url = args[0]
const urlValid = pattern.test(args[0])
if (!urlValid) return message.reply("Invalid youtube playlist url").catch(console.error)

const serverQueue = message.client.queue.get(message.guild.id)
const queueConstruct = {
textChannel: message.channel,
channel,
connection: null,
songs: [],
volume: 100,
playing: true
}

let song = null
let playlist = null
let videos = []

try {
playlist = await youtube.getPlaylist(url, { part: "snippet" })
videos = await playlist.getVideos(undefined, { part: "snippet" })
console.log(videos[0])
} catch (error) {
console.error(error)
}

videos.forEach((video, index) => {
song = {
index: index+1,
title: video.title,
url: video.url
}

if (serverQueue) {
serverQueue.songs.push(song)
message.channel.send(`✅ **${song.title}** has been added to the queue by ${message.author}`).catch(console.error)
} else {
queueConstruct.songs.push(song)
}
})

if (!serverQueue) message.client.queue.set(message.guild.id, queueConstruct)

message.channel.send(`${message.author} 📃 Added a playlist - **${playlist.title}** <${playlist.url}>
${queueConstruct.songs.map(song => song.index + ". " + song.title).join("\n")}
`, { split: true }).catch(console.error)

const play = async song => {
const queue = message.client.queue.get(message.guild.id)
if (!song) {
queue.channel.leave()
message.client.queue.delete(message.guild.id)
return queue.textChannel.send("🚫 Music queue ended.").catch(console.error)
}

const options = { filter: "audioonly", quality: "highestaudio" }
const dispatcher = queue.connection.play(await ytdlDiscord(song.url, options), { type: "opus", passes: 3 })
.on("end", () => {
queue.songs.shift()
play(queue.songs[0])
})
.on("error", error => console.log(error))
dispatcher.setVolumeLogarithmic(queue.volume / 100)
queue.textChannel.send(`🎶 Started playing: **${song.title}** ${song.url}`).catch(console.error)
}

try {
const connection = await channel.join()
queueConstruct.connection = connection
play(queueConstruct.songs[0])
} catch (error) {
console.error(`Could not join voice channel: ${error}`)
message.client.queue.delete(message.guild.id)
await channel.leave()
return message.channel.send(`Could not join the channel: ${error}`).catch(console.error)
}
}
}
14 changes: 14 additions & 0 deletions commands/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
name: "queue",
description: "Show the music queue and now playing.",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id)
if (!serverQueue) return message.reply("There is nothing playing.").catch(console.error)
return message.reply(`📃 **Song queue**
${serverQueue.songs.map(song => song.index + ". " + song.title).join('\n')}
Now playing: **${serverQueue.songs[0].title}**
`, { split: true }).catch(console.error)
}
}
16 changes: 16 additions & 0 deletions commands/resume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
name: "resume",
description: "Resume currently playing music",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id)

if (!message.member.voice.channel) return message.reply("You need to join a voice channel first!").catch(console.error)

if (serverQueue && !serverQueue.playing) {
serverQueue.playing = true
serverQueue.connection.dispatcher.resume()
return serverQueue.textChannel.send(`${message.author} ▶ resumed the music!`).catch(console.error)
}
return message.reply("There is nothing playing.").catch(console.error)
}
}
13 changes: 13 additions & 0 deletions commands/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
name: "skip",
description: "Skip the currently playing song",
async execute(message) {
const serverQueue = message.client.queue.get(message.guild.id)

if (!message.member.voice.channel) return message.reply("You need to join a voice channel first!").catch(console.error)
if (!serverQueue) return message.channel.send("There is nothing playing that I could skip for you.").catch(console.error)

serverQueue.connection.dispatcher.end()
serverQueue.textChannel.send(`${message.author} ⏩ skipped the song`).catch(console.error)
}
}
14 changes: 14 additions & 0 deletions commands/stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
name: "stop",
description: "Stops the music",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id)

if (!message.member.voice.channel) return message.reply("You need to join a voice channel first!").catch(console.error)
if (!serverQueue) return message.reply("There is nothing playing.").catch(console.error)

serverQueue.songs = []
serverQueue.connection.dispatcher.end()
serverQueue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error)
}
}
17 changes: 17 additions & 0 deletions commands/volume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
name: "volume",
description: "Change volume of currentply playing voiceConnection",
execute(message, args) {
const serverQueue = message.client.queue.get(message.guild.id)

if (!message.member.voice.channel) return message.reply("You need to join a voice channel first!").catch(console.error)
if (!serverQueue) return message.reply("There is nothing playing.").catch(console.error)

if (!args[0]) return message.reply(`🔊 The current volume is: **${serverQueue.volume}**`).catch(console.error)

serverQueue.volume = args[0]
serverQueue.connection.dispatcher.setVolumeLogarithmic(args[0] / 100)

return serverQueue.textChannel.send(`Volume set to: **${args[0]}%**`).catch(console.error)
}
}
5 changes: 5 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"TOKEN": "",
"YOUTUBE_API_KEY": "",
"PREFIX": "/"
}
Loading

0 comments on commit e0f145a

Please sign in to comment.