2019-09-13 20:02:41 +00:00
const client = require ( "./client.js" ) ;
2019-10-24 18:54:23 +00:00
const logger = require ( "./logger.js" ) ;
2020-06-27 17:18:26 +00:00
const fetch = require ( "node-fetch" ) ;
const { Manager } = require ( "@lavacord/eris" ) ;
2019-09-13 20:02:41 +00:00
2020-06-27 17:18:26 +00:00
const nodes = [
{ id : "1" , host : "localhost" , port : 2333 , password : process . env . LAVAPASS }
] ;
let manager ;
exports . connect = async ( ) => {
manager = new Manager ( client , nodes , {
user : client . user . id
} ) ;
const { length } = await manager . connect ( ) ;
logger . log ( ` Successfully connected to ${ length } Lavalink node(s). ` ) ;
manager . on ( "error" , ( error , node ) => {
logger . error ( ` An error occurred on Lavalink node ${ node } : ${ error } ` ) ;
} ) ;
} ;
exports . play = async ( sound , message ) => {
2019-09-13 20:02:41 +00:00
if ( message . member . voiceState . channelID ) {
if ( ! message . channel . guild . members . get ( client . user . id ) . permission . has ( "voiceConnect" ) || ! message . channel . permissionsOf ( client . user . id ) . has ( "voiceConnect" ) ) return client . createMessage ( message . channel . id , ` ${ message . author . mention } , I can't join this voice channel! ` ) ;
const voiceChannel = message . channel . guild . channels . get ( message . member . voiceState . channelID ) ;
2019-11-11 20:54:19 +00:00
if ( ! voiceChannel . permissionsOf ( client . user . id ) . has ( "voiceConnect" ) ) return client . createMessage ( message . channel . id , ` ${ message . author . mention } , I don't have permission to join this voice channel! ` ) ;
2020-06-27 17:18:26 +00:00
const node = manager . idealNodes [ 0 ] ;
const { tracks } = await fetch ( ` http:// ${ node . host } : ${ node . port } /loadtracks?identifier= ${ sound } ` , { headers : { Authorization : node . password } } ) . then ( res => res . json ( ) ) ;
const connection = await manager . join ( {
guild : voiceChannel . guild . id ,
channel : voiceChannel . id ,
node : node . id
2019-12-07 02:04:17 +00:00
} ) ;
2019-11-11 20:54:19 +00:00
const playingMessage = await client . createMessage ( message . channel . id , "🔊 Playing sound..." ) ;
2020-06-27 17:18:26 +00:00
await connection . play ( tracks [ 0 ] . track ) ;
2019-09-13 20:02:41 +00:00
connection . on ( "error" , ( error ) => {
2020-06-27 17:18:26 +00:00
manager . leave ( voiceChannel . guild . id ) ;
2019-11-11 20:54:19 +00:00
playingMessage . delete ( ) ;
2019-10-24 18:54:23 +00:00
logger . error ( error ) ;
2019-09-13 20:02:41 +00:00
} ) ;
2020-06-27 17:18:26 +00:00
connection . once ( "end" , ( data ) => {
if ( data . reason === "REPLACED" ) return ;
manager . leave ( voiceChannel . guild . id ) ;
2019-11-11 20:54:19 +00:00
playingMessage . delete ( ) ;
2019-09-13 20:02:41 +00:00
} ) ;
} else {
client . createMessage ( message . channel . id , ` ${ message . author . mention } , you need to be in a voice channel first! ` ) ;
}
} ;