2020-12-19 00:50:25 +00:00
const collections = require ( "../collections.js" ) ;
const logger = require ( "../logger.js" ) ;
const misc = require ( "../misc.js" ) ;
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 . warn ( "\x1b[1m\x1b[31m\x1b[40m" + "The MongoDB database driver has been deprecated and will be removed in a future release. Please migrate your database to PostgreSQL as soon as possible." + "\x1b[0m" ) ;
2020-12-19 00:50:25 +00:00
const mongoose = require ( "mongoose" ) ;
mongoose . connect ( process . env . DB , {
poolSize : 10 ,
bufferMaxEntries : 0 ,
useNewUrlParser : true ,
2021-03-16 20:45:10 +00:00
useUnifiedTopology : true
2020-12-19 00:50:25 +00:00
} ) ;
const guildSchema = new mongoose . Schema ( {
id : String ,
tags : Map ,
prefix : String ,
disabled : [ String ] ,
tagsDisabled : Boolean
} ) ;
const Guild = mongoose . model ( "Guild" , guildSchema ) ;
const globalSchema = new mongoose . Schema ( {
cmdCounts : Map ,
} ) ;
const Global = mongoose . model ( "Global" , globalSchema ) ;
2020-12-19 00:57:41 +00:00
const connection = mongoose . connection ;
2020-12-19 00:50:25 +00:00
exports . getGuild = async ( query ) => {
return await Guild . findOne ( { id : query } ) ;
} ;
exports . setPrefix = async ( prefix , guild ) => {
const guildDB = await this . getGuild ( guild . id ) ;
guildDB . prefix = prefix ;
await guildDB . save ( ) ;
collections . prefixCache . set ( guild . id , prefix ) ;
} ;
exports . setTag = async ( name , content , guild ) => {
const guildDB = await this . getGuild ( guild . id ) ;
guildDB . tags [ name ] = content ;
await guildDB . save ( ) ;
} ;
exports . removeTag = async ( name , guild ) => {
const guildDB = await this . getGuild ( guild . id ) ;
delete guildDB . tags [ name ] ;
await guildDB . save ( ) ;
} ;
exports . toggleTags = async ( guild ) => {
const guildDB = await this . getGuild ( guild . id ) ;
guildDB . tagsDisabled = ! guildDB . tagsDisabled ;
await guildDB . save ( ) ;
return guildDB . tagsDisabled ;
} ;
exports . disableChannel = async ( channel ) => {
const guildDB = await this . getGuild ( channel . guild . id ) ;
guildDB . disabled . push ( channel . id ) ;
await guildDB . save ( ) ;
collections . disabledCache . set ( channel . guild . id , guildDB . disabled ) ;
} ;
exports . enableChannel = async ( channel ) => {
const guildDB = await this . getGuild ( channel . guild . id ) ;
guildDB . disabled = guildDB . disabled . filter ( item => item !== channel . id ) ;
await guildDB . save ( ) ;
collections . disabledCache . set ( channel . guild . id , guildDB . disabled ) ;
} ;
exports . getCounts = async ( ) => {
return [ ... ( await Global . findOne ( { } ) ) . cmdCounts . entries ( ) ] ;
} ;
exports . addCount = async ( command ) => {
const global = await Global . findOne ( { } ) ;
const count = global . cmdCounts . get ( command ) ;
global . cmdCounts . set ( command , parseInt ( count ) + 1 ) ;
await global . save ( ) ;
} ;
exports . addGuild = async ( guild ) => {
const guildDB = new Guild ( {
id : guild . id ,
tags : misc . tagDefaults ,
prefix : process . env . PREFIX ,
disabled : [ ] ,
tagsDisabled : false
} ) ;
await guildDB . save ( ) ;
return guildDB ;
} ;
exports . fixGuild = async ( guild ) => {
const guildDB = await Guild . findOne ( { id : guild . id } ) ;
if ( ! guildDB ) {
logger . log ( ` Registering guild database entry for guild ${ guild . id } ... ` ) ;
return await this . addGuild ( guild ) ;
} else {
if ( ! guildDB . disabled && guildDB . disabledChannels ) {
guildDB . set ( "disabled" , guildDB . disabledChannels ) ;
guildDB . set ( "disabledChannels" , undefined ) ;
await guildDB . save ( ) ;
return guildDB ;
}
}
} ;
exports . setup = async ( ) => {
const global = await Global . findOne ( { } ) ;
if ( ! global ) {
const countObject = { } ;
for ( const command of collections . commands . keys ( ) ) {
countObject [ command ] = 0 ;
}
const newGlobal = new Global ( {
cmdCounts : countObject
} ) ;
await newGlobal . save ( ) ;
} else {
const exists = [ ] ;
for ( const command of collections . commands . keys ( ) ) {
if ( ! global . cmdCounts . has ( command ) ) {
global . cmdCounts . set ( command , 0 ) ;
}
exists . push ( command ) ;
}
for ( const command of global . cmdCounts . keys ( ) ) {
if ( ! exists . includes ( command ) ) {
global . cmdCounts . set ( command , undefined ) ;
}
}
await global . save ( ) ;
}
2020-12-19 00:57:41 +00:00
} ;
exports . stop = async ( ) => {
await connection . disconnect ( ) ;
2020-12-19 00:50:25 +00:00
} ;