mirror of
https://github.com/discordjs/discord.js.git
synced 2024-08-20 21:14:46 +12:00
Added more functionality
This commit is contained in:
parent
0a278a32f3
commit
856149860e
4 changed files with 110 additions and 1 deletions
1
hydrabot/.gitignore
vendored
1
hydrabot/.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
config.json
|
||||
authority.json
|
||||
|
|
37
hydrabot/authority.js
Normal file
37
hydrabot/authority.js
Normal file
|
@ -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);
|
||||
} );
|
||||
}
|
14
hydrabot/commands.js
Normal file
14
hydrabot/commands.js
Normal file
|
@ -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;
|
|
@ -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!" );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue