Fixed error handling and added more functions to hydrabot

This commit is contained in:
hydrabolt
2015-08-14 18:42:53 +01:00
parent 5289e61007
commit 35c38bf2bb
3 changed files with 112 additions and 38 deletions

View File

@@ -1,5 +1,6 @@
var Authority = require( "./authority.js" ); var Authority = require( "./authority.js" );
var Discord = require( "./hydrabot.js" ).Discord; var BotClass = require( "./hydrabot.js" );
var Discord = BotClass.Discord;
Commands = []; Commands = [];
@@ -35,7 +36,7 @@ Commands[ "echo" ] = {
} }
Commands[ "auth" ] = { Commands[ "auth" ] = {
oplevel: 2, oplevel: 0,
fn: function( bot, params, message ) { fn: function( bot, params, message ) {
var level = getKey( params, "level", "0" ); 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!" ); bot.reply( message, "that authority level is too high for you to set!" );
} else if ( user.equals( message.author ) ) { } else if ( user.equals( message.author ) ) {
bot.reply( message, "you can't alter your own authority level!" ); bot.reply( message, "you can't alter your own authority level!" );
} else if ( authLevel( user ) > authLevel( message.author ) ) { } else if ( authLevel( user ) >= authLevel( message.author ) ) {
bot.reply( message, "that user has a higher OP level than you!" ); 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 { } else {
setAuthLevel( user, level ); setAuthLevel( user, level );
bot.reply( message, "I set the authority of " + user.mention() + " to **" + level + "**" ); bot.reply( message, "I set the authority of " + user.mention() + " to **" + level + "**" );
@@ -61,9 +64,16 @@ Commands[ "auth" ] = {
} }
Commands[ "clear" ] = { Commands[ "clear" ] = {
oplevel: 1, oplevel: 0,
fn: function( bot, params, message ) { fn: function( bot, params, message ) {
if(!message.isPM()){
if(authLevel(message.author) < 1){
bot.reply(message, BotClass.AUTH_ERROR);
return;
}
}
var initMessage = false, var initMessage = false,
cleared = false; cleared = false;
@@ -126,18 +136,76 @@ Commands[ "clear" ] = {
Commands[ "leave" ] = { Commands[ "leave" ] = {
oplevel: 3, oplevel: 3,
fn: function( bot, params, message) { fn: function( bot, params, message ) {
if(message.isPM()){ var silent = hasFlag( params, "s" ) || hasFlag( params, "silent" );
bot.reply(message, "Umm... I can't leave PMs... How awkward...");
}else{ if ( message.isPM() ) {
bot.reply(message, "Ok ;( I'm leaving!"); bot.reply( message, "Umm... I can't leave PMs... How awkward..." );
bot.leaveServer(message.channel.server, function(err){ } else {
if(err){
bot.reply(message, "There was an error leaving... how awkward."); 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+"**." );
} }
} }
} }

View File

@@ -4,6 +4,8 @@
var Discord = require( "../" ); var Discord = require( "../" );
exports.Discord = Discord; exports.Discord = Discord;
exports.LOL = "CHING CHONG UNTOUCHED";
// Load the config file. If you have not already, make one that follows the // Load the config file. If you have not already, make one that follows the
// structure : { "email" : "discordEmail", "password" : "discordPassword" } // structure : { "email" : "discordEmail", "password" : "discordPassword" }
var BotConfig = require( "./config.json" ); var BotConfig = require( "./config.json" );
@@ -37,6 +39,7 @@ hydrabot.on( "ready", function() {
hydrabot.on( "disconnected", function( obj ) { hydrabot.on( "disconnected", function( obj ) {
// Say we couldn't connect and then exit // Say we couldn't connect and then exit
console.log( "Disconnected - " + obj.reason ); console.log( "Disconnected - " + obj.reason );
console.log(obj.error);
process.exit( 0 ); process.exit( 0 );
} ); } );
@@ -69,20 +72,23 @@ hydrabot.on( "message", function( message ) {
} ); } );
function handleMessage( command, params, message ) { function handleMessage( command, params, message ) {
if ( Commands[ command ] ) { if ( Commands[ command ] ) {
console.log( Authority.getLevel( message.author ) );
if ( Authority.getLevel( message.author ) >= Commands[ command ].oplevel ) { if ( Authority.getLevel( message.author ) >= Commands[ command ].oplevel ) {
//user has authority to do this //user has authority to do this
Commands[ command ].fn( hydrabot, params, message ); Commands[ command ].fn( hydrabot, params, message );
} else { } else {
//user doesn't have authority //user doesn't have authority
hydrabot.reply( message, "you don't have authority to do this!" ); hydrabot.reply( message, exports.AUTH_ERROR );
} }
} else { } 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!";

View File

@@ -18,8 +18,8 @@ exports.List = List;
exports.Invite = Invite; exports.Invite = Invite;
exports.PMChannel = PMChannel; exports.PMChannel = PMChannel;
exports.isUserID = function(id){ exports.isUserID = function( id ) {
return ((id + "").length === 17 && !isNaN(id)); return ( ( id + "" ).length === 17 && !isNaN( id ) );
} }
exports.Client = function( options ) { exports.Client = function( options ) {
@@ -104,7 +104,7 @@ exports.Client.prototype.login = function( email, password ) {
.post( Endpoints.LOGIN ) .post( Endpoints.LOGIN )
.send( details ) .send( details )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
client.triggerEvent( "disconnected", [ { client.triggerEvent( "disconnected", [ {
reason: "failed to log in", reason: "failed to log in",
error: err 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){ if ( arguments[ 1 ] instanceof Array ) {
arguments[1] = arguments[1].join("\n"); 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 ) .set( "authorization", client.token )
.send( details ) .send( details )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
client.cacheServer( res.body.id, function( server ) { client.cacheServer( res.body.id, function( server ) {
@@ -338,7 +338,7 @@ exports.Client.prototype.leaveServer = function( server, cb ) {
.del( Endpoints.SERVERS + "/" + server.id ) .del( Endpoints.SERVERS + "/" + server.id )
.set( "authorization", client.token ) .set( "authorization", client.token )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
client.serverList.removeElement( server ); client.serverList.removeElement( server );
@@ -367,7 +367,7 @@ exports.Client.prototype.createInvite = function( channel, options, cb ) {
.set( "authorization", client.token ) .set( "authorization", client.token )
.send( options ) .send( options )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
cb( false, new Invite( res.body ) ); cb( false, new Invite( res.body ) );
@@ -387,7 +387,7 @@ exports.Client.prototype.startPM = function( user, message, cb, _mentions, optio
recipient_id: user.id recipient_id: user.id
} ) } )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
client.PMList.add( new PMChannel( res.body.recipient, res.body.id ) ); 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 || {}; options = options || {};
if(message instanceof Array){ if ( message instanceof Array ) {
message = message.join("\n"); message = message.join( "\n" );
} }
var thisLoopId = Math.floor( Math.random() * 1000 ); 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 channelList = this.serverList.concatSublists( "channels", "id" );
var channel = channelList.filter( "id", id, true ); 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 ) .set( "authorization", client.token )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
return; return;
} }
@@ -541,7 +541,7 @@ exports.Client.prototype.createChannel = function( server, serverName, serverTyp
type: serverType type: serverType
} ) } )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
var chann = new Channel( res.body, server ); var chann = new Channel( res.body, server );
@@ -559,7 +559,7 @@ exports.Client.prototype.deleteChannel = function( channel, cb ) {
.del( Endpoints.CHANNELS + "/" + channel.id ) .del( Endpoints.CHANNELS + "/" + channel.id )
.set( "authorization", client.token ) .set( "authorization", client.token )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {
@@ -581,7 +581,7 @@ exports.Client.prototype.deleteServer = function( server, cb ) {
.del( Endpoints.SERVERS + "/" + server.id ) .del( Endpoints.SERVERS + "/" + server.id )
.set( "authorization", client.token ) .set( "authorization", client.token )
.end( function( err, res ) { .end( function( err, res ) {
if ( !res.ok ) { if ( err ) {
cb( err ); cb( err );
} else { } else {