From 35c38bf2bb511a784848a440c00c9f00106d7b1d Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Fri, 14 Aug 2015 18:42:53 +0100 Subject: [PATCH] Fixed error handling and added more functions to hydrabot --- hydrabot/commands.js | 98 +++++++++++++++++++++++++++++++++++++------- hydrabot/hydrabot.js | 12 ++++-- index.js | 40 +++++++++--------- 3 files changed, 112 insertions(+), 38 deletions(-) diff --git a/hydrabot/commands.js b/hydrabot/commands.js index 32bdd3efb..e0b684048 100644 --- a/hydrabot/commands.js +++ b/hydrabot/commands.js @@ -1,5 +1,6 @@ var Authority = require( "./authority.js" ); -var Discord = require( "./hydrabot.js" ).Discord; +var BotClass = require( "./hydrabot.js" ); +var Discord = BotClass.Discord; Commands = []; @@ -35,7 +36,7 @@ Commands[ "echo" ] = { } Commands[ "auth" ] = { - oplevel: 2, + oplevel: 0, fn: function( bot, params, message ) { var level = getKey( params, "level", "0" ); @@ -47,8 +48,10 @@ Commands[ "auth" ] = { bot.reply( message, "that authority level is too high for you to set!" ); } else if ( user.equals( message.author ) ) { bot.reply( message, "you can't alter your own authority level!" ); - } else if ( authLevel( user ) > authLevel( message.author ) ) { - bot.reply( message, "that user has a higher OP level than you!" ); + } else if ( authLevel( user ) >= authLevel( message.author ) ) { + bot.reply( message, "that user has a higher or equal OP level to you!" ); + } else if ( level < 0 ) { + bot.reply( message, "that level's a bit too low :P"); } else { setAuthLevel( user, level ); bot.reply( message, "I set the authority of " + user.mention() + " to **" + level + "**" ); @@ -61,9 +64,16 @@ Commands[ "auth" ] = { } Commands[ "clear" ] = { - oplevel: 1, + oplevel: 0, fn: function( bot, params, message ) { + if(!message.isPM()){ + if(authLevel(message.author) < 1){ + bot.reply(message, BotClass.AUTH_ERROR); + return; + } + } + var initMessage = false, cleared = false; @@ -126,18 +136,76 @@ Commands[ "clear" ] = { Commands[ "leave" ] = { oplevel: 3, - fn: function( bot, params, message) { + fn: function( bot, params, message ) { - if(message.isPM()){ - bot.reply(message, "Umm... I can't leave PMs... How awkward..."); - }else{ - bot.reply(message, "Ok ;( I'm leaving!"); - bot.leaveServer(message.channel.server, function(err){ - if(err){ - bot.reply(message, "There was an error leaving... how awkward."); - } - }); + var silent = hasFlag( params, "s" ) || hasFlag( params, "silent" ); + + if ( message.isPM() ) { + bot.reply( message, "Umm... I can't leave PMs... How awkward..." ); + } else { + + if ( !silent ) + bot.reply( message, "Ok ;( I'm leaving!" ); + + bot.leaveServer( message.channel.server, function( err ) { + if ( err ) { + bot.reply( message, "There was an error leaving... how awkward." ); + } + } ); + } + } +} + +Commands[ "avatar" ] = { + oplevel: 0, + fn: function( bot, params, message ) { + + var user = getUser( message, params ); + + if ( !user.avatar ) { + bot.sendMessage( message.channel, user.mention() + " does not have an avatar!" ); + } else { + bot.reply( message, user.getAvatarURL() ); + } + } +} + +Commands[ "icon" ] = { + oplevel: 0, + fn: function( bot, params, message ) { + + if ( message.isPM() ) { + bot.reply( message, "PMs don't have avatars!" ); + return; + } + + if ( !message.channel.server.icon ) { + bot.reply( message, "this server does not have an icon!" ); + return; + } + + bot.reply( message, message.channel.server.getIconURL() ); + + } +} + +Commands[ "remind" ] = { + oplevel: 0, + fn: function( bot, params, message ) { + + var time = parseInt(getKey(params, "t") || getKey(params, "time")) * 1000 || 21000; + var msg = getKey(params, "m") || getKey(params, "msg") || getKey(params, "message"); + + bot.reply( message, "I'll remind you to *"+msg+"* in *"+time/1000+"* seconds.", false, true, { + selfDestruct : time + }); + + setTimeout(send, time); + + function send(){ + bot.sendMessage( message.author, time + " seconds are up! **"+msg+"**." ); } + } } diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js index 7f06b879e..92da060e7 100644 --- a/hydrabot/hydrabot.js +++ b/hydrabot/hydrabot.js @@ -4,6 +4,8 @@ var Discord = require( "../" ); exports.Discord = Discord; +exports.LOL = "CHING CHONG UNTOUCHED"; + // Load the config file. If you have not already, make one that follows the // structure : { "email" : "discordEmail", "password" : "discordPassword" } var BotConfig = require( "./config.json" ); @@ -37,6 +39,7 @@ hydrabot.on( "ready", function() { hydrabot.on( "disconnected", function( obj ) { // Say we couldn't connect and then exit console.log( "Disconnected - " + obj.reason ); + console.log(obj.error); process.exit( 0 ); } ); @@ -69,20 +72,23 @@ hydrabot.on( "message", function( message ) { } ); function handleMessage( command, params, message ) { + if ( Commands[ command ] ) { - console.log( Authority.getLevel( message.author ) ); if ( Authority.getLevel( message.author ) >= Commands[ command ].oplevel ) { //user has authority to do this Commands[ command ].fn( hydrabot, params, message ); } else { //user doesn't have authority - hydrabot.reply( message, "you don't have authority to do this!" ); + hydrabot.reply( message, exports.AUTH_ERROR ); } } else { - hydrabot.reply( message, "that command was not found!" ); + hydrabot.reply( message, exports.NOT_FOUND ); } } + +exports.AUTH_ERROR = "you don't have authority to do this!"; +exports.NOT_FOUND = "that command was not found!"; diff --git a/index.js b/index.js index 53a2a45c5..c8238ed9c 100644 --- a/index.js +++ b/index.js @@ -18,8 +18,8 @@ exports.List = List; exports.Invite = Invite; exports.PMChannel = PMChannel; -exports.isUserID = function(id){ - return ((id + "").length === 17 && !isNaN(id)); +exports.isUserID = function( id ) { + return ( ( id + "" ).length === 17 && !isNaN( id ) ); } exports.Client = function( options ) { @@ -104,7 +104,7 @@ exports.Client.prototype.login = function( email, password ) { .post( Endpoints.LOGIN ) .send( details ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { client.triggerEvent( "disconnected", [ { reason: "failed to log in", error: err @@ -118,15 +118,15 @@ exports.Client.prototype.login = function( email, password ) { } -exports.Client.prototype.reply = function(){ +exports.Client.prototype.reply = function() { - if(arguments[1] instanceof Array){ - arguments[1] = arguments[1].join("\n"); + if ( arguments[ 1 ] instanceof Array ) { + arguments[ 1 ] = arguments[ 1 ].join( "\n" ); } - arguments[1] = arguments[0].author.mention() + ", " + arguments[1]; + arguments[ 1 ] = arguments[ 0 ].author.mention() + ", " + arguments[ 1 ]; - this.sendMessage.apply(this, arguments); + this.sendMessage.apply( this, arguments ); } @@ -317,7 +317,7 @@ exports.Client.prototype.createServer = function( _name, _region, cb ) { .set( "authorization", client.token ) .send( details ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { client.cacheServer( res.body.id, function( server ) { @@ -338,7 +338,7 @@ exports.Client.prototype.leaveServer = function( server, cb ) { .del( Endpoints.SERVERS + "/" + server.id ) .set( "authorization", client.token ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { client.serverList.removeElement( server ); @@ -367,7 +367,7 @@ exports.Client.prototype.createInvite = function( channel, options, cb ) { .set( "authorization", client.token ) .send( options ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { cb( false, new Invite( res.body ) ); @@ -387,7 +387,7 @@ exports.Client.prototype.startPM = function( user, message, cb, _mentions, optio recipient_id: user.id } ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { client.PMList.add( new PMChannel( res.body.recipient, res.body.id ) ); @@ -401,8 +401,8 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions options = options || {}; - if(message instanceof Array){ - message = message.join("\n"); + if ( message instanceof Array ) { + message = message.join( "\n" ); } var thisLoopId = Math.floor( Math.random() * 1000 ); @@ -496,9 +496,9 @@ exports.Client.prototype.channelFromId = function( id ) { var channelList = this.serverList.concatSublists( "channels", "id" ); var channel = channelList.filter( "id", id, true ); - if(!channel){ + if ( !channel ) { - channel = this.PMList.filter( "id", id, true); + channel = this.PMList.filter( "id", id, true ); } @@ -514,7 +514,7 @@ exports.Client.prototype.getChannelLogs = function( channel, amount, cb ) { .set( "authorization", client.token ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); return; } @@ -541,7 +541,7 @@ exports.Client.prototype.createChannel = function( server, serverName, serverTyp type: serverType } ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { var chann = new Channel( res.body, server ); @@ -559,7 +559,7 @@ exports.Client.prototype.deleteChannel = function( channel, cb ) { .del( Endpoints.CHANNELS + "/" + channel.id ) .set( "authorization", client.token ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else { @@ -581,7 +581,7 @@ exports.Client.prototype.deleteServer = function( server, cb ) { .del( Endpoints.SERVERS + "/" + server.id ) .set( "authorization", client.token ) .end( function( err, res ) { - if ( !res.ok ) { + if ( err ) { cb( err ); } else {