Added joining servers and better message handling

This commit is contained in:
hydrabolt
2015-08-17 21:02:18 +01:00
parent 945fe8f65e
commit 93361925ab
7 changed files with 110 additions and 44 deletions

View File

@@ -19,7 +19,7 @@ Commands[ "info" ] = {
"User ID is *" + user.id + "*", "User ID is *" + user.id + "*",
"Authority/OP Level to me is **" + Authority.getLevel( user ) + "**" "Authority/OP Level to me is **" + Authority.getLevel( user ) + "**"
], function( err ) { ], function( err ) {
if(err) if ( err )
console.log( err ); console.log( err );
} ); } );
@@ -27,77 +27,77 @@ Commands[ "info" ] = {
} }
Commands[ "loading" ] = { Commands[ "loading" ] = {
oplevel:0, oplevel: 0,
fn: function(bot, params, message){ fn: function( bot, params, message ) {
var progress = 0; var progress = 0;
var currentMessage; var currentMessage;
var bars = 20; var bars = 20;
function getM(){ function getM() {
var before = progress; var before = progress;
var after = bars - progress; var after = bars - progress;
var ret = ""; var ret = "";
for(x=0; x < before; x++){ for ( x = 0; x < before; x++ ) {
ret += "-"; ret += "-";
} }
ret += "**#**"; ret += "**#**";
for(y=0; y < after; y++){ for ( y = 0; y < after; y++ ) {
ret += "-"; ret += "-";
} }
return ret; return ret;
} }
function doProg(){ function doProg() {
if(progress === (bars + 1)){ if ( progress === ( bars + 1 ) ) {
progress = 0; progress = 0;
} }
if(currentMessage){ if ( currentMessage ) {
bot.updateMessage(currentMessage, getM(), function(err, msg){ bot.updateMessage( currentMessage, getM(), function( err, msg ) {
if(!err) if ( !err )
currentMessage = msg; currentMessage = msg;
}); } );
progress++; progress++;
} }
} }
bot.sendMessage(message.channel, getM(), function(err, message){ bot.sendMessage( message.channel, getM(), function( err, message ) {
currentMessage = message; currentMessage = message;
setInterval(doProg, 200); setInterval( doProg, 200 );
}); } );
} }
} }
Commands[ "flashy" ] = { Commands[ "flashy" ] = {
oplevel:0, oplevel: 0,
fn: function(bot, params, message){ fn: function( bot, params, message ) {
var phase = 0; var phase = 0;
var msg; var msg;
var textToSay = getKey(params, "m", "FLASH"); var textToSay = getKey( params, "m", "FLASH" );
var speed = parseInt( getKey(params, "s", "500") ); var speed = parseInt( getKey( params, "s", "500" ) );
function change(){ function change() {
if(msg){ if ( msg ) {
var highlighting = ((phase % 2) === 0 ? "**" : ""); var highlighting = ( ( phase % 2 ) === 0 ? "**" : "" );
phase++; phase++;
bot.updateMessage(msg, highlighting + textToSay + highlighting, function(err, message){ bot.updateMessage( msg, highlighting + textToSay + highlighting, function( err, message ) {
if(!err){ if ( !err ) {
msg = message; msg = message;
} }
}); } );
} }
} }
bot.sendMessage(message.channel, textToSay, function(err, message){ bot.sendMessage( message.channel, textToSay, function( err, message ) {
msg = message; msg = message;
setInterval(change, speed); setInterval( change, speed );
}); } );
} }
} }
@@ -278,7 +278,7 @@ Commands[ "feedback" ] = {
bot.getChannelLogs( message.channel, amount, function( err, logs ) { bot.getChannelLogs( message.channel, amount, function( err, logs ) {
console.log(logs); console.log( logs );
if ( err ) { if ( err ) {
bot.reply( message, "an error occurred when grabbing the logs.", false, { bot.reply( message, "an error occurred when grabbing the logs.", false, {
@@ -289,8 +289,8 @@ Commands[ "feedback" ] = {
var found = []; var found = [];
for ( msg of logs.contents ) { for ( msg of logs.contents ) {
if ( ~msg.content.indexOf( "[request" ) || ~msg.content.indexOf( "[feature" || ~msg.content.indexOf( "[suggestion") ) ) { if ( ~msg.content.indexOf( "[request" ) || ~msg.content.indexOf( "[feature" || ~msg.content.indexOf( "[suggestion" ) ) ) {
if(msg.content.length > 10){ if ( msg.content.length > 10 ) {
found.push( msg ); found.push( msg );
} }
} }
@@ -299,13 +299,13 @@ Commands[ "feedback" ] = {
bot.sendMessage( message.author, "Ok, here's a rundown of all feature requests so far:", function( err, ms ) { bot.sendMessage( message.author, "Ok, here's a rundown of all feature requests so far:", function( err, ms ) {
if (!err) if ( !err )
gothroughit(); gothroughit();
} ); } );
bot.reply( message, "I found " + found.length + " result(s) that matched this. I'll send it to you in a PM.", false, { bot.reply( message, "I found " + found.length + " result(s) that matched this. I'll send it to you in a PM.", false, {
selfDestruct : 3000 selfDestruct: 3000
} ); } );
function gothroughit() { function gothroughit() {
@@ -322,6 +322,23 @@ Commands[ "feedback" ] = {
} }
Commands[ "acceptinvite" ] = {
oplevel: 0,
fn: function( bot, params, message ) {
var inv = getKey(params, "i");
bot.joinServer(inv, function(err, server){
if(err){
bot.reply(message, "I couldn't join that server :(");
}else{
bot.reply(message, "I joined **"+ server.name +"**, a server with "+server.channels.length()+" channels and "+server.members.length()+" members.");
}
});
}
}
Commands[ "remind" ] = { Commands[ "remind" ] = {
oplevel: 0, oplevel: 0,
fn: function( bot, params, message ) { fn: function( bot, params, message ) {
@@ -380,12 +397,12 @@ Commands[ "activity" ] = {
var report = "here's a list of activity over the last " + count + " messages :\n\n"; var report = "here's a list of activity over the last " + count + " messages :\n\n";
var usernames = {}; var usernames = {};
for( id in activity ){ for ( id in activity ) {
usernames[id] = bot.getUser(id).username; usernames[ id ] = bot.getUser( id ).username;
} }
for ( id in activity ) { for ( id in activity ) {
report += usernames[id] + " | " + activity[ id ] + " | **" + Math.round( ( activity[ id ] / count ) * 100 ) + "%**.\n"; report += usernames[ id ] + " | " + activity[ id ] + " | **" + Math.round( ( activity[ id ] / count ) * 100 ) + "%**.\n";
} }
bot.reply( message, report, false, false ); bot.reply( message, report, false, false );

View File

@@ -24,6 +24,7 @@ var hydrabot = new Discord.Client();
var commandPrefixes = [ "$", "£", "`" ]; var commandPrefixes = [ "$", "£", "`" ];
// Log the client in using the auth details in config.json // Log the client in using the auth details in config.json
console.time("hydrabotbenchmark");
hydrabot.login( BotConfig.email, BotConfig.password ); hydrabot.login( BotConfig.email, BotConfig.password );
console.log( "Starting up..." ); console.log( "Starting up..." );
@@ -32,7 +33,7 @@ var time = Date.now();
// When the bot is ready to go, output to the console // When the bot is ready to go, output to the console
hydrabot.on( "ready", function() { hydrabot.on( "ready", function() {
console.log( "Ready in "+ (Date.now() - time) +"ms!" ); console.timeEnd("hydrabotbenchmark");
} ); } );
// When the bot gets disconnected, exit. // When the bot gets disconnected, exit.
@@ -48,12 +49,12 @@ hydrabot.on("messageDelete", function(message){
hydrabot.on("messageUpdate", function(former, edit){ hydrabot.on("messageUpdate", function(former, edit){
if(former.author.equals(this.user)){
return;
}
if(former){ if(former){
if(former.author.equals(this.user) || former.content === edit.content){
return;
}
var seconds = Math.round((Date.now() - former.time) / 1000); var seconds = Math.round((Date.now() - former.time) / 1000);
var channel = former.channel; var channel = former.channel;

View File

@@ -10,6 +10,8 @@ var PMChannel = require( "./lib/PMChannel.js" ).PMChannel;
var WebSocket = require( 'ws' ); var WebSocket = require( 'ws' );
var Internal = require( "./lib/internal.js" ).Internal; var Internal = require( "./lib/internal.js" ).Internal;
var serverCreateRequests = [];
/** /**
* The wrapper module for the Discord Client, also provides some helpful objects. * The wrapper module for the Discord Client, also provides some helpful objects.
* *
@@ -34,6 +36,7 @@ exports.PMChannel = PMChannel;
* @param {Object} [options] An object containing configurable options. * @param {Object} [options] An object containing configurable options.
* @param {Number} [options.maxmessage=5000] The maximum amount of messages to be stored per channel. * @param {Number} [options.maxmessage=5000] The maximum amount of messages to be stored per channel.
*/ */
exports.Client = function( options ) { exports.Client = function( options ) {
/** /**
@@ -405,6 +408,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
data.id = data.id || formerMessage.id; data.id = data.id || formerMessage.id;
data.mentions = data.mentions || formerMessage.mentions; data.mentions = data.mentions || formerMessage.mentions;
data.mention_everyone = data.mention_everyone || formerMessage.everyoneMentioned; data.mention_everyone = data.mention_everyone || formerMessage.everyoneMentioned;
data.embeds = data.embeds || formerMessage.embeds;
newMessage = new Message( data, channel ); newMessage = new Message( data, channel );
@@ -447,7 +451,12 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
if ( !self.serverList.filter( "id", dat.d.id, true ) ) { if ( !self.serverList.filter( "id", dat.d.id, true ) ) {
self.cacheServer( dat.d, function( server ) { self.cacheServer( dat.d, function( server ) {
self.triggerEvent( "serverJoin", [ server ] ); if ( serverCreateRequests[ server.id ] ) {
serverCreateRequests[ server.id ]( null, server );
serverCreateRequests[ server.id ] = null;
} else {
self.triggerEvent( "serverJoin", [ server ] );
}
} ); } );
} }
@@ -878,6 +887,24 @@ exports.Client.prototype.deleteServer = function( server, callback ) {
} }
exports.Client.prototype.joinServer = function( invite, callback ) {
var self = this;
var code = ( invite instanceof Invite ? invite.code : invite );
Internal.XHR.acceptInvite( self.token, code, function( err, inviteData ) {
if ( err ) {
callback( err );
} else {
serverCreateRequests[inviteData.guild.id] = callback;
}
} );
}
exports.Client.prototype.getServers = function() { exports.Client.prototype.getServers = function() {
return this.serverList; return this.serverList;
} }

View File

@@ -16,7 +16,7 @@ exports.Channel = function(name, server, type, id, isPrivate){
this.type = type; this.type = type;
this.id = id; this.id = id;
this.isPrivate = isPrivate; this.isPrivate = isPrivate;
this.messages = new List("id", "5000"); this.messages = new List("id", 5000);
} }
exports.Channel.equals = function(otherChannel){ exports.Channel.equals = function(otherChannel){

View File

@@ -1,6 +1,8 @@
var base = "https://discordapp.com/"; var base = "https://discordapp.com/";
var apibase = base + "api"; var apibase = base + "api";
exports.API = apibase;
exports.WEBSOCKET_HUB = "wss://discordapp.com/hub" exports.WEBSOCKET_HUB = "wss://discordapp.com/hub"
exports.USERS = apibase + "/users"; exports.USERS = apibase + "/users";

View File

@@ -225,4 +225,21 @@ Internal.XHR.getServer = function( token, serverID, callback ) {
} }
Internal.XHR.acceptInvite = function(token, inviteID, callback){
console.log(Endpoints.API + "/invite/" + inviteID);
request
.post( Endpoints.API + "/invite/" + inviteID )
.set( "authorization", token )
.end(function(err, res){
if(err){
callback(err);
}else{
callback(null, res.body)
}
});
}
exports.Internal = Internal; exports.Internal = Internal;

View File

@@ -2,7 +2,7 @@ var User = require( "./user.js" ).User;
var List = require( "./list.js" ).List; var List = require( "./list.js" ).List;
var PMChannel = require( "./PMChannel.js" ).PMChannel; var PMChannel = require( "./PMChannel.js" ).PMChannel;
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned ) { exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned, embeds ) {
if ( !content ) { if ( !content ) {
message = time; message = time;
@@ -13,6 +13,7 @@ exports.Message = function( time, author, content, channel, id, mentions, everyo
id = message.id; id = message.id;
mentions = message.mentions; mentions = message.mentions;
everyoneMentioned = message.mention_everyone; everyoneMentioned = message.mention_everyone;
embeds = message.embeds;
} }
this.time = Date.parse( time ); this.time = Date.parse( time );
@@ -22,6 +23,7 @@ exports.Message = function( time, author, content, channel, id, mentions, everyo
this.id = id; this.id = id;
this.mentions = new List( "id" ); this.mentions = new List( "id" );
this.everyoneMentioned = everyoneMentioned; this.everyoneMentioned = everyoneMentioned;
this.embeds = embeds;
for ( x in mentions ) { for ( x in mentions ) {
var _mention = mentions[ x ]; var _mention = mentions[ x ];
this.mentions.add( new User( _mention ) ); this.mentions.add( new User( _mention ) );