From 4c414452aca87f747994bcd4370c4ef1346be36a Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Fri, 14 Aug 2015 12:55:55 +0100 Subject: [PATCH 1/4] created hydrabot --- hydrabot/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 hydrabot/README.md diff --git a/hydrabot/README.md b/hydrabot/README.md new file mode 100644 index 000000000..5d0f750aa --- /dev/null +++ b/hydrabot/README.md @@ -0,0 +1,2 @@ +# hydrabot +Hydrabot is an open-source bot made with the intents of demonstrating the capabilities of [discord.js](https://github.com/hydrabolt/discord.js/). From 9549fea940595e680e69c35cf00a1bd6c8c90078 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Fri, 14 Aug 2015 13:24:42 +0100 Subject: [PATCH 2/4] basic setup --- hydrabot/.gitignore | 1 + hydrabot/README.md | 9 +++++++++ hydrabot/hydrabot.js | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 hydrabot/.gitignore create mode 100644 hydrabot/hydrabot.js diff --git a/hydrabot/.gitignore b/hydrabot/.gitignore new file mode 100644 index 000000000..d344ba6b0 --- /dev/null +++ b/hydrabot/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/hydrabot/README.md b/hydrabot/README.md index 5d0f750aa..dfaab46a1 100644 --- a/hydrabot/README.md +++ b/hydrabot/README.md @@ -1,2 +1,11 @@ # hydrabot Hydrabot is an open-source bot made with the intents of demonstrating the capabilities of [discord.js](https://github.com/hydrabolt/discord.js/). + +### Set up +The easiest setup would be to clone the discord.js repo, and then open a terminal/cmd in this directory and run `node hydrabot.js`. + +If you don't want to clone the repo but instead just use this folder, you need to edit `hydrabot.js` to use `require("discord.js")` as opposed to `require("../")`. Cloned directories will always be using the latest **discord.js**. + +### Setting up credentials + +Edit `config.json` to use your Discord email and password, and then run `node hydrabot.js`. diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js new file mode 100644 index 000000000..4c0991f3c --- /dev/null +++ b/hydrabot/hydrabot.js @@ -0,0 +1,26 @@ +// If you did not clone discord.js, change the require parameter to `discord.js` +// and then run `npm install --save discord.js` in the same directory as this +// file. The bot should then run. +var Discord = require( "../" ); + +// Load the config file. If you have not already, make one that follows the +// structure : { "email" : "discordEmail", "password" : "discordPassword" } +var BotConfig = require( "./config.json" ); + +// Create a new Discord Client +var hydrabot = new Discord.Client(); + +// Log the client in using the auth details in config.json +hydrabot.login( BotConfig.email, BotConfig.password+"a" ); + +console.log("Starting up..."); + +// When the bot is ready to go, output to the console +hydrabot.on( "ready", function() { + console.log( "Ready!" ); +} ); + +// When the bot gets disconnected, exit. +hydrabot.on( "disconnected", function( obj ) { + console.log( "Disconnected", obj.reason ); +} ); From dfe1a85c6ed4809989f4c337d0035694f677aaa7 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Fri, 14 Aug 2015 13:32:34 +0100 Subject: [PATCH 3/4] small fix required to change --- hydrabot/hydrabot.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js index 4c0991f3c..2695d3b42 100644 --- a/hydrabot/hydrabot.js +++ b/hydrabot/hydrabot.js @@ -11,9 +11,9 @@ var BotConfig = require( "./config.json" ); var hydrabot = new Discord.Client(); // Log the client in using the auth details in config.json -hydrabot.login( BotConfig.email, BotConfig.password+"a" ); +hydrabot.login( BotConfig.email, BotConfig.password ); -console.log("Starting up..."); +console.log( "Starting up..." ); // When the bot is ready to go, output to the console hydrabot.on( "ready", function() { @@ -22,5 +22,13 @@ hydrabot.on( "ready", function() { // When the bot gets disconnected, exit. hydrabot.on( "disconnected", function( obj ) { - console.log( "Disconnected", obj.reason ); + // Say we couldn't connect and then exit + console.log( "Disconnected - " + obj.reason ); + process.exit( 0 ); +} ); + +hydrabot.on( "message", function( message ) { + + console.log( message ); + } ); From 856149860eac8077e31c3be35157d7f3b1b2c1ff Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Fri, 14 Aug 2015 16:02:40 +0100 Subject: [PATCH 4/4] Added more functionality --- hydrabot/.gitignore | 1 + hydrabot/authority.js | 37 +++++++++++++++++++++++++++ hydrabot/commands.js | 14 ++++++++++ hydrabot/hydrabot.js | 59 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 hydrabot/authority.js create mode 100644 hydrabot/commands.js diff --git a/hydrabot/.gitignore b/hydrabot/.gitignore index d344ba6b0..070bc70aa 100644 --- a/hydrabot/.gitignore +++ b/hydrabot/.gitignore @@ -1 +1,2 @@ config.json +authority.json diff --git a/hydrabot/authority.js b/hydrabot/authority.js new file mode 100644 index 000000000..1f7c1c4af --- /dev/null +++ b/hydrabot/authority.js @@ -0,0 +1,37 @@ +var fs = require( "fs" ); + +var authCache = {}; + +exports.init = function() { + try { + var fd = fs.openSync( "./authority.json", "wx" ); + exports.writeCache(); + } catch ( e ) { + if ( e.errno !== -4075 ){ + throw e; + }else{ + authCache = JSON.parse(fs.readFileSync("./authority.json", "utf8")); + } + } +} + +exports.getLevel = function(user){ + + if(authCache[user.id]) + return authCache[user.id]; + else + return 0; + +} + +exports.setLevel = function(user, level){ + authCache[user.id] = level; + exports.writeCache(); +} + +exports.writeCache = function() { + fs.writeFile( './authority.json', JSON.stringify(authCache), function( err ) { + if ( err ) + console.log("Error saving Authority Caches - " + err.code); + } ); +} diff --git a/hydrabot/commands.js b/hydrabot/commands.js new file mode 100644 index 000000000..a26f25aae --- /dev/null +++ b/hydrabot/commands.js @@ -0,0 +1,14 @@ +var Authority = require("./authority.js"); + +Commands = []; + +Commands["info"] = { + oplevel : 0, + fn : function(bot, params, message){ + + bot.reply(message, "Info!"); + + } +} + +exports.Commands = Commands; diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js index 2695d3b42..594140c7f 100644 --- a/hydrabot/hydrabot.js +++ b/hydrabot/hydrabot.js @@ -7,9 +7,21 @@ var Discord = require( "../" ); // structure : { "email" : "discordEmail", "password" : "discordPassword" } var BotConfig = require( "./config.json" ); +// Load the commands file +var Commands = require( "./commands.js" ).Commands; + +// Load the Authority handler +var Authority = require( "./authority.js" ); + +// Initialise it +Authority.init(); + // Create a new Discord Client var hydrabot = new Discord.Client(); +// An array of single character prefixes the bot will respond to +var commandPrefixes = [ "$", "£", "`" ]; + // Log the client in using the auth details in config.json hydrabot.login( BotConfig.email, BotConfig.password ); @@ -29,6 +41,51 @@ hydrabot.on( "disconnected", function( obj ) { hydrabot.on( "message", function( message ) { - console.log( message ); + // if the message doesn't begin with a valid command prefix exit + if ( commandPrefixes.indexOf( message.content.charAt( 0 ) ) == -1 ) + return; + + var command = "", + params = []; //set the message details + + // remove the prefix from the start of the message + message.content = message.content.substr( 1 ); + + // split the message by slashes. This will yield something + // like: ["command", "a", "b", "c"]. + var chunks = message.content.split( "/" ); + + for ( key in chunks ) { //loop through the chunks and trim them + chunks[ key ] = chunks[ key ].trim(); + } + + command = chunks[ 0 ]; //the first param will be the command + params = chunks.slice( 1 ); + + // it's less messy if we outsource to another function + handleMessage( command, params, message ); } ); + +function handleMessage( command, params, message ) { + var channel = message.channel; // set the channel variable to message.channel + var sender = message.author; // set the sender variable to the author of the message + var isPM = ( message.channel instanceof Discord.PMChannel ); // set isPM to true if the channel is a Private Message Channel. + + 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 + hydrabolt.reply( message, "you don't have authority to do this!" ); + } + + } else { + hydrabot.reply( message, "that command was not found!" ); + } + +}