2019-09-13 20:02:41 +00:00
const client = require ( "./client.js" ) ;
2019-10-24 18:54:23 +00:00
const fs = require ( "fs" ) ;
const logger = require ( "./logger.js" ) ;
2019-11-18 01:57:12 +00:00
const connections = require ( "./collections.js" ) . voiceConnections ;
2019-09-13 20:02:41 +00:00
module . exports = async ( sound , message ) => {
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! ` ) ;
2019-11-18 01:57:12 +00:00
const checkConnection = connections . get ( message . channel . guild . id ) ;
const connection = checkConnection ? checkConnection : await voiceChannel . join ( ) ;
2019-11-11 20:54:19 +00:00
const playingMessage = await client . createMessage ( message . channel . id , "🔊 Playing sound..." ) ;
2019-11-18 01:57:12 +00:00
connections . set ( message . channel . guild . id , connection ) ;
2019-09-13 20:02:41 +00:00
if ( connection . playing ) {
connection . stopPlaying ( ) ;
}
2019-11-18 01:57:12 +00:00
connection . play ( fs . createReadStream ( sound ) ) ;
2019-09-13 20:02:41 +00:00
connection . on ( "error" , ( error ) => {
2019-11-18 01:57:12 +00:00
connections . delete ( message . channel . guild . id ) ;
2019-09-13 20:02:41 +00:00
voiceChannel . leave ( ) ;
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
} ) ;
2019-11-18 01:57:12 +00:00
connection . on ( "warn" , ( warn ) => {
logger . warn ( warn ) ;
} ) ;
2019-09-13 20:02:41 +00:00
connection . once ( "end" , ( ) => {
2019-11-18 01:57:12 +00:00
connections . delete ( message . channel . guild . id ) ;
2019-09-13 20:02:41 +00:00
voiceChannel . leave ( ) ;
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! ` ) ;
}
} ;