2024-02-09 05:22:35 +00:00
const { AudioPlayerStatus , createAudioPlayer , createAudioResource , getVoiceConnection , joinVoiceChannel , NoSubscriberBehavior } = require ( '@discordjs/voice' ) ;
const { Client , ActivityType , PermissionFlagsBits } = require ( 'discord.js' ) ;
2019-05-30 09:01:25 +00:00
const Discord = require ( 'discord.js' ) ;
const YouTube = require ( 'simple-youtube-api' )
const ytdl = require ( 'ytdl-core' ) ;
2024-02-09 05:22:35 +00:00
const PREFIX = ( process . env . BOT _PREFIX ? ? "mx>" ) ;
const client = new Client ( {
intents : [
"Guilds" ,
"GuildMessages" ,
"GuildVoiceStates" ,
"MessageContent"
] ,
disableMentions : 'everyone'
} ) ;
const youtube = new YouTube ( process . env . YOUTUBE _API _KEY ) ;
2019-05-30 09:01:25 +00:00
const queue = new Map ( ) ;
2024-02-09 05:22:35 +00:00
client . login ( process . env . DISCORD _API _TOKEN ) ;
2019-07-10 10:01:42 +00:00
client . on ( 'ready' , ( ) => {
2024-02-09 05:22:35 +00:00
client . user . setActivity ( ` ${ PREFIX } help ` , { type : ActivityType . Listening } )
client . user . setStatus ( 'online' ) ;
2019-05-30 09:01:25 +00:00
} ) ;
2024-02-09 05:22:35 +00:00
client . on ( 'messageCreate' , async msg => {
2019-07-30 07:13:04 +00:00
if ( ! msg . guild || msg . author . bot ) return ;
2019-06-09 19:31:08 +00:00
if ( msg . content . startsWith ( ` ${ PREFIX } ` ) ) {
2019-07-18 17:16:43 +00:00
if ( msg . content === ` ${ PREFIX } ping ` ) {
2024-02-09 05:22:35 +00:00
msg . channel . send ( ` My current Ping: ** ${ Math . floor ( client . ws . ping * 10 ) / 10 } ms**. ` ) ;
2019-07-18 17:16:43 +00:00
return ;
}
2019-06-09 19:31:08 +00:00
if ( msg . content === ` ${ PREFIX } help ` ) {
2024-02-09 05:22:35 +00:00
const embed = new Discord . EmbedBuilder ( )
. setTitle ( 'Commands for ' + client . user . username + '!' )
. addFields (
{ name : '```' + ` ${ PREFIX } ` + 'play | ' + ` ${ PREFIX } ` + 'p```' , value : 'Play a song.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'queue | ' + ` ${ PREFIX } ` + 'q```' , value : 'Display the queue.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'nowplaying | ' + ` ${ PREFIX } ` + 'np```' , value : 'Display whats currently playing.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'volume```' , value : 'Change or check the volume.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'pause```' , value : 'Pause the music.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'resume```' , value : 'Resume the music.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'stop```' , value : 'Stop the music, Clear the queue and leave the current voice channel.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'skip | ' + ` ${ PREFIX } ` + 's```' , value : 'Skip a song.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'invite```' , value : 'Invite ' + client . user . username + '.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'ping```' , value : 'See the current ping for ' + client . user . username , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'info```' , value : 'Display info and instructions.' , inline : true } ,
{ name : '```' + ` ${ PREFIX } ` + 'help```' , value : 'Display the help.' , inline : true }
)
. setAuthor ( { name : client . user . username , iconURL : client . user . avatarURL ( ) } )
. setColor ( '#b50002' ) ;
msg . channel . send ( { embeds : [ embed ] } ) ;
2019-07-18 17:16:43 +00:00
return undefined ;
2019-06-05 13:29:58 +00:00
}
2019-06-09 19:31:08 +00:00
if ( msg . content === ` ${ PREFIX } info ` ) {
var line = '**>-----------------------------------------------------------------------<**' ;
2024-02-09 05:22:35 +00:00
var dj = msg . guild . roles . cache . find ( x => x . name === 'DJ' ) ? true : false ;
const embed = new Discord . EmbedBuilder ( )
. setTitle ( '**' + client . user . username + ' instructions and info**:' )
. addFields (
{ name : 'If your current guild has a role called \'DJ\' you will need it to use music commands! If your current guild doesn\'t have a role called \'DJ\' everyone can use music commands!' , value : 'DJ role existance: ' + dj , inline : true } ,
{ name : 'If you encounter any errors with ' + client . user . username + ' please report about them on the offical ' + client . user . username + ' support server!' , value : 'https://discord.gg/rvHuJtB' , inline : true } ,
{ name : ` On errors you can do ${ PREFIX } stop to reset the queue and try again! ` , value : line , inline : true } ,
{ name : 'Current Ping in milliseconds' , value : ` ${ Math . floor ( client . ws . ping * 10 ) / 10 } ms ` , inline : true } ,
{ name : 'Be careful with the Volume command! Volume is not recommended to be put over 3 with user volume at 100%!' , value : 'Volume will reset to 1 always when a new song begins!' , inline : true }
)
. setAuthor ( { name : client . user . username , iconURL : client . user . avatarURL ( ) } )
. setColor ( '#b50002' ) ;
msg . channel . send ( { embeds : [ embed ] } ) ;
2019-07-18 17:16:43 +00:00
return undefined ;
2019-06-05 13:29:58 +00:00
}
2019-07-18 17:16:43 +00:00
if ( msg . content === ` ${ PREFIX } invite ` ) {
2024-02-09 05:22:35 +00:00
msg . channel . send ( 'Invite me with:' + '\n' + 'https://discord.com/oauth2/authorize?client_id=' + client . user . id + '&permissions=2184465408&scope=applications.commands+bot' ) ;
2019-07-18 17:16:43 +00:00
return undefined ;
}
2024-02-09 05:22:35 +00:00
if ( msg . member . guild . roles . cache . find ( x => x . name === 'DJ' ) ) {
if ( msg . member . roles . cache . find ( x => x . name === 'DJ' ) ) {
2019-07-18 17:16:43 +00:00
const args = msg . content . split ( ' ' ) ;
const searchString = args . slice ( 1 ) . join ( ' ' ) ;
const url = args [ 1 ] ? args [ 1 ] . replace ( /<(.+)>/g , '$1' ) : '' ;
const serverQueue = queue . get ( msg . guild . id ) ;
let command = msg . content . toLowerCase ( ) . split ( ' ' ) [ 0 ] ;
2024-02-09 05:22:35 +00:00
command = command . slice ( PREFIX . length ) ;
2019-07-18 17:16:43 +00:00
2019-07-29 14:56:30 +00:00
if ( command === 'play' || command === 'p' ) {
2019-07-18 17:16:43 +00:00
if ( ! args [ 1 ] ) return msg . channel . send ( ':x: I\'m sorry but you didn\'t specify a song' ) ;
2024-02-09 05:22:35 +00:00
const voiceChannel = msg . member . voice . channel ;
2019-07-18 17:16:43 +00:00
if ( ! voiceChannel ) return msg . channel . send ( ':x: I\'m sorry but you need to be in a voice channel to play music!' ) ;
const permissions = voiceChannel . permissionsFor ( msg . client . user ) ;
2024-02-09 05:22:35 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Connect ) ) {
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':x: I cannot connect to your voice channel, make sure I have the proper permissions!' ) ;
}
2024-02-09 05:22:35 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Speak ) ) {
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':x: I cannot speak in this voice channel, make sure I have the proper permissions!' ) ;
}
if ( url . match ( /^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/ ) ) {
const playlist = await youtube . getPlaylist ( url ) ;
const videos = await playlist . getVideos ( ) ;
for ( const video of Object . values ( videos ) ) {
2019-07-23 15:06:48 +00:00
const video2 = await youtube . getVideoByID ( video . id ) ;
await handleVideo ( video2 , msg , voiceChannel , true ) ;
2019-07-18 17:16:43 +00:00
}
return msg . channel . send ( ` :white_check_mark: Playlist: ** ${ playlist . title } ** has been added to the queue! ` ) ;
} else {
try {
var video = await youtube . getVideo ( url ) ;
} catch ( error ) {
try {
var videos = await youtube . searchVideos ( searchString , 10 ) ;
let index = 0 ;
msg . channel . send ( `
2019-05-30 11:04:07 +00:00
_ _ * * Song selection : * * _ _
2019-05-30 18:05:16 +00:00
$ { videos . map ( video2 => ` ** ${ ++ index } -** ${ video2 . title } ` ) . join ( '\n' ) }
2024-02-09 05:22:35 +00:00
Please provide a value to select one of the search results ranging from 1 - 10.
2019-05-30 11:04:07 +00:00
` );
2024-02-09 05:22:35 +00:00
2019-07-18 17:16:43 +00:00
try {
2024-02-09 05:22:35 +00:00
var response = await msg . channel . awaitMessages ( {
filter : msg2 => msg2 . content > 0 && msg2 . content < 11 ,
max : 1 ,
2019-07-18 17:16:43 +00:00
time : 10000 ,
errors : [ 'time' ]
} ) ;
} catch ( err ) {
2019-07-31 06:55:31 +00:00
return msg . channel . send ( ':x: Cancelling song selection.' ) ;
2019-07-18 17:16:43 +00:00
}
const videoIndex = parseInt ( response . first ( ) . content ) ;
2024-02-09 05:22:35 +00:00
2019-07-18 17:16:43 +00:00
var video = await youtube . getVideoByID ( videos [ videoIndex - 1 ] . id ) ;
} catch ( err ) {
return msg . channel . send ( ':x: I could not obtain any search results.' ) ;
}
2019-05-30 11:04:07 +00:00
}
2019-07-18 17:16:43 +00:00
return handleVideo ( video , msg , voiceChannel ) ;
2019-05-30 09:01:25 +00:00
}
2019-07-29 14:56:30 +00:00
} else if ( command === 'skip' || command === 's' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing that I could skip for you.' ) ;
2019-07-23 06:30:56 +00:00
if ( ! serverQueue . songs [ 1 ] ) return msg . channel . send ( ':x: Theres nothing to skip to!' )
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . stop ( ) ;
2019-07-23 15:06:48 +00:00
return ;
2019-07-18 17:16:43 +00:00
} else if ( command === 'stop' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing that I could stop for you.' ) ;
serverQueue . songs = [ ] ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . stop ( ) ;
2019-07-23 15:06:48 +00:00
return ;
2019-07-18 17:16:43 +00:00
} else if ( command === 'volume' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing.' ) ;
if ( ! args [ 1 ] ) return msg . channel . send ( ` The current volume is: ** ${ serverQueue . volume } ** :speaker: ` ) ;
if ( isNaN ( args [ 1 ] ) ) {
return msg . channel . send ( ':x: I\'m sorry, But you need to enter a valid __number__.' )
}
serverQueue . volume = args [ 1 ] ;
2024-02-09 05:22:35 +00:00
serverQueue . audioResource . volume . setVolume ( args [ 1 ] / 100 ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ` I set the volume to: ** ${ args [ 1 ] } ** 🔊 ` ) ;
2019-07-30 05:01:29 +00:00
} else if ( command === 'np' || command === 'nowplaying' ) {
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing.' ) ;
return msg . channel . send ( ` :musical_note: Now playing: ** ${ serverQueue . songs [ 0 ] . title } ** ` ) ;
2019-07-30 05:01:29 +00:00
} else if ( command === "queue" || command === 'q' ) {
2019-07-18 17:16:43 +00:00
if ( ! serverQueue )
return msg . channel . send ( ":x: There is nothing in the queue." ) ;
var queuemessage = ` __**Song queue:**__
2019-07-19 09:20:22 +00:00
$ { serverQueue . songs . map ( song => ` **-** ${ song . title } ` ) . join ( "\n" ) }
* * Now playing : * * $ { serverQueue . songs [ 0 ] . title } : musical _note : ` ;
2019-07-19 08:09:38 +00:00
if ( queuemessage . length > 2000 ) {
2019-07-19 09:20:22 +00:00
return msg . channel . send ( ":x: The queue has too many songs in it to show all in this channel. Try again after a few songs." ) ;
2019-07-18 17:16:43 +00:00
}
return msg . channel . send ( queuemessage ) ;
} else if ( command === 'pause' ) {
if ( serverQueue && serverQueue . playing ) {
serverQueue . playing = false ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . pause ( ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':pause_button: Paused the music for you!' ) ;
}
return msg . channel . send ( ':x: There is nothing playing.' ) ;
} else if ( command === 'resume' ) {
if ( serverQueue && ! serverQueue . playing ) {
serverQueue . playing = true ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . unpause ( ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':play_pause: Resumed the music for you!' ) ;
}
return msg . channel . send ( ':x: There is nothing playing.' ) ;
}
if ( msg . content === ` ${ PREFIX } ` ) return ;
2024-02-09 05:25:29 +00:00
msg . channel . send ( ` :x: Unknown command! Type ${ PREFIX } help for the list of commands! ` )
2019-07-23 15:06:48 +00:00
return ;
2019-05-30 11:04:07 +00:00
}
2019-07-18 17:16:43 +00:00
if ( msg . content === ` ${ PREFIX } ` ) return ;
2024-02-09 05:22:35 +00:00
var coms = [ ` ${ PREFIX } play ` , ` ${ PREFIX } queue ` , ` ${ PREFIX } np ` , ` ${ PREFIX } volume ` , ` ${ PREFIX } pause ` , ` ${ PREFIX } resume ` , ` ${ PREFIX } stop ` , ` ${ PREFIX } skip ` , ` ${ PREFIX } ping ` , ` ${ PREFIX } q ` , ` ${ PREFIX } nowplaying ` , ` ${ PREFIX } p ` , ` ${ PREFIX } s ` ] ;
2019-07-18 17:16:43 +00:00
for ( var i = 0 ; i < coms . length ; i ++ ) {
if ( msg . content . includes ( coms [ i ] ) ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . roles . cache . find ( x => x . name === 'DJ' ) ) {
2019-07-18 17:16:43 +00:00
msg . channel . send ( ':x: i\'m sorry but you need to have the \'DJ\' role to use music commands!' )
return ;
}
}
2019-05-30 11:04:07 +00:00
}
2024-02-09 05:25:29 +00:00
msg . channel . send ( ` :x: Unknown command! Type ${ PREFIX } help for the list of commands! ` )
2019-06-16 13:25:12 +00:00
return ;
2019-07-18 17:19:28 +00:00
} else {
2019-07-18 17:16:43 +00:00
const args = msg . content . split ( ' ' ) ;
const searchString = args . slice ( 1 ) . join ( ' ' ) ;
const url = args [ 1 ] ? args [ 1 ] . replace ( /<(.+)>/g , '$1' ) : '' ;
const serverQueue = queue . get ( msg . guild . id ) ;
let command = msg . content . toLowerCase ( ) . split ( ' ' ) [ 0 ] ;
command = command . slice ( PREFIX . length )
2019-07-29 14:56:30 +00:00
if ( command === 'play' || command === 'p' ) {
2019-07-18 17:16:43 +00:00
if ( ! args [ 1 ] ) return msg . channel . send ( ':x: I think you forgot what you wanted to play!' ) ;
2024-02-09 05:22:35 +00:00
const voiceChannel = msg . member . voice . channel ;
2019-07-18 17:16:43 +00:00
if ( ! voiceChannel ) return msg . channel . send ( ':x: I\'m sorry but you need to be in a voice channel to play music!' ) ;
const permissions = voiceChannel . permissionsFor ( msg . client . user ) ;
2024-02-09 05:22:35 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Connect ) ) {
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':x: I cannot connect to your voice channel, make sure I have the proper permissions!' ) ;
2019-05-30 11:04:07 +00:00
}
2024-02-09 05:22:35 +00:00
if ( ! permissions . has ( PermissionFlagsBits . Speak ) ) {
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':x: I cannot speak in this voice channel, make sure I have the proper permissions!' ) ;
}
if ( url . match ( /^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/ ) ) {
const playlist = await youtube . getPlaylist ( url ) ;
const videos = await playlist . getVideos ( ) ;
for ( const video of Object . values ( videos ) ) {
const video2 = await youtube . getVideoByID ( video . id ) ; // eslint-disable-line no-await-in-loop
await handleVideo ( video2 , msg , voiceChannel , true ) ; // eslint-disable-line no-await-in-loop
}
return msg . channel . send ( ` :white_check_mark: Playlist: ** ${ playlist . title } ** has been added to the queue! ` ) ;
} else {
2019-05-30 11:04:07 +00:00
try {
2019-07-18 17:16:43 +00:00
var video = await youtube . getVideo ( url ) ;
} catch ( error ) {
try {
var videos = await youtube . searchVideos ( searchString , 10 ) ;
let index = 0 ;
msg . channel . send ( `
2019-05-30 11:04:07 +00:00
_ _ * * Song selection : * * _ _
2019-06-01 06:23:23 +00:00
$ { videos . map ( video2 => ` ** ${ ++ index } -** ${ video2 . title } ` ) . join ( '\n' ) }
Please provide a value to select one of the search results ranging from 1 - 10.
2019-05-30 11:04:07 +00:00
` );
2019-07-18 17:16:43 +00:00
// eslint-disable-next-line max-depth
try {
var response = await msg . channel . awaitMessages ( msg2 => msg2 . content > 0 && msg2 . content < 11 , {
maxMatches : 1 ,
time : 10000 ,
errors : [ 'time' ]
} ) ;
} catch ( err ) {
return msg . channel . send ( ':x: No or invalid value entered, cancelling video selection.' ) ;
}
const videoIndex = parseInt ( response . first ( ) . content ) ;
var video = await youtube . getVideoByID ( videos [ videoIndex - 1 ] . id ) ;
2019-05-30 11:04:07 +00:00
} catch ( err ) {
2019-07-18 17:16:43 +00:00
console . error ( err ) ;
return msg . channel . send ( ':x: I could not obtain any search results.' ) ;
2019-05-30 11:04:07 +00:00
}
}
2019-07-18 17:16:43 +00:00
return handleVideo ( video , msg , voiceChannel ) ;
2019-05-30 11:04:07 +00:00
}
2019-07-29 14:56:30 +00:00
} else if ( command === 'skip' || command === 's' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing that I could skip for you.' ) ;
msg . channel . send ( 'Skipped :thumbsup:' )
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . stop ( ) ;
2019-07-18 17:16:43 +00:00
return undefined ;
} else if ( command === 'stop' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing that I could stop for you.' ) ;
msg . channel . send ( 'Stopped the music! :stop_button:' )
serverQueue . songs = [ ] ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . stop ( ) ;
2019-07-18 17:16:43 +00:00
return undefined ;
} else if ( command === 'volume' ) {
2024-02-09 05:22:35 +00:00
if ( ! msg . member . voice . channel ) return msg . channel . send ( ':x: You are not in a voice channel!' ) ;
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing.' ) ;
if ( ! args [ 1 ] ) return msg . channel . send ( ` The current volume is: ** ${ serverQueue . volume } ** :speaker: ` ) ;
if ( isNaN ( args [ 1 ] ) ) {
return msg . channel . send ( ':x: I\'m sorry, But you need to enter a valid __number__.' )
}
serverQueue . volume = args [ 1 ] ;
2024-02-09 05:22:35 +00:00
serverQueue . audioResource . volume . setVolume ( args [ 1 ] / 100 ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ` I set the volume to: ** ${ args [ 1 ] } ** 🔊 ` ) ;
2019-07-30 05:01:29 +00:00
} else if ( command === 'np' || command === 'nowplaying' ) {
2019-07-18 17:16:43 +00:00
if ( ! serverQueue ) return msg . channel . send ( ':x: There is nothing playing.' ) ;
return msg . channel . send ( ` :musical_note: Now playing: ** ${ serverQueue . songs [ 0 ] . title } ** ` ) ;
2019-07-30 05:01:29 +00:00
} else if ( command === "queue" || command === 'q' ) {
2019-07-18 17:16:43 +00:00
if ( ! serverQueue )
return msg . channel . send ( ":x: There is nothing in the queue." ) ;
var queuemessage = ` __**Song queue:**__
2019-07-23 15:06:48 +00:00
$ { serverQueue . songs . map ( song => ` **-** ${ song . title } ` ) . join ( "\n" ) }
* * Now playing : * * $ { serverQueue . songs [ 0 ] . title } : musical _note : ` ;
2019-07-18 17:16:43 +00:00
if ( queuemessage . length < 2000 ) {
2019-07-19 07:19:00 +00:00
return msg . channel . send ( ":x: The queue has too many songs in it to show all in this channel. Try again after a few songs" ) ;
2019-07-18 17:16:43 +00:00
}
return msg . channel . send ( queuemessage ) ;
} else if ( command === 'pause' ) {
if ( serverQueue && serverQueue . playing ) {
serverQueue . playing = false ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . pause ( ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':pause_button: Paused the music for you!' ) ;
}
return msg . channel . send ( ':x: There is nothing playing.' ) ;
} else if ( command === 'resume' ) {
if ( serverQueue && ! serverQueue . playing ) {
serverQueue . playing = true ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . unpause ( ) ;
2019-07-18 17:16:43 +00:00
return msg . channel . send ( ':play_pause: Resumed the music for you!' ) ;
}
return msg . channel . send ( ':x: There is nothing playing.' ) ;
2019-07-30 13:34:58 +00:00
}
2019-07-30 14:14:23 +00:00
}
if ( msg . content === ` ${ PREFIX } ` ) {
return ;
}
2024-02-09 05:25:29 +00:00
msg . channel . send ( ` :x: Unknown command! Type ${ PREFIX } help for the list of commands! ` )
2019-07-30 14:14:23 +00:00
return ;
2019-07-30 13:34:58 +00:00
}
2019-05-30 09:01:25 +00:00
} ) ;
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
async function handleVideo ( video , msg , voiceChannel , playlist = false ) {
const serverQueue = queue . get ( msg . guild . id ) ;
const song = {
id : video . id ,
2024-02-09 05:22:35 +00:00
title : video . title ,
2019-05-30 09:01:25 +00:00
url : ` https://www.youtube.com/watch?v= ${ video . id } `
} ;
if ( ! serverQueue ) {
const queueConstruct = {
textChannel : msg . channel ,
voiceChannel : voiceChannel ,
connection : null ,
2024-02-09 05:22:35 +00:00
audioPlayer : createAudioPlayer ( {
behaviors : {
noSubscriber : NoSubscriberBehavior . Play ,
}
} ) ,
audioResource : null ,
2019-05-30 09:01:25 +00:00
songs : [ ] ,
2024-02-09 05:22:35 +00:00
volume : 50 ,
2019-05-30 09:01:25 +00:00
playing : true
} ;
2024-02-09 05:22:35 +00:00
2019-05-30 09:01:25 +00:00
queue . set ( msg . guild . id , queueConstruct ) ;
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
queueConstruct . songs . push ( song ) ;
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
try {
2024-02-09 05:22:35 +00:00
const connection =
getVoiceConnection ( voiceChannel . guild . id ) ? ?
joinVoiceChannel ( {
channelId : voiceChannel . id ,
guildId : voiceChannel . guild . id ,
adapterCreator : voiceChannel . guild . voiceAdapterCreator
} ) ;
2019-05-30 09:01:25 +00:00
queueConstruct . connection = connection ;
play ( msg . guild , queueConstruct . songs [ 0 ] ) ;
} catch ( error ) {
2019-07-10 10:11:50 +00:00
console . error ( ` I could not join the voice channel: ${ error } ` ) ;
2019-05-30 09:01:25 +00:00
queue . delete ( msg . guild . id ) ;
return msg . channel . send ( ` :x: I could not join the voice channel: ${ error } ` ) ;
}
} else {
serverQueue . songs . push ( song ) ;
if ( playlist ) return undefined ;
else return msg . channel . send ( ` :white_check_mark: ** ${ song . title } ** has been added to the queue! ` ) ;
}
return undefined ;
}
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
function play ( guild , song ) {
const serverQueue = queue . get ( guild . id ) ;
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
if ( ! song ) {
2024-02-09 05:22:35 +00:00
serverQueue . connection . destroy ( ) ;
2019-05-30 09:01:25 +00:00
queue . delete ( guild . id ) ;
return ;
}
2019-07-10 10:11:50 +00:00
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer
. on ( AudioPlayerStatus . Idle , ( ) => {
2019-05-30 09:01:25 +00:00
serverQueue . songs . shift ( ) ;
2024-02-09 05:22:35 +00:00
serverQueue . audioPlayer . removeAllListeners ( ) ;
2019-05-30 09:01:25 +00:00
play ( guild , serverQueue . songs [ 0 ] ) ;
} )
2024-02-09 05:22:35 +00:00
. on ( 'error' , ( error ) => {
console . error ( error )
} ) ;
const audioResource = createAudioResource ( ytdl ( song . url , { quality : ` highestaudio ` , filter : 'audioonly' } ) , {
inlineVolume : true
} ) ;
audioResource . volume . setVolume ( serverQueue . volume / 100 ) ;
serverQueue . audioPlayer . play ( audioResource ) ;
serverQueue . audioResource = audioResource ;
serverQueue . connection . subscribe ( serverQueue . audioPlayer ) ;
2019-07-10 10:11:50 +00:00
2019-05-30 09:01:25 +00:00
serverQueue . textChannel . send ( ` :musical_note: Start playing: ** ${ song . title } ** ` ) ;
2024-02-09 05:22:35 +00:00
}