Added guild.kick(member) and member.kick()

This commit is contained in:
Amish Shah
2016-07-02 17:50:44 +01:00
parent fb49ad7d93
commit bbf0b0683a
11 changed files with 465 additions and 391 deletions

74
.gitignore vendored
View File

@@ -1,38 +1,38 @@
# Created by https://www.gitignore.io # Created by https://www.gitignore.io
.tmp/ .tmp/
.vscode/ .vscode/
### Node ### ### Node ###
# Logs # Logs
logs logs
*.log *.log
test/auth.json test/auth.json
# Runtime data # Runtime data
pids pids
*.pid *.pid
*.seed *.seed
# Directory for instrumented libs generated by jscoverage/JSCover # Directory for instrumented libs generated by jscoverage/JSCover
lib-cov lib-cov
# Coverage directory used by tools like istanbul # Coverage directory used by tools like istanbul
coverage coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt .grunt
# node-waf configuration # node-waf configuration
.lock-wscript .lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html) # Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release build/Release
# Dependency directory # Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules node_modules
test/auth.json test/auth.json
examples/auth.json examples/auth.json
docs/_build docs/_build

View File

@@ -1,26 +1,27 @@
'use strict'; 'use strict';
const requireAction = name => require(`./${name}`); const requireAction = name => require(`./${name}`);
class ActionsManager { class ActionsManager {
constructor(client) { constructor(client) {
this.client = client; this.client = client;
this.register('MessageCreate'); this.register('MessageCreate');
this.register('MessageDelete'); this.register('MessageDelete');
this.register('MessageUpdate'); this.register('MessageUpdate');
this.register('ChannelCreate'); this.register('ChannelCreate');
this.register('ChannelDelete'); this.register('ChannelDelete');
this.register('ChannelUpdate'); this.register('ChannelUpdate');
this.register('GuildDelete'); this.register('GuildDelete');
this.register('GuildUpdate'); this.register('GuildUpdate');
this.register('UserUpdate'); this.register('GuildMemberRemove');
} this.register('UserUpdate');
}
register(name) {
let Action = requireAction(name); register(name) {
this[name] = new Action(this.client); let Action = requireAction(name);
} this[name] = new Action(this.client);
} }
}
module.exports = ActionsManager;
module.exports = ActionsManager;

View File

@@ -1,44 +1,44 @@
'use strict'; 'use strict';
const Action = require('./Action'); const Action = require('./Action');
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
const Message = require('../../structures/Message'); const Message = require('../../structures/Message');
class ChannelDeleteAction extends Action { class ChannelDeleteAction extends Action {
constructor(client) { constructor(client) {
super(client); super(client);
this.timeouts = []; this.timeouts = [];
this.deleted = {}; this.deleted = {};
} }
handle(data) { handle(data) {
let client = this.client; let client = this.client;
let channel = client.store.get('channels', data.id); let channel = client.store.get('channels', data.id);
if (channel) { if (channel) {
client.store.KillChannel(channel); client.store.KillChannel(channel);
this.deleted[channel.id] = channel; this.deleted[channel.id] = channel;
this.scheduleForDeletion(channel.id); this.scheduleForDeletion(channel.id);
} else if (this.deleted[data.id]) { } else if (this.deleted[data.id]) {
channel = this.deleted[data.id]; channel = this.deleted[data.id];
} }
return { return {
channel, channel,
}; };
} }
scheduleForDeletion(id) { scheduleForDeletion(id) {
this.timeouts.push( this.timeouts.push(
setTimeout(() => delete this.deleted[id], setTimeout(() => delete this.deleted[id],
this.client.options.rest_ws_bridge_timeout) this.client.options.rest_ws_bridge_timeout)
); );
} }
}; };
module.exports = ChannelDeleteAction; module.exports = ChannelDeleteAction;

View File

@@ -0,0 +1,50 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class GuildMemberRemoveAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
handle(data) {
let client = this.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
let member = guild.store.get('members', data.user.id);
if (member) {
guild._removeMember(member);
this.deleted[guild.id + data.user.id] = member;
this.scheduleForDeletion(guild.id, data.user.id);
}
if (!member) {
member = this.deleted[guild.id + data.user.id];
}
return {
g: guild,
m: member,
};
}
return {
g: guild,
m: null,
};
}
scheduleForDeletion(guildID, userID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + userID],
this.client.options.rest_ws_bridge_timeout)
);
}
};
module.exports = GuildMemberRemoveAction;

View File

@@ -1,247 +1,260 @@
'use strict'; 'use strict';
const Constants = require('../../util/Constants'); const Constants = require('../../util/Constants');
const Structure = name => require('../../structures/' + name); const Structure = name => require('../../structures/' + name);
const User = Structure('User'); const User = Structure('User');
const GuildMember = Structure('GuildMember'); const GuildMember = Structure('GuildMember');
const Message = Structure('Message'); const Message = Structure('Message');
class RESTMethods{ class RESTMethods{
constructor(restManager) { constructor(restManager) {
this.rest = restManager; this.rest = restManager;
} }
LoginEmailPassword(email, password) { LoginEmailPassword(email, password) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.client.store.email = email; this.rest.client.store.email = email;
this.rest.client.store.password = password; this.rest.client.store.password = password;
this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password }) this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password })
.then(data => { .then(data => {
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject); this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
}) })
.catch(reject); .catch(reject);
}); });
} }
LoginToken(token) { LoginToken(token) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.client.manager.connectToWebSocket(token, resolve, reject); this.rest.client.manager.connectToWebSocket(token, resolve, reject);
}); });
} }
GetGateway() { GetGateway() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.GATEWAY, true) this.rest.makeRequest('get', Constants.Endpoints.GATEWAY, true)
.then(res => resolve(res.url)) .then(res => resolve(res.url))
.catch(reject); .catch(reject);
}); });
} }
SendMessage(channel, content, tts, nonce) { SendMessage(channel, content, tts, nonce) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var _this = this; var _this = this;
if (channel instanceof User || channel instanceof GuildMember) { if (channel instanceof User || channel instanceof GuildMember) {
this.CreateDM(channel).then(chan => { this.CreateDM(channel).then(chan => {
channel = chan; channel = chan;
req(); req();
}) })
.catch(reject); .catch(reject);
} else { } else {
req(); req();
} }
function req() { function req() {
_this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, { _this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content, tts, nonce, content, tts, nonce,
}) })
.then(data => resolve(_this.rest.client.actions.MessageCreate.handle(data).m)) .then(data => resolve(_this.rest.client.actions.MessageCreate.handle(data).m))
.catch(reject); .catch(reject);
} }
}); });
} }
DeleteMessage(message) { DeleteMessage(message) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true) this.rest.makeRequest('del', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true)
.then(data => { .then(data => {
resolve(this.rest.client.actions.MessageDelete.handle({ resolve(this.rest.client.actions.MessageDelete.handle({
id: message.id, id: message.id,
channel_id: message.channel.id, channel_id: message.channel.id,
}).m); }).m);
}) })
.catch(reject); .catch(reject);
}); });
} }
UpdateMessage(message, content) { UpdateMessage(message, content) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true, { this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true, {
content, content,
}) })
.then(data => { .then(data => {
resolve(this.rest.client.actions.MessageUpdate.handle(data).updated); resolve(this.rest.client.actions.MessageUpdate.handle(data).updated);
}) })
.catch(reject); .catch(reject);
}); });
} }
CreateChannel(guild, channelName, channelType) { CreateChannel(guild, channelName, channelType) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.GUILD_CHANNELS(guild.id), true, { this.rest.makeRequest('post', Constants.Endpoints.GUILD_CHANNELS(guild.id), true, {
name: channelName, name: channelName,
type: channelType, type: channelType,
}) })
.then(data => { .then(data => {
resolve(this.rest.client.actions.ChannelCreate.handle(data).channel); resolve(this.rest.client.actions.ChannelCreate.handle(data).channel);
}) })
.catch(reject); .catch(reject);
}); });
} }
GetExistingDM(recipient) { GetExistingDM(recipient) {
let dmChannel = this.rest.client.store.getAsArray('channels') let dmChannel = this.rest.client.store.getAsArray('channels')
.filter(channel => channel.recipient) .filter(channel => channel.recipient)
.filter(channel => channel.recipient.id === recipient.id); .filter(channel => channel.recipient.id === recipient.id);
return dmChannel[0]; return dmChannel[0];
} }
CreateDM(recipient) { CreateDM(recipient) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let dmChannel = this.GetExistingDM(recipient); let dmChannel = this.GetExistingDM(recipient);
if (dmChannel) { if (dmChannel) {
return resolve(dmChannel); return resolve(dmChannel);
} }
this.rest.makeRequest('post', Constants.Endpoints.USER_CHANNELS(this.rest.client.store.user.id), true, { this.rest.makeRequest('post', Constants.Endpoints.USER_CHANNELS(this.rest.client.store.user.id), true, {
recipient_id: recipient.id, recipient_id: recipient.id,
}) })
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel)) .then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
.catch(reject); .catch(reject);
}); });
} }
DeleteChannel(channel) { DeleteChannel(channel) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (channel instanceof User || channel instanceof GuildMember) { if (channel instanceof User || channel instanceof GuildMember) {
channel = this.GetExistingDM(channel); channel = this.GetExistingDM(channel);
} }
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true) this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true)
.then(data => { .then(data => {
data.id = channel.id; data.id = channel.id;
resolve(this.rest.client.actions.ChannelDelete.handle(data).channel); resolve(this.rest.client.actions.ChannelDelete.handle(data).channel);
}) })
.catch(reject); .catch(reject);
}); });
} }
UpdateChannel(channel, data) { UpdateChannel(channel, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
data.name = (data.name || channel.name).trim(); data.name = (data.name || channel.name).trim();
data.topic = data.topic || channel.topic; data.topic = data.topic || channel.topic;
data.position = data.position || channel.position; data.position = data.position || channel.position;
data.bitrate = data.bitrate || channel.bitrate; data.bitrate = data.bitrate || channel.bitrate;
this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL(channel.id), true, data) this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL(channel.id), true, data)
.then(data => { .then(data => {
resolve(this.rest.client.actions.ChannelUpdate.handle(data).updated); resolve(this.rest.client.actions.ChannelUpdate.handle(data).updated);
}) })
.catch(reject); .catch(reject);
}); });
} }
LeaveGuild(guild) { LeaveGuild(guild) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.ME_GUILD(guild.id), true) this.rest.makeRequest('del', Constants.Endpoints.ME_GUILD(guild.id), true)
.then(() => { .then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild); resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
}) })
.catch(reject); .catch(reject);
}); });
} }
// untested but probably will work // untested but probably will work
DeleteGuild(guild) { DeleteGuild(guild) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.GUILD(guild.id), true) this.rest.makeRequest('del', Constants.Endpoints.GUILD(guild.id), true)
.then(() => { .then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild); resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
}) })
.catch(reject); .catch(reject);
}); });
} }
UpdateCurrentUser(_data) { UpdateCurrentUser(_data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let user = this.rest.client.store.user; let user = this.rest.client.store.user;
let data = {}; let data = {};
data.username = _data.username || user.username; data.username = _data.username || user.username;
data.avatar = this.rest.client.resolver.ResolveBase64(_data.avatar) || user.avatar; data.avatar = this.rest.client.resolver.ResolveBase64(_data.avatar) || user.avatar;
if (!user.bot) { if (!user.bot) {
data.password = this.rest.client.store.password; data.password = this.rest.client.store.password;
data.email = _data.email || this.rest.client.store.email; data.email = _data.email || this.rest.client.store.email;
data.new_password = _data.newPassword; data.new_password = _data.newPassword;
} }
this.rest.makeRequest('patch', Constants.Endpoints.ME, true, data) this.rest.makeRequest('patch', Constants.Endpoints.ME, true, data)
.then(data => resolve(this.rest.client.actions.UserUpdate.handle(data).updated)) .then(data => resolve(this.rest.client.actions.UserUpdate.handle(data).updated))
.catch(reject); .catch(reject);
}); });
} }
UpdateGuild(guild, _data) { UpdateGuild(guild, _data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
/* /*
can contain: can contain:
name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash
*/ */
let data = {}; let data = {};
if (_data.name) { if (_data.name) {
data.name = _data.name; data.name = _data.name;
} }
if (_data.region) { if (_data.region) {
data.region = _data.region; data.region = _data.region;
} }
if (_data.verificationLevel) { if (_data.verificationLevel) {
data.verification_level = Number(_data.verificationLevel); data.verification_level = Number(_data.verificationLevel);
} }
if (_data.afkChannel) { if (_data.afkChannel) {
data.afk_channel_id = this.rest.client.resolver.ResolveChannel(_data.afkChannel).id; data.afk_channel_id = this.rest.client.resolver.ResolveChannel(_data.afkChannel).id;
} }
if (_data.afkTimeout) { if (_data.afkTimeout) {
data.afk_timeout = Number(_data.afkTimeout); data.afk_timeout = Number(_data.afkTimeout);
} }
if (_data.icon) { if (_data.icon) {
data.icon = this.rest.client.resolver.ResolveBase64(_data.icon); data.icon = this.rest.client.resolver.ResolveBase64(_data.icon);
} }
if (_data.owner) { if (_data.owner) {
data.owner_id = this.rest.client.resolver.ResolveUser(_data.owner).id; data.owner_id = this.rest.client.resolver.ResolveUser(_data.owner).id;
} }
if (_data.splash) { if (_data.splash) {
data.splash = this.rest.client.resolver.ResolveBase64(_data.splash); data.splash = this.rest.client.resolver.ResolveBase64(_data.splash);
} }
this.rest.makeRequest('patch', Constants.Endpoints.GUILD(guild.id), true, data) this.rest.makeRequest('patch', Constants.Endpoints.GUILD(guild.id), true, data)
.then(data => resolve(this.rest.client.actions.GuildUpdate.handle(data).updated)) .then(data => resolve(this.rest.client.actions.GuildUpdate.handle(data).updated))
.catch(reject); .catch(reject);
}); });
} }
}
KickGuildMember(guild, member) {
module.exports = RESTMethods; return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.GUILD_MEMBER(guild.id, member.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildMemberRemove.handle({
guild_id : guild.id,
user : member.user,
}).m);
})
.catch(reject);
});
}
}
module.exports = RESTMethods;

View File

@@ -1,28 +1,28 @@
'use strict'; 'use strict';
const AbstractHandler = require('./AbstractHandler'); const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`); const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser'); const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild'); const Guild = Structure('Guild');
const ServerChannel = Structure('ServerChannel'); const ServerChannel = Structure('ServerChannel');
const Constants = require('../../../../util/Constants'); const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject'); const CloneObject = require('../../../../util/CloneObject');
class ChannelUpdateHandler extends AbstractHandler { class ChannelUpdateHandler extends AbstractHandler {
constructor(packetManager) { constructor(packetManager) {
super(packetManager); super(packetManager);
} }
handle(packet) { handle(packet) {
let data = packet.d; let data = packet.d;
let client = this.packetManager.client; let client = this.packetManager.client;
client.actions.ChannelUpdate.handle(data); client.actions.ChannelUpdate.handle(data);
} }
}; };
module.exports = ChannelUpdateHandler; module.exports = ChannelUpdateHandler;

View File

@@ -20,13 +20,10 @@ class GuildMemberRemoveHandler extends AbstractHandler {
let data = packet.d; let data = packet.d;
let client = this.packetManager.client; let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id); let response = client.actions.GuildMemberRemove.handle(data);
if (guild) { if (response.m) {
let member = guild.store.get('members', data.user.id); client.emit(Constants.Events.GUILD_MEMBER_REMOVE, response.g, response.m);
if (member) {
guild._removeMember(member);
}
} }
} }

View File

@@ -66,15 +66,16 @@ class Guild {
_removeMember(guildMember) { _removeMember(guildMember) {
this.store.remove('members', guildMember); this.store.remove('members', guildMember);
if (this.client.ws.status === Constants.Status.READY) {
this.client.emit(Constants.Events.GUILD_MEMBER_REMOVE, this, guildMember);
}
} }
toString() { toString() {
return this.name; return this.name;
} }
kick(member) {
return this.member(member).kick();
}
member(user) { member(user) {
return this.client.resolver.ResolveGuildMember(this, user); return this.client.resolver.ResolveGuildMember(this, user);
} }

View File

@@ -62,6 +62,10 @@ class GuildMember {
deleteDM() { deleteDM() {
return this.client.rest.methods.DeleteChannel(this); return this.client.rest.methods.DeleteChannel(this);
} }
kick() {
return this.client.rest.methods.KickGuildMember(this.guild, this);
}
} }
TextBasedChannel.applyToClass(GuildMember); TextBasedChannel.applyToClass(GuildMember);

View File

@@ -58,6 +58,7 @@ const Endpoints = exports.Endpoints = {
GUILD_BANS: (guildID) => `${Endpoints.GUILD(guildID)}/bans`, GUILD_BANS: (guildID) => `${Endpoints.GUILD(guildID)}/bans`,
GUILD_INTEGRATIONS: (guildID) => `${Endpoints.GUILD(guildID)}/integrations`, GUILD_INTEGRATIONS: (guildID) => `${Endpoints.GUILD(guildID)}/integrations`,
GUILD_MEMBERS: (guildID) => `${Endpoints.GUILD(guildID)}/members`, GUILD_MEMBERS: (guildID) => `${Endpoints.GUILD(guildID)}/members`,
GUILD_MEMBER: (guildID, memberID) => `${Endpoints.GUILD_MEMBERS(guildID)}/${memberID}`,
GUILD_CHANNELS: (guildID) => `${Endpoints.GUILD(guildID)}/channels`, GUILD_CHANNELS: (guildID) => `${Endpoints.GUILD(guildID)}/channels`,
// channels // channels

View File

@@ -128,6 +128,13 @@ client.on('message', message => {
message.author.deleteDM(); message.author.deleteDM();
} }
if (message.content.startsWith('kick')) {
message.guild.member(message.mentions[0]).kick().then(member => {
console.log(member);
message.channel.sendMessage('Kicked!' + member.user.username);
}).catch(console.log);
}
} }
}); });