diff --git a/hydrabot/commands.js b/hydrabot/commands.js index 6552b5f4f..9f9418207 100644 --- a/hydrabot/commands.js +++ b/hydrabot/commands.js @@ -250,6 +250,20 @@ Commands[ "avatar" ] = { } } +Commands["setusername"] = { + oplevel : 2, + fn : function( bot, params, message ){ + + var name = getKey( params, "name", "Boris Johnson" ); + + bot.setUsername(name, function(err){ + if(err) + bot.reply(message, err); + }) + + } +} + Commands[ "icon" ] = { oplevel: 0, fn: function( bot, params, message ) { diff --git a/hydrabot/hydrabot.js b/hydrabot/hydrabot.js index 308964f15..cfc3ea386 100644 --- a/hydrabot/hydrabot.js +++ b/hydrabot/hydrabot.js @@ -31,9 +31,6 @@ hydrabot.on("debug", function(m){ console.time("hydrabotbenchmark"); hydrabot.login( BotConfig.email, BotConfig.password ); - -console.log( "Starting up..." ); - var time = Date.now(); // When the bot is ready to go, output to the console diff --git a/index.js b/index.js index 9b338c7cd..1d70d216d 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,9 @@ var WebSocket = require( 'ws' ); var Internal = require( "./lib/internal.js" ).Internal; var TokenManager = require( "./lib/TokenManager.js" ).TokenManager; -var serverCreateRequests = []. globalLoginTime = Date.now(); +var serverCreateRequests = [].globalLoginTime = Date.now(); -function tp(time){ +function tp( time ) { return Date.now() - time; } @@ -267,7 +267,7 @@ exports.Client.prototype.login = function( email, password, callback, noCache ) globalLoginTime = Date.now(); - this.debug("login called at " + globalLoginTime); + this.debug( "login called at " + globalLoginTime ); var self = this; callback = callback || function() {}; @@ -281,12 +281,12 @@ exports.Client.prototype.login = function( email, password, callback, noCache ) if ( this.tokenManager.exists( email ) && !noCache ) { var token = this.tokenManager.getToken( email, password ); - if(!token.match(/[^\w.-]+/g)){ + if ( !token.match( /[^\w.-]+/g ) ) { done( this.tokenManager.getToken( email, password ) ); - self.debug("loaded token from caches in "+tp(globalLoginTime)); + self.debug( "loaded token from caches in " + tp( globalLoginTime ) ); return; - }else{ - self.debug("error getting token from caches, using default auth"); + } else { + self.debug( "error getting token from caches, using default auth" ); } } @@ -298,19 +298,21 @@ exports.Client.prototype.login = function( email, password, callback, noCache ) error: err } ] ); self.websocket.close(); - self.debug("failed to login in "+tp(globalLoginTime)); + self.debug( "failed to login in " + tp( globalLoginTime ) ); } else { if ( !noCache ) { self.tokenManager.addToken( email, token, password ); } - self.debug("loaded token from auth servers in "+tp(globalLoginTime)); + self.debug( "loaded token from auth servers in " + tp( globalLoginTime ) ); done( token ); } } ); function done( token ) { - self.debug("using token " + token); + self.email = email; + self.password = password; + self.debug( "using token " + token ); self.token = token; self.websocket.sendData(); self.loggedIn = true; @@ -366,7 +368,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { case 0: if ( dat.t === "READY" ) { - self.debug("got ready packet"); + self.debug( "got ready packet" ); var data = dat.d; @@ -390,7 +392,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { if ( cached === toCache ) { self.ready = true; self.triggerEvent( "ready" ); - self.debug("ready triggered"); + self.debug( "ready triggered" ); } } ); } @@ -447,10 +449,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) { data.mention_everyone = data.mention_everyone || formerMessage.everyoneMentioned; data.embeds = data.embeds || formerMessage.embeds; - try{ + try { newMessage = new Message( data, channel ); - }catch(e){ - self.debug("dropped a message update packet"); + } catch ( e ) { + self.debug( "dropped a message update packet" ); return; } @@ -519,6 +521,13 @@ exports.Client.prototype.connectWebsocket = function( cb ) { } + } else if ( dat.t === "USER_UPDATE" ) { + + if ( dat.d.id === self.user.id ) { + self.user.username = dat.d.username; + self.user.avatar = dat.d.avatar; + } + } break; @@ -541,7 +550,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { } this.websocket.onopen = function() { - self.debug("websocket conn open"); + self.debug( "websocket conn open" ); this.sendData( "onopen" ); } @@ -562,8 +571,8 @@ exports.Client.prototype.connectWebsocket = function( cb ) { } } -exports.Client.prototype.debug = function(msg){ - this.triggerEvent("debug", ["[SL "+ tp(globalLoginTime) +"] " + msg]); +exports.Client.prototype.debug = function( msg ) { + this.triggerEvent( "debug", [ "[SL " + tp( globalLoginTime ) + "] " + msg ] ); } /** @@ -872,6 +881,18 @@ exports.Client.prototype.updateMessage = function( oldMessage, newContent, callb } +exports.Client.prototype.setUsername = function( username, callback ) { + + var self = this; + + Internal.XHR.setUsername( self.token, self.user.avatar, self.email, null, self.password, username, function( err ) { + + callback( err ); + + } ); + +} + exports.Client.prototype.getChannelLogs = function( channel, amount, callback ) { var self = this; @@ -970,7 +991,7 @@ exports.Client.prototype.getServer = function( id ) { exports.Client.prototype.getChannel = function( id ) { var normalChan = this.getChannels().filter( "id", id, true ); - return normalChan || this.PMList.filter("id", id, true); + return normalChan || this.PMList.filter( "id", id, true ); } exports.Client.prototype.getUser = function( id ) { diff --git a/lib/internal.js b/lib/internal.js index d892613ff..ce9581a7f 100644 --- a/lib/internal.js +++ b/lib/internal.js @@ -227,8 +227,6 @@ 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 ) @@ -242,4 +240,22 @@ Internal.XHR.acceptInvite = function(token, inviteID, callback){ } +Internal.XHR.setUsername = function(token, avatar, email, newPassword, password, username, callback){ + + request + .patch(Endpoints.API + "/users/@me") + .set( "authorization", token ) + .send({ + avatar : avatar, + email : email, + new_password : newPassword, + password : password, + username : username + }) + .end(function(err){ + callback(err); + }); + +} + exports.Internal = Internal; diff --git a/package.json b/package.json index 97e8e278b..7a384f55f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord.js", - "version": "2.6.8", + "version": "2.7.0", "description": "A way to interface with the Discord API", "main": "index.js", "scripts": {