2021-08-19 14:19:14 +00:00
import * as logger from "./logger.js" ;
import fetch from "node-fetch" ;
import fs from "fs" ;
import format from "format-duration" ;
2022-06-14 05:38:01 +00:00
import { Shoukaku , Connectors } from "shoukaku" ;
2020-06-27 17:18:26 +00:00
2021-08-19 14:19:14 +00:00
export const players = new Map ( ) ;
export const queues = new Map ( ) ;
export const skipVotes = new Map ( ) ;
2020-07-06 20:19:30 +00:00
2021-08-19 14:19:14 +00:00
export let manager ;
2022-06-14 05:38:01 +00:00
export let nodes ;
2021-08-19 14:19:14 +00:00
export let status = false ;
export let connected = false ;
2020-07-16 14:28:09 +00:00
2021-08-19 14:19:14 +00:00
export async function checkStatus ( ) {
2022-03-12 02:28:35 +00:00
const json = await fs . promises . readFile ( new URL ( "../servers.json" , import . meta . url ) , { encoding : "utf8" } ) ;
2021-03-05 21:13:54 +00:00
nodes = JSON . parse ( json ) . lava ;
2020-12-11 22:46:05 +00:00
const newNodes = [ ] ;
2020-06-27 21:34:31 +00:00
for ( const node of nodes ) {
try {
2022-06-14 05:38:01 +00:00
const response = await fetch ( ` http:// ${ node . url } /version ` , { headers : { Authorization : node . auth } } ) . then ( res => res . text ( ) ) ;
2020-12-11 22:46:05 +00:00
if ( response ) newNodes . push ( node ) ;
2020-06-27 21:34:31 +00:00
} catch {
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
logger . error ( ` Failed to get status of Lavalink node ${ node . host } . ` ) ;
2020-06-27 21:34:31 +00:00
}
}
2020-12-11 22:46:05 +00:00
nodes = newNodes ;
2021-08-19 14:19:14 +00:00
status = newNodes . length === 0 ? true : false ;
return status ;
}
2020-06-27 21:34:31 +00:00
2021-08-19 14:19:14 +00:00
export async function connect ( client ) {
2022-06-14 05:38:01 +00:00
manager = new Shoukaku ( new Connectors . Eris ( client ) , nodes ) ;
client . emit ( "ready" ) ; // workaround
manager . on ( "error" , ( node , error ) => {
2020-06-27 17:18:26 +00:00
logger . error ( ` An error occurred on Lavalink node ${ node } : ${ error } ` ) ;
} ) ;
2022-06-28 05:40:54 +00:00
manager . once ( "ready" , ( ) => {
2022-06-14 05:38:01 +00:00
logger . log ( ` Successfully connected to ${ manager . nodes . size } Lavalink node(s). ` ) ;
connected = true ;
} ) ;
}
export function reload ( ) {
const activeNodes = manager . nodes ;
const names = nodes . map ( ( a ) => a . name ) ;
for ( const name in activeNodes ) {
if ( ! names . includes ( name ) ) {
manager . removeNode ( name ) ;
}
}
for ( const node of nodes ) {
if ( ! activeNodes . has ( node . name ) ) {
manager . addNode ( node ) ;
}
}
return manager . nodes . size ;
2021-08-19 14:19:14 +00:00
}
2020-06-27 17:18:26 +00:00
2022-04-05 03:05:28 +00:00
export async function play ( client , sound , options , music = false ) {
2021-08-19 14:19:14 +00:00
if ( ! manager ) return "The sound commands are still starting up!" ;
2022-04-05 03:05:28 +00:00
if ( ! options . channel . guild ) return "This command only works in servers!" ;
2022-04-06 00:03:49 +00:00
if ( ! options . member . voiceState . channelID ) return "You need to be in a voice channel first!" ;
2022-04-05 03:05:28 +00:00
if ( ! options . channel . guild . permissionsOf ( client . user . id ) . has ( "voiceConnect" ) ) return "I can't join this voice channel!" ;
2022-04-06 00:03:49 +00:00
const voiceChannel = options . channel . guild . channels . get ( options . member . voiceState . channelID ) ;
2021-05-11 16:12:01 +00:00
if ( ! voiceChannel . permissionsOf ( client . user . id ) . has ( "voiceConnect" ) ) return "I don't have permission to join this voice channel!" ;
2022-04-05 03:05:28 +00:00
const player = players . get ( options . channel . guild . id ) ;
2022-06-28 05:40:54 +00:00
if ( ! music && manager . players . has ( options . channel . guild . id ) && ( player && player . type === "music" ) ) return "I can't play a sound effect while playing music!" ;
2022-06-14 05:38:01 +00:00
let node = manager . getNode ( ) ;
2022-01-08 21:54:34 +00:00
if ( ! node ) {
const status = await checkStatus ( ) ;
if ( ! status ) {
await connect ( client ) ;
2022-06-14 05:38:01 +00:00
node = manager . getNode ( ) ;
2022-01-08 21:54:34 +00:00
}
}
2022-06-14 05:38:01 +00:00
if ( ! music && ! nodes . filter ( obj => obj . name === node . name ) [ 0 ] . local ) {
2021-01-04 16:29:18 +00:00
sound = sound . replace ( /\.\// , "https://raw.githubusercontent.com/esmBot/esmBot/master/" ) ;
}
2022-06-14 05:38:01 +00:00
let response ;
2022-01-08 21:54:34 +00:00
try {
2022-06-14 05:38:01 +00:00
response = await node . rest . resolve ( sound ) ;
if ( ! response ) return "🔊 I couldn't get a response from the audio server." ;
if ( response . loadType === "NO_MATCHES" || response . loadType === "LOAD_FAILED" ) return "I couldn't find that song!" ;
2022-01-08 21:54:34 +00:00
} catch {
return "🔊 Hmmm, seems that all of the audio servers are down. Try again in a bit." ;
}
2021-08-19 14:19:14 +00:00
const oldQueue = queues . get ( voiceChannel . guild . id ) ;
2022-06-14 05:38:01 +00:00
if ( ! response . tracks || response . tracks . length === 0 ) return "I couldn't find that song!" ;
2020-09-22 20:33:07 +00:00
if ( music ) {
2022-06-14 05:38:01 +00:00
const sortedTracks = response . tracks . map ( ( val ) => { return val . track ; } ) ;
const playlistTracks = response . playlistInfo . selectedTrack ? sortedTracks : [ sortedTracks [ 0 ] ] ;
2021-09-15 00:14:44 +00:00
queues . set ( voiceChannel . guild . id , oldQueue ? [ ... oldQueue , ... playlistTracks ] : playlistTracks ) ;
2020-09-22 20:33:07 +00:00
}
2022-06-29 01:36:32 +00:00
const connection = player && player . player && player . player . connection . state !== 3 ? player . player : await node . joinChannel ( {
2022-06-14 05:38:01 +00:00
guildId : voiceChannel . guild . id ,
channelId : voiceChannel . id ,
shardId : voiceChannel . guild . shard . id ,
deaf : true
} ) ;
2020-07-06 20:19:30 +00:00
2021-12-12 05:44:49 +00:00
if ( oldQueue && oldQueue . length !== 0 && music ) {
2022-06-14 05:38:01 +00:00
return ` Your ${ response . playlistInfo . name ? "playlist" : "tune" } \` ${ response . playlistInfo . name ? response . playlistInfo . name . trim ( ) : ( response . tracks [ 0 ] . info . title !== "" ? response . tracks [ 0 ] . info . title . trim ( ) : "(blank)" ) } \` has been added to the queue! ` ;
2019-09-13 20:02:41 +00:00
} else {
2022-06-14 05:38:01 +00:00
nextSong ( client , options , connection , response . tracks [ 0 ] . track , response . tracks [ 0 ] . info , music , voiceChannel , player ? player . host : options . member . id , player ? player . loop : false , player ? player . shuffle : false ) ;
2020-12-11 19:52:02 +00:00
return ;
2019-09-13 20:02:41 +00:00
}
2021-08-19 14:19:14 +00:00
}
2020-07-06 20:19:30 +00:00
2022-04-05 03:05:28 +00:00
export async function nextSong ( client , options , connection , track , info , music , voiceChannel , host , loop = false , shuffle = false , lastTrack = null ) {
2021-08-31 04:44:22 +00:00
skipVotes . delete ( voiceChannel . guild . id ) ;
2020-07-06 20:19:30 +00:00
const parts = Math . floor ( ( 0 / info . length ) * 10 ) ;
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
let playingMessage ;
2022-01-18 19:05:39 +00:00
if ( ! music && players . has ( voiceChannel . guild . id ) ) {
2021-08-19 14:19:14 +00:00
const playMessage = players . get ( voiceChannel . guild . id ) . playMessage ;
2021-08-17 04:04:11 +00:00
try {
2021-08-31 04:44:22 +00:00
players . delete ( voiceChannel . guild . id ) ;
2021-12-13 21:46:08 +00:00
await playMessage . delete ( ) ;
2021-08-17 04:04:11 +00:00
} catch {
// no-op
}
2021-07-06 13:25:12 +00:00
}
2022-01-18 19:05:39 +00:00
if ( music && lastTrack === track && players . has ( voiceChannel . guild . id ) ) {
2021-08-19 14:19:14 +00:00
playingMessage = players . get ( voiceChannel . guild . id ) . playMessage ;
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
} else {
2022-01-06 00:48:08 +00:00
try {
2022-04-05 03:05:28 +00:00
const content = ! music ? "🔊 Playing sound..." : {
2022-01-06 00:48:08 +00:00
embeds : [ {
color : 16711680 ,
author : {
name : "Now Playing" ,
icon _url : client . user . avatarURL
} ,
fields : [ {
name : "ℹ ️ Title:" ,
value : info . title && info . title . trim ( ) !== "" ? info . title : "(blank)"
} ,
{
name : "🎤 Artist:" ,
value : info . title && info . author . trim ( ) !== "" ? info . author : "(blank)"
} ,
{
name : "💬 Channel:" ,
value : voiceChannel . name
} ,
{
name : ` ${ "▬" . repeat ( parts ) } 🔘 ${ "▬" . repeat ( 10 - parts ) } ` ,
value : ` 0:00/ ${ info . isStream ? "∞" : format ( info . length ) } `
} ]
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
} ]
2022-04-05 03:05:28 +00:00
} ;
if ( options . type === "classic" ) {
playingMessage = await client . createMessage ( options . channel . id , content ) ;
} else {
await options . interaction [ options . interaction . acknowledged ? "editOriginalMessage" : "createMessage" ] ( content ) ;
playingMessage = await options . interaction . getOriginalMessage ( ) ;
}
2022-01-06 00:48:08 +00:00
} catch {
// no-op
}
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
}
2022-06-29 02:54:15 +00:00
connection . removeAllListeners ( "exception" ) ;
2021-09-13 22:24:15 +00:00
connection . removeAllListeners ( "end" ) ;
2022-06-14 05:38:01 +00:00
connection . playTrack ( { track } ) ;
2022-06-29 01:36:32 +00:00
players . set ( voiceChannel . guild . id , { player : connection , type : music ? "music" : "sound" , host : host , voiceChannel : voiceChannel , originalChannel : options . channel , loop , shuffle , playMessage : playingMessage } ) ;
2022-06-14 05:38:01 +00:00
connection . once ( "exception" , async ( exception ) => {
2021-09-20 23:32:59 +00:00
try {
2022-03-17 19:28:35 +00:00
if ( playingMessage . channel . messages . has ( playingMessage . id ) ) await playingMessage . delete ( ) ;
2021-09-20 23:32:59 +00:00
const playMessage = players . get ( voiceChannel . guild . id ) . playMessage ;
2022-03-17 19:28:35 +00:00
if ( playMessage . channel . messages . has ( playMessage . id ) ) await playMessage . delete ( ) ;
2021-09-20 23:32:59 +00:00
} catch {
// no-op
}
2022-01-08 21:54:34 +00:00
try {
2022-06-14 05:38:01 +00:00
connection . node . leaveChannel ( voiceChannel . guild . id ) ;
2022-01-08 21:54:34 +00:00
} catch {
// no-op
}
2021-09-13 22:24:15 +00:00
connection . removeAllListeners ( "end" ) ;
players . delete ( voiceChannel . guild . id ) ;
queues . delete ( voiceChannel . guild . id ) ;
2022-06-14 05:38:01 +00:00
logger . error ( exception . error ) ;
const content = ` 🔊 Looks like there was an error regarding sound playback: \n \` \` \` ${ exception . type } : ${ exception . error } \` \` \` ` ;
2022-06-11 20:23:41 +00:00
if ( options . type === "classic" ) {
await client . createMessage ( options . channel . id , content ) ;
} else {
await options . interaction . createMessage ( content ) ;
}
2021-09-13 22:24:15 +00:00
} ) ;
connection . on ( "end" , async ( data ) => {
if ( data . reason === "REPLACED" ) return ;
2021-09-20 17:26:40 +00:00
let queue = queues . get ( voiceChannel . guild . id ) ;
2021-09-13 22:24:15 +00:00
const player = players . get ( voiceChannel . guild . id ) ;
2021-12-12 05:44:49 +00:00
if ( player && process . env . STAYVC === "true" ) {
player . type = "idle" ;
players . set ( voiceChannel . guild . id , player ) ;
}
2021-09-13 22:24:15 +00:00
let newQueue ;
2021-09-20 17:26:40 +00:00
if ( player && player . shuffle ) {
if ( player . loop ) {
queue . push ( queue . shift ( ) ) ;
} else {
queue = queue . slice ( 1 ) ;
}
queue . unshift ( queue . splice ( Math . floor ( Math . random ( ) * queue . length ) , 1 ) [ 0 ] ) ;
newQueue = queue ;
} else if ( player && player . loop ) {
2021-09-13 22:24:15 +00:00
queue . push ( queue . shift ( ) ) ;
newQueue = queue ;
} else {
newQueue = queue ? queue . slice ( 1 ) : [ ] ;
}
queues . set ( voiceChannel . guild . id , newQueue ) ;
2021-12-12 05:44:49 +00:00
if ( newQueue . length !== 0 ) {
2022-06-14 05:38:01 +00:00
const newTrack = await connection . node . rest . decode ( newQueue [ 0 ] ) ;
2022-04-05 03:05:28 +00:00
nextSong ( client , options , connection , newQueue [ 0 ] , newTrack , music , voiceChannel , host , player . loop , player . shuffle , track ) ;
2021-12-12 05:44:49 +00:00
try {
2022-03-17 19:28:35 +00:00
if ( newQueue [ 0 ] !== track && playingMessage . channel . messages . has ( playingMessage . id ) ) await playingMessage . delete ( ) ;
if ( newQueue [ 0 ] !== track && player . playMessage . channel . messages . has ( player . playMessage . id ) ) await player . playMessage . delete ( ) ;
2021-12-12 05:44:49 +00:00
} catch {
// no-op
}
} else if ( process . env . STAYVC !== "true" ) {
2022-06-14 05:38:01 +00:00
connection . node . leaveChannel ( voiceChannel . guild . id ) ;
2021-08-19 14:19:14 +00:00
players . delete ( voiceChannel . guild . id ) ;
queues . delete ( voiceChannel . guild . id ) ;
2021-09-19 22:49:02 +00:00
skipVotes . delete ( voiceChannel . guild . id ) ;
2022-06-11 20:23:41 +00:00
const content = "🔊 The current voice channel session has ended." ;
if ( options . type === "classic" ) {
await client . createMessage ( options . channel . id , content ) ;
} else {
await options . interaction . createMessage ( content ) ;
}
2021-09-13 22:24:15 +00:00
try {
2022-03-17 19:28:35 +00:00
if ( playingMessage . channel . messages . has ( playingMessage . id ) ) await playingMessage . delete ( ) ;
if ( player && player . playMessage . channel . messages . has ( player . playMessage . id ) ) await player . playMessage . delete ( ) ;
2021-09-13 22:24:15 +00:00
} catch {
// no-op
2020-12-11 19:52:02 +00:00
}
2021-09-13 22:24:15 +00:00
} else {
try {
2022-03-17 19:28:35 +00:00
if ( playingMessage . channel . messages . has ( playingMessage . id ) ) await playingMessage . delete ( ) ;
if ( player && player . playMessage . channel . messages . has ( player . playMessage . id ) ) await player . playMessage . delete ( ) ;
2021-09-13 22:24:15 +00:00
} catch {
// no-op
2020-11-05 21:40:18 +00:00
}
2021-09-13 22:24:15 +00:00
}
} ) ;
2021-12-12 05:44:49 +00:00
}