@ -36,7 +36,7 @@ exports.GuildHandler = class {
logger . debug ( ` Connected to the database for ${ this . guild } ` ) ;
await this . createTables ( ) ;
// register commands
this . register Music Commands( ) ;
this . register Commands( ) ;
}
/ * *
@ -66,6 +66,11 @@ exports.GuildHandler = class {
name VARCHAR ( 32 ) UNIQUE NOT NULL ,
url VARCHAR ( 255 ) NOT NULL
) ` );
await this . db . run ( ` ${ utils . sql . tableExistCreate } commands (
$ { utils . sql . pkIdSerial } ,
name VARCHAR ( 32 ) UNIQUE NOT NULL ,
command VARCHAR ( 255 ) NOT NULL
) ` );
}
/ * *
@ -81,10 +86,7 @@ exports.GuildHandler = class {
let resolvedAnswer = await answer ;
await this . answerMessage ( msg , resolvedAnswer ) ;
} else if ( answer instanceof Array ) {
let answerPromises = [ ] ;
for ( let answerPart of answer )
answerPromises . push ( async ( ) => await this . answerMessage ( msg , answerPart ) ) ;
await waterfall ( answerPromises ) ;
await waterfall ( answer . map ( ( x ) => async ( ) => await this . answerMessage ( msg , x ) ) ) ;
} else if ( { } . toString . call ( answer ) === '[object Function]' ) { // check if the answer is of type function
await this . answerMessage ( msg , answer ( ) ) ;
} else {
@ -124,7 +126,7 @@ exports.GuildHandler = class {
/ * *
* registers all music commands and initializes a dj
* /
register Music Commands( ) {
register Commands( ) {
this . dj = new music . DJ ( ) ;
let playCb = async ( msg , kwargs , argv , template , next ) => {
@ -261,8 +263,8 @@ exports.GuildHandler = class {
}
} ) ;
// saves playlists
this . servant . createCommand ( servercmd . music . save , async ( msg , kwargs , argv ) => {
// saves playlists and videos
this . servant . createCommand ( servercmd . music . save media , async ( msg , kwargs , argv ) => {
let saveName = argv . join ( ' ' ) ;
let row = await this . db . get ( 'SELECT COUNT(*) count FROM playlists WHERE name = ?' , [ saveName ] ) ;
if ( ! row || row . count === 0 )
@ -272,19 +274,75 @@ exports.GuildHandler = class {
return ` Saved song/playlist as ${ saveName } ` ;
} ) ;
// saved command - prints out saved playlists
this . servant . createCommand ( servercmd . music . saved , async ( msg ) => {
// saved media command - prints out saved playlists and video s
this . servant . createCommand ( servercmd . music . saved media , async ( ) => {
let response = '' ;
let rows = await this . db . all ( 'SELECT name, url FROM playlists' ) ;
for ( let row of rows )
response += ` [ ${ row . name } ]( ${ row . url } ) \n ` ;
if ( rows . length === 0 )
msg . channel . send ( servercmd . music . saved . response . no _saved ) ;
return servercmd . music . saved media . response . no _saved ;
else
return new Discord . RichEmbed ( )
. setTitle ( 'Saved Songs and Playlists' )
. setDescription ( response ) ;
. setDescription ( response )
. setFooter ( ` Play a saved entry with ${ this . prefix } play [Entryname] ` )
. setTimestamp ( ) ;
} ) ;
this . servant . createCommand ( servercmd . music . deletemedia , async ( msg , kwargs , argv ) => {
let saveName = argv . join ( ' ' ) ;
if ( ! saveName ) {
return servercmd . music . deletemedia . response . no _name ;
} else {
await this . db . run ( 'DELETE FROM playlists WHERE name = ?' , [ saveName ] ) ;
return ` Deleted ${ saveName } from saved media ` ;
}
} ) ;
// savecmd - saves a command sequence with a name
this . servant . createCommand ( servercmd . utils . savecmd , async ( msg , kwargs , argv ) => {
let saveName = argv . pop ( ) ;
let cmdsequence = argv . join ( ' ' ) . replace ( /\\/g , '' ) ;
let row = await this . db . get ( 'SELECT COUNT(*) count FROM commands WHERE name = ?' , [ saveName ] ) ;
if ( ! row || row . count === 0 )
await this . db . run ( 'INSERT INTO commands (name, command) VALUES (?, ?)' , [ saveName , cmdsequence ] ) ;
else
await this . db . run ( 'UPDATE commands SET sequence = ? WHERE name = ?' , [ cmdsequence , saveName ] ) ;
return ` saved command sequence as ${ saveName } ` ;
} ) ;
// savedcmd - prints saved commands
this . servant . createCommand ( servercmd . utils . savedcmd , async ( ) => {
let response = new Discord . RichEmbed ( )
. setTitle ( 'Saved Commands' )
. setFooter ( ` Execute a saved entry with ${ this . prefix } execute [Entryname] ` )
. setTimestamp ( ) ;
let rows = await this . db . all ( 'SELECT name, command FROM commands' ) ;
if ( rows . length === 0 )
return servercmd . utils . savedcmd . response . no _commands ;
else
for ( let row of rows )
response . addField ( row . name , '`' + row . command + '`' ) ;
return response ;
} ) ;
// deletecmd - deletes a command from saved commands
this . servant . createCommand ( servercmd . utils . deletecmd , async ( msg , kwargs ) => {
await this . db . run ( 'DELETE FROM commands WHERE name = ?' , [ kwargs . cmdname ] ) ;
return ` Deleted command ${ kwargs . cmdname } ` ;
} ) ;
// execute - executes a saved command
this . servant . createCommand ( servercmd . utils . execute , async ( msg , kwargs ) => {
let row = await this . db . get ( 'SELECT command FROM commands WHERE name = ?' , [ kwargs . cmdname ] ) ;
if ( row ) {
msg . content = row . command ;
await this . handleMessage ( msg ) ;
} else {
return servercmd . utils . execute . response . not _found ;
}
} ) ;
}
} ;