diff --git a/index.js b/index.js index 77e8e1d14..605f91167 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ var Message = require( "./lib/message.js" ).Message; var User = require( "./lib/user.js" ).User; var Channel = require( "./lib/channel.js" ).Channel; var List = require( "./lib/list.js" ).List; +var Invite = require( "./lib/invite.js" ).Invite; var WebSocket = require( 'ws' ); exports.Client = function( options ) { @@ -85,10 +86,10 @@ exports.Client.prototype.login = function( email, password ) { .send( details ) .end( function( err, res ) { if ( !res.ok ) { - client.triggerEvent( "disconnected", [{ + client.triggerEvent( "disconnected", [ { reason: "failed to log in", error: err - }] ); + } ] ); } else { client.token = res.body.token; client.loggedIn = true; @@ -104,10 +105,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) { this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB ); this.websocket.onclose = function( e ) { - client.triggerEvent( "disconnected", [{ + client.triggerEvent( "disconnected", [ { reason: "websocket disconnected", error: e - }] ); + } ] ); }; this.websocket.onmessage = function( e ) { @@ -140,7 +141,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { }, _server.members ); } - client.user = new User( data.user.username, data.user.id, data.user.discriminator, data.user.avatar ); + client.user = new User( data.user ); } else if ( dat.t === "MESSAGE_CREATE" ) { var data = dat.d; @@ -210,10 +211,15 @@ exports.Client.prototype.logout = function() { } -exports.Client.prototype.createServer = function( details, cb ) { +exports.Client.prototype.createServer = function( _name, _region, cb ) { var client = this; + var details = { + name: _name, + region: _region + }; + request .post( Endpoints.SERVERS ) .set( "authorization", client.token ) @@ -222,12 +228,61 @@ exports.Client.prototype.createServer = function( details, cb ) { if ( !res.ok ) { cb( err ); } else { - cb( new Server( res.body ) ); + client.cacheServer( res.body.id, function( server ) { + + cb( null, server ); + + } ); } } ); } +exports.Client.prototype.leaveServer = function( server, cb ){ + + var client = this; + + request + .del( Endpoints.SERVERS + "/" + server.id) + .set( "authorization", client.token ) + .end( function(err, res){ + if ( !res.ok ) { + cb( err ); + } else { + cb( null ); + } + } ); + +} + +exports.Client.prototype.createInvite = function( channel, options, cb ) { + + var client = this; + var options = options || {}; + + if ( channel instanceof Server ) { + channel = channel.getDefaultChannel(); + } + + options.max_age = options.max_age || 0; + options.max_uses = options.max_uses || 0; + options.temporary = options.temporary || false; + options.xkcdpass = options.xkcd || false; + + request + .post( Endpoints.CHANNELS + "/" + channel.id + "/invites" ) + .set( "authorization", client.token ) + .send( options ) + .end( function( err, res ) { + if ( !res.ok ) { + cb( err ); + } else { + cb( false, new Invite(res.body) ); + } + } ) + +} + exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions, options ) { options = options || {}; @@ -266,8 +321,8 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions .send( details ) .end( function( err, res ) { - if(err){ - cb(err); + if ( err ) { + cb( err ); return; } diff --git a/lib/invite.js b/lib/invite.js new file mode 100644 index 000000000..f2e10077a --- /dev/null +++ b/lib/invite.js @@ -0,0 +1,21 @@ +var User = require("./user.js").User; + +exports.Invite = function(json){ + + this.max_age = json.max_age; + this.code = json.code; + this.server = json.guild; + this.revoked = json.revoked; + this.created_at = Date.parse(json.created_at); + this.temporary = json.temporary; + this.uses = json.uses; + this.max_uses = json.uses; + this.inviter = new User(json.inviter); + this.xkcdpass = json.xkcdpass; + this.channel = json.channel; +} + +exports.Invite.prototype.generateInviteURL = function(xkcd){ + var code = (xkcd ? this.xkcdpass : this.code); + return "https://discord.gg/"+code; +}