From 5685c5f4ce1a9c15ccde0c21f45d975c3ad5707c Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 15 Aug 2015 22:34:09 +0100 Subject: [PATCH] added updating of messages --- hydrabot/commands.js | 76 +++++++++++++++++++++++++++++++++++++++++++- hydrabot/hydrabot.js | 21 ++++++++++++ index.js | 70 ++++++++++++++++++++++++++++++++++++++-- lib/internal.js | 15 +++++++++ lib/list.js | 30 +++++++++++++++++ 5 files changed, 208 insertions(+), 4 deletions(-) diff --git a/hydrabot/commands.js b/hydrabot/commands.js index 3d694de18..48b4f93cf 100644 --- a/hydrabot/commands.js +++ b/hydrabot/commands.js @@ -27,7 +27,81 @@ Commands[ "info" ] = { } } -Commands[ "" ] +Commands[ "loading" ] = { + oplevel:0, + fn: function(bot, params, message){ + + var progress = 0; + var currentMessage; + var bars = 20; + + function getM(){ + var before = progress; + var after = bars - progress; + var ret = ""; + for(x=0; x < before; x++){ + ret += "-"; + } + ret += "**#**"; + for(y=0; y < after; y++){ + ret += "-"; + } + return ret; + } + + function doProg(){ + if(progress === (bars + 1)){ + progress = 0; + } + + if(currentMessage){ + bot.updateMessage(currentMessage, getM(), function(err, msg){ + if(!err) + currentMessage = msg; + }); + progress++; + } + + } + + bot.sendMessage(message.channel, getM(), function(err, message){ + currentMessage = message; + setInterval(doProg, 200); + }); + + } +} + +Commands[ "flashy" ] = { + oplevel:0, + fn: function(bot, params, message){ + + var phase = 0; + var msg; + + var textToSay = getKey(params, "m", "FLASH"); + var speed = parseInt( getKey(params, "s", "500") ); + + function change(){ + if(msg){ + + var highlighting = ((phase % 2) === 0 ? "**" : ""); + phase++; + bot.updateMessage(msg, highlighting + textToSay + highlighting, function(err, message){ + if(!err){ + msg = message; + } + }); + } + } + + bot.sendMessage(message.channel, textToSay, function(err, message){ + msg = message; + setInterval(change, speed); + }); + + } +} Commands[ "echo" ] = { oplevel: 0, diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js index 9ca7bd106..fa8f5a2a5 100644 --- a/hydrabot/hydrabot.js +++ b/hydrabot/hydrabot.js @@ -44,6 +44,27 @@ hydrabot.on( "disconnected", function( obj ) { process.exit( 0 ); } ); +hydrabot.on("messageDelete", function(message){ + console.log(message); +}) + +hydrabot.on("messageUpdate", function(former, edit){ + + if(former.author.equals(this.user)){ + return; + } + + if(former){ + + var seconds = Math.round((Date.now() - former.time) / 1000); + + var channel = former.channel; + hydrabot.sendMessage(channel, "**"+former.author.username + "** (edit from message "+seconds+" seconds ago):\n " + former.content); + + } + +}) + hydrabot.on( "message", function( message ) { // if the message doesn't begin with a valid command prefix exit diff --git a/index.js b/index.js index 16f3390c2..58e490a0b 100644 --- a/index.js +++ b/index.js @@ -222,8 +222,47 @@ exports.Client.prototype.connectWebsocket = function( cb ) { var channel = self.getChannel( data.channel_id ); var message = new Message( data, channel ); - self.triggerEvent( "message", [ message ] ); + if ( channel.messages ) + channel.messages.add( message ); + + } else if ( dat.t === "MESSAGE_DELETE" ) { + var data = dat.d; + + var channel = self.getChannel( data.channel_id ); + + if ( !channel.messages ) + return; + + var _msg = channel.messages.filter( "id", data.id, true ); + + if ( _msg ) { + self.triggerEvent( "messageDelete", [ _msg ] ); + channel.messages.removeElement( _msg ); + } + + } else if ( dat.t === "MESSAGE_UPDATE" ) { + + var data = dat.d; + if ( !self.ready ) + return; + + var formerMessage = false; + + var channel = self.getChannel( data.channel_id ); + + if ( channel ) { + + formerMessage = channel.messages.filter( "id", data.id, true ); + newMessage = new Message( data, channel ); + self.triggerEvent( "messageUpdate", [ formerMessage, newMessage ] ); + + if ( formerMessage ) + channel.messages.updateElement( formerMessage, newMessage ); + else + channel.messages.add( newMessage ); + + } } else if ( dat.t === "PRESENCE_UPDATE" ) { @@ -527,13 +566,38 @@ exports.Client.prototype.deleteMessage = function( message, callback ) { Internal.XHR.deleteMessage( self.token, message.channel.id, message.id, callback ); } +exports.Client.prototype.updateMessage = function(oldMessage, newContent, callback, options){ + + var self = this; + var channel = oldMessage.channel; + options = options || {}; + + Internal.XHR.updateMessage(self.token, channel.id, oldMessage.id, { + content : newContent, + mentions : [] + }, function(err, data){ + if(err){ + callback(err); + return; + } + var msg = new Message( data, self.getChannel( data.channel_id ) ); + if ( options.selfDestruct ) { + setTimeout( function() { + self.deleteMessage( msg ); + }, options.selfDestruct ); + } + callback( null, msg ); + }); + +} + exports.Client.prototype.getChannelLogs = function( channel, amount, callback ) { var self = this; Internal.XHR.getChannelLogs( self.token, channel.id, ( amount || 50 ), function( err, data ) { - if(err){ - callback(err); + if ( err ) { + callback( err ); return; } diff --git a/lib/internal.js b/lib/internal.js index 09d83acca..7e1552415 100644 --- a/lib/internal.js +++ b/lib/internal.js @@ -133,6 +133,21 @@ Internal.XHR.deleteMessage = function( token, channelID, messageID, callback ) { } ); } +Internal.XHR.updateMessage = function( token, channelID, messageID, messageParameters, callback ){ + + request + .patch(Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID) + .set( "authorization", token ) + .send( messageParameters ) + .end(function(err, res){ + if ( err ) { + callback( err ); + } else { + callback( null, res.body ); + } + }); +} + Internal.XHR.getChannelLogs = function( token, channelID, amount, callback ) { request .get( Endpoints.CHANNELS + "/" + channelID + "/messages?limit=" + amount ) diff --git a/lib/list.js b/lib/list.js index cc403ab6e..5de3d955d 100644 --- a/lib/list.js +++ b/lib/list.js @@ -34,6 +34,22 @@ exports.List.prototype.length = function() { return this.contents.length; } +exports.List.prototype.getIndex = function( object ){ + + var index = false; + + for(elementIndex in this.contents){ + var element = this.contents[elementIndex]; + if( element[this.discriminator] == object[this.discriminator] ){ + return elementIndex; + } + + } + + return index; + +} + exports.List.prototype.removeIndex = function( index ) { this.contents.splice( index, 1 ); } @@ -50,6 +66,20 @@ exports.List.prototype.removeElement = function( child ) { return false; } +exports.List.prototype.updateElement = function( child, newChild ){ + + for ( _element in this.contents ) { + var element = this.contents[_element]; + if ( child[ this.discriminator ] == element[ this.discriminator ] ) { + this.contents[_element] = newChild; + return true; + } + } + + return false; + +} + exports.List.prototype.concatSublists = function( whereList, discriminator ) { //this is meant to look at the contents, and assuming the contents are all lists, concatenate their values.