mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 18:13:29 +01:00
Added server creation, server leaving/deletion and invite creation.
This commit is contained in:
73
index.js
73
index.js
@@ -5,6 +5,7 @@ var Message = require( "./lib/message.js" ).Message;
|
|||||||
var User = require( "./lib/user.js" ).User;
|
var User = require( "./lib/user.js" ).User;
|
||||||
var Channel = require( "./lib/channel.js" ).Channel;
|
var Channel = require( "./lib/channel.js" ).Channel;
|
||||||
var List = require( "./lib/list.js" ).List;
|
var List = require( "./lib/list.js" ).List;
|
||||||
|
var Invite = require( "./lib/invite.js" ).Invite;
|
||||||
var WebSocket = require( 'ws' );
|
var WebSocket = require( 'ws' );
|
||||||
|
|
||||||
exports.Client = function( options ) {
|
exports.Client = function( options ) {
|
||||||
@@ -85,10 +86,10 @@ exports.Client.prototype.login = function( email, password ) {
|
|||||||
.send( details )
|
.send( details )
|
||||||
.end( function( err, res ) {
|
.end( function( err, res ) {
|
||||||
if ( !res.ok ) {
|
if ( !res.ok ) {
|
||||||
client.triggerEvent( "disconnected", [{
|
client.triggerEvent( "disconnected", [ {
|
||||||
reason: "failed to log in",
|
reason: "failed to log in",
|
||||||
error: err
|
error: err
|
||||||
}] );
|
} ] );
|
||||||
} else {
|
} else {
|
||||||
client.token = res.body.token;
|
client.token = res.body.token;
|
||||||
client.loggedIn = true;
|
client.loggedIn = true;
|
||||||
@@ -104,10 +105,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
|
|||||||
|
|
||||||
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
|
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
|
||||||
this.websocket.onclose = function( e ) {
|
this.websocket.onclose = function( e ) {
|
||||||
client.triggerEvent( "disconnected", [{
|
client.triggerEvent( "disconnected", [ {
|
||||||
reason: "websocket disconnected",
|
reason: "websocket disconnected",
|
||||||
error: e
|
error: e
|
||||||
}] );
|
} ] );
|
||||||
};
|
};
|
||||||
this.websocket.onmessage = function( e ) {
|
this.websocket.onmessage = function( e ) {
|
||||||
|
|
||||||
@@ -140,7 +141,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
|
|||||||
}, _server.members );
|
}, _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" ) {
|
} else if ( dat.t === "MESSAGE_CREATE" ) {
|
||||||
var data = dat.d;
|
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 client = this;
|
||||||
|
|
||||||
|
var details = {
|
||||||
|
name: _name,
|
||||||
|
region: _region
|
||||||
|
};
|
||||||
|
|
||||||
request
|
request
|
||||||
.post( Endpoints.SERVERS )
|
.post( Endpoints.SERVERS )
|
||||||
.set( "authorization", client.token )
|
.set( "authorization", client.token )
|
||||||
@@ -222,12 +228,61 @@ exports.Client.prototype.createServer = function( details, cb ) {
|
|||||||
if ( !res.ok ) {
|
if ( !res.ok ) {
|
||||||
cb( err );
|
cb( err );
|
||||||
} else {
|
} 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 ) {
|
exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions, options ) {
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
@@ -266,8 +321,8 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions
|
|||||||
.send( details )
|
.send( details )
|
||||||
.end( function( err, res ) {
|
.end( function( err, res ) {
|
||||||
|
|
||||||
if(err){
|
if ( err ) {
|
||||||
cb(err);
|
cb( err );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
lib/invite.js
Normal file
21
lib/invite.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user