Skip to content

Instantly share code, notes, and snippets.

View galnir's full-sized avatar
🎯
Focusing

Nir galnir

🎯
Focusing
  • Tel Aviv, Israel
  • 22:47 (UTC +02:00)
View GitHub Profile
{
"workbench.startupEditor": "welcomePage",
"window.zoomLevel": 3,
"workbench.colorTheme": "Atom One Dark",
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
"files.associations": {
"*.ejs": "html"
},
"editor.fontSize": 17,
"explorer.confirmDragAndDrop": false,
console.log(`Bot is ready and working in ${client.guilds.cache.size} servers with ${client.users.cache.size} users!`);
function playSong(queue, message) {
let voiceChannel;
queue[0].voiceChannel
.join() // join the voice channel the user is in
.then(connection => {
const dispatcher = connection // sends voice packet data to the voice connection
.play(
ytdl(queue[0].url, { // provide ytdl library with the song url
volume: 0.1,
quality: 'highestaudio', // highest audio quality
try {
/*
call 'searchVideos' method to get a list of 5 video objects that match the
query. Then create an array of the 5 videos numbered from 1 to 5 with their
titles.
*/
const videos = await youtube.searchVideos(query, 5);
const vidNameArr = [];
for (let i = 0; i < videos.length; i++) {
vidNameArr.push(`${i + 1}: ${videos[i].title}`);
if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/)) {
const url = query;
try { // using try catch because there are many api calls and as a result -async await usage
/*
the 'replace' and 'split' methods create an array that looks
like this: [ 'https://www.youtube.com/watch?', 'v=', 'dQw4w9WgXcQ' ]
then we declare an 'id' variable and assign it to the 3rd element
*/
query = query
.replace(/(>|<)/gi, '')
module.exports = class PlayCommand extends Command {
constructor(client) {
super(client, {
name: 'play',
aliases: ['play-song', 'add'],
memberName: 'play',
group: 'music', // this means the folder the file is inside
description: 'Play any song from youtube',
guildOnly: true, // make this command available only in servers not dm's
clientPermissions: ['SPEAK', 'CONNECT'],
if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/)) {
const url = query;
try {
query = query
.replace(/(>|<)/gi, '')
.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
const id = query[2].split(/[^0-9a-z_\-]/i)[0];
const video = await youtube.getVideoByID(id);
if (video.raw.snippet.liveBroadcastContent === 'live')
return message.say("I don't support live streams!");
async run(message, { query }) {
 // determine if the user is in a voice channel when calling the command
 var voiceChannel = message.member.voice.channel;
 if (!voiceChannel) return message.say('Join a channel and try again');
 // if not, return the above message
module.exports = class PlayCommand extends Command {
constructor(client) {
super(client, {
name: 'play',
aliases: ['play-song', 'add'],
memberName: 'play',
group: 'music',
description: 'Play any song from youtube',
guildOnly: true,
clientPermissions: ['SPEAK', 'CONNECT'],
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const Youtube = require('simple-youtube-api');
const ytdl = require('ytdl-core');
const youtube = new Youtube(youtubeApiKey); // insert here your Youtube API key, you can also store it as an environment variable or in a config.json
var queue = []; // in this array we will store songs in queue
var isPlaying; // we will use this variable to determine if a song is playing