ESLint stuff...

This commit is contained in:
Amish Shah
2016-08-13 14:44:49 +01:00
parent 53d767ec04
commit b8db4c4f4b
74 changed files with 2574 additions and 2956 deletions

View File

@@ -1,64 +1,61 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const MergeDefault = require('../util/MergeDefault');
const Constants = require('../util/Constants');
const RESTManager = require('./rest/RestManager');
const ClientDataStore = require('../structures/DataStore/ClientDataStore');
const ClientManager = require('./ClientManager');
const ClientDataResolver = require('./ClientDataResolver');
const WebSocketManager = require('./websocket/WebSocketManager');
const ActionsManager = require('./actions/ActionsManager');
/**
* Creates a new Discord Client
* ```js
* const Discord = require("discord.js");
* const client = new Discord.Client();
* ```
*/
class Client extends EventEmitter{
constructor(options) {
super();
this.options = MergeDefault(Constants.DefaultOptions, options);
this.rest = new RESTManager(this);
this.store = new ClientDataStore(this);
this.manager = new ClientManager(this);
this.ws = new WebSocketManager(this);
this.resolver = new ClientDataResolver(this);
this.actions = new ActionsManager(this);
}
/**
* Logs the client in. If successful, resolves with the account's token.
* @param {string} emailOrToken The email or token used for the account. If it is an email, a password _must_ be
* provided.
* @param {string} [password] The password for the account, only needed if an email was provided.
* @return {Promise<String>}
* @example
* client.login("token");
* // or
* client.login("email", "password");
*/
login(email, password) {
if (password) {
// login with email and password
return this.rest.methods.LoginEmailPassword(email, password);
} else {
// login with token
return this.rest.methods.LoginToken(email);
}
}
/**
* The User of the logged in Client, only available after `READY` has been fired.
* @return {ClientUser} [description]
*/
get user() {
return this.store.user;
}
}
module.exports = Client;
const EventEmitter = require('events').EventEmitter;
const mergeDefault = require('../util/MergeDefault');
const Constants = require('../util/Constants');
const RESTManager = require('./rest/RESTManager');
const ClientDataStore = require('../structures/datastore/ClientDataStore');
const ClientManager = require('./ClientManager');
const ClientDataResolver = require('./ClientDataResolver');
const WebSocketManager = require('./websocket/WebSocketManager');
const ActionsManager = require('./actions/ActionsManager');
/**
* Creates a new Discord Client
* ```js
* const Discord = require("discord.js");
* const client = new Discord.Client();
* ```
*/
class Client extends EventEmitter {
constructor(options) {
super();
this.options = mergeDefault(Constants.DefaultOptions, options);
this.rest = new RESTManager(this);
this.store = new ClientDataStore(this);
this.manager = new ClientManager(this);
this.ws = new WebSocketManager(this);
this.resolver = new ClientDataResolver(this);
this.actions = new ActionsManager(this);
}
/**
* Logs the client in. If successful, resolves with the account's token.
* @param {string} emailOrToken The email or token used for the account. If it is an email, a password _must_ be
* provided.
* @param {string} [password] The password for the account, only needed if an email was provided.
* @return {Promise<String>}
* @example
* client.login("token");
* // or
* client.login("email", "password");
*/
login(email, password) {
if (password) {
// login with email and password
return this.rest.methods.loginEmailPassword(email, password);
}
// login with token
return this.rest.methods.loginToken(email);
}
/**
* The User of the logged in Client, only available after `READY` has been fired.
* @return {ClientUser} [description]
*/
get user() {
return this.store.user;
}
}
module.exports = Client;

View File

@@ -1,90 +1,90 @@
'use strict';
const getStructure = name => require(`../structures/${name}`);
const Structure = name => require(`../structures/${name}`);
const User = Structure('User');
const Message = Structure('Message');
const Guild = Structure('Guild');
const Channel = Structure('Channel');
const ServerChannel = Structure('ServerChannel');
const TextChannel = Structure('TextChannel');
const VoiceChannel = Structure('VoiceChannel');
const GuildMember = Structure('GuildMember');
const User = getStructure('User');
const Message = getStructure('Message');
const Guild = getStructure('Guild');
const Channel = getStructure('Channel');
const GuildMember = getStructure('GuildMember');
function $string(obj) {
return (typeof obj === 'string' || obj instanceof String);
return (typeof obj === 'string' || obj instanceof String);
}
class ClientDataResolver {
constructor(client) {
this.client = client;
}
constructor(client) {
this.client = client;
}
ResolveUser(user) {
if (user instanceof User) {
return user;
}else if ($string(user)) {
return this.client.store.get('users', user);
}else if (user instanceof Message) {
return user.author;
}else if (user instanceof Guild) {
return user.owner;
}
resolveUser(user) {
if (user instanceof User) {
return user;
} else if ($string(user)) {
return this.client.store.get('users', user);
} else if (user instanceof Message) {
return user.author;
} else if (user instanceof Guild) {
return user.owner;
}
return null;
}
return null;
}
ResolveGuild(guild) {
if (guild instanceof Guild) {
return guild;
}
}
resolveGuild(guild) {
if (guild instanceof Guild) {
return guild;
}
return null;
}
ResolveGuildMember(guild, user) {
if (user instanceof GuildMember) {
return user;
}
resolveGuildMember($guild, $user) {
let guild = $guild;
let user = $user;
if (user instanceof GuildMember) {
return user;
}
guild = this.ResolveGuild(guild);
user = this.ResolveUser(user);
guild = this.resolveGuild(guild);
user = this.resolveUser(user);
if (!guild || !user) {
return null;
}
if (!guild || !user) {
return null;
}
return guild.store.get('members', user.id);
}
return guild.store.get('members', user.id);
}
ResolveBase64(data) {
if (data instanceof Buffer) {
return 'data:image/jpg;base64,' + data.toString('base64');
}
resolveBase64(data) {
if (data instanceof Buffer) {
return `data:image/jpg;base64,${data.toString('base64')}`;
}
return data;
}
return data;
}
ResolveChannel(channel) {
if (channel instanceof Channel) {
return channel;
}
resolveChannel(channel) {
if (channel instanceof Channel) {
return channel;
}
if ($string(channel)) {
return this.client.store.get('channels', channel);
}
}
if ($string(channel)) {
return this.client.store.get('channels', channel);
}
ResolveString(data) {
if (data instanceof String) {
return data;
}
return null;
}
if (data instanceof Array) {
return data.join('\n');
}
resolveString(data) {
if (data instanceof String) {
return data;
}
return String(data);
}
if (data instanceof Array) {
return data.join('\n');
}
return String(data);
}
}
module.exports = ClientDataResolver;

View File

@@ -1,34 +1,32 @@
'use strict';
const Constants = require('../util/Constants');
class ClientManager {
constructor(client) {
this.client = client;
this.heartbeatInterval = null;
}
constructor(client) {
this.client = client;
this.heartbeatInterval = null;
}
connectToWebSocket(token, resolve, reject) {
this.client.store.token = token;
this.client.rest.methods.GetGateway()
.then(gateway => {
this.client.ws.connect(gateway);
this.client.once(Constants.Events.READY, () => resolve(token));
})
.catch(reject);
connectToWebSocket(token, resolve, reject) {
this.client.store.token = token;
this.client.rest.methods.getGateway()
.then(gateway => {
this.client.ws.connect(gateway);
this.client.once(Constants.Events.READY, () => resolve(token));
})
.catch(reject);
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 15);
}
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 15);
}
setupKeepAlive(time) {
this.heartbeatInterval = setInterval(() => {
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: Date.now(),
});
}, time);
}
setupKeepAlive(time) {
this.heartbeatInterval = setInterval(() => {
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: Date.now(),
});
}, time);
}
}
module.exports = ClientManager;

View File

@@ -1,5 +1,3 @@
'use strict';
/*
ABOUT ACTIONS
@@ -14,14 +12,14 @@ that WebSocket events don't clash with REST methods.
class GenericAction {
constructor(client) {
this.client = client;
}
constructor(client) {
this.client = client;
}
handle(data) {
handle(data) {
return data;
}
}
};
}
module.exports = GenericAction;

View File

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

View File

@@ -1,23 +1,15 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class ChannelCreateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const channel = client.store.newChannel(data);
handle(data) {
let client = this.client;
let channel = client.store.NewChannel(data);
return {
channel,
};
}
};
return {
channel,
};
}
}
module.exports = ChannelCreateAction;

View File

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

View File

@@ -1,39 +1,31 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
const cloneObject = require('../../util/CloneObject');
class ChannelUpdateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.id);
handle(data) {
if (channel) {
const oldChannel = cloneObject(channel);
channel.setup(data);
if (!oldChannel.equals(data)) {
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
}
let client = this.client;
let channel = client.store.get('channels', data.id);
return {
old: oldChannel,
updated: channel,
};
}
if (channel) {
let oldChannel = CloneObject(channel);
channel.setup(data);
if (!oldChannel.equals(data)) {
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
}
return {
old: oldChannel,
updated: channel,
};
}
return {
old: null,
updated: null,
};
}
};
return {
old: null,
updated: null,
};
}
}
module.exports = ChannelUpdateAction;

View File

@@ -1,54 +1,49 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class GuildDeleteAction extends Action {
constructor(client) {
super(client);
this.deleted = {};
this.timeouts = [];
}
constructor(client) {
super(client);
this.deleted = {};
this.timeouts = [];
}
handle(data) {
handle(data) {
const client = this.client;
let guild = client.store.get('guilds', data.id);
let client = this.client;
let guild = client.store.get('guilds', data.id);
if (guild) {
if (guild.available && data.unavailable) {
// guild is unavailable
guild.available = false;
client.emit(Constants.Events.GUILD_UNAVAILABLE, guild);
if (guild) {
if (guild.available && data.unavailable) {
// guild is unavailable
guild.available = false;
client.emit(Constants.Events.GUILD_UNAVAILABLE, guild);
// stops the GuildDelete packet thinking a guild was actually deleted,
// handles emitting of event itself
return {
guild: null,
};
}
// delete guild
client.store.remove('guilds', guild);
this.deleted[guild.id] = guild;
this.scheduleForDeletion(guild.id);
} else if (this.deleted[data.id]) {
guild = this.deleted[data.id];
}
// stops the GuildDelete packet thinking a guild was actually deleted,
// handles emitting of event itself
return {
guild: null,
};
} else {
// delete guild
client.store.remove('guilds', guild);
this.deleted[guild.id] = guild;
this.scheduleForDeletion(guild.id);
}
} else if (this.deleted[data.id]) {
guild = this.deleted[data.id];
}
return {
guild,
};
}
return {
guild,
};
}
scheduleForDeletion(id) {
this.timeouts.push(
setTimeout(() => delete this.deleted[id],
this.client.options.rest_ws_bridge_timeout)
);
}
};
scheduleForDeletion(id) {
this.timeouts.push(
setTimeout(() => delete this.deleted[id],
this.client.options.rest_ws_bridge_timeout)
);
}
}
module.exports = GuildDeleteAction;

View File

@@ -1,51 +1,48 @@
'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 = {};
}
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;
client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member);
this.scheduleForDeletion(guild.id, data.user.id);
}
handle(data) {
const client = this.client;
const 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;
client.emit(Constants.Events.GUILD_MEMBER_REMOVE, guild, member);
this.scheduleForDeletion(guild.id, data.user.id);
}
if (!member) {
member = this.deleted[guild.id + data.user.id];
}
if (!member) {
member = this.deleted[guild.id + data.user.id];
}
return {
g: guild,
m: member,
};
}
return {
g: guild,
m: member,
};
}
return {
g: guild,
m: null,
};
}
return {
g: guild,
m: null,
};
}
scheduleForDeletion(guildID, userID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + userID],
this.client.options.rest_ws_bridge_timeout)
);
}
};
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,38 +1,31 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Role = require('../../structures/Role');
class GuildRoleCreate extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
handle(data) {
if (guild) {
const already = guild.store.get('roles', data.role.id);
const role = new Role(guild, data.role);
guild.store.add('roles', role);
let client = this.client;
let guild = client.store.get('guilds', data.guild_id);
if (!already) {
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
}
if (guild) {
let already = guild.store.get('roles', data.role.id);
let role = new Role(guild, data.role);
guild.store.add('roles', role);
return {
role,
};
}
if (!already) {
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
}
return {
role,
};
}
return {
role: null,
};
}
};
return {
role: null,
};
}
}
module.exports = GuildRoleCreate;

View File

@@ -1,50 +1,47 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class GuildRoleDeleteAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
handle(data) {
let client = this.client;
let guild = client.store.get('guilds', data.guild_id);
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
if (guild) {
let exists = guild.store.get('roles', data.role_id);
if (exists) {
guild.store.remove('roles', data.role_id);
this.deleted[guild.id + data.role_id] = exists;
this.scheduleForDeletion(guild.id, data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
}
if (guild) {
let exists = guild.store.get('roles', data.role_id);
if (exists) {
guild.store.remove('roles', data.role_id);
this.deleted[guild.id + data.role_id] = exists;
this.scheduleForDeletion(guild.id, data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
}
if (!exists) {
exists = this.deleted[guild.id + data.role_id];
}
if (!exists) {
exists = this.deleted[guild.id + data.role_id];
}
return {
role: exists,
};
}
return {
role: exists,
};
}
return {
role: null,
};
}
return {
role: null,
};
}
scheduleForDeletion(guildID, roleID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + roleID],
this.client.options.rest_ws_bridge_timeout)
);
}
};
scheduleForDeletion(guildID, roleID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + roleID],
this.client.options.rest_ws_bridge_timeout)
);
}
}
module.exports = GuildRoleDeleteAction;

View File

@@ -1,44 +1,36 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
const cloneObject = require('../../util/CloneObject');
class GuildRoleUpdateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
handle(data) {
const roleData = data.role;
let client = this.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
let oldRole;
const existingRole = guild.store.get('roles', roleData.id);
// exists and not the same
if (existingRole && !existingRole.equals(roleData)) {
oldRole = cloneObject(existingRole);
existingRole.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
}
let roleData = data.role;
return {
old: oldRole,
updated: existingRole,
};
}
if (guild) {
let oldRole;
let existingRole = guild.store.get('roles', roleData.id);
// exists and not the same
if (existingRole && !existingRole.equals(roleData)) {
oldRole = CloneObject(existingRole);
existingRole.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, guild, oldRole, existingRole);
}
return {
old: oldRole,
updated: existingRole,
};
}
return {
old: null,
updated: null,
};
}
};
return {
old: null,
updated: null,
};
}
}
module.exports = GuildRoleUpdateAction;

View File

@@ -1,42 +1,38 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
const cloneObject = require('../../util/CloneObject');
class GuildUpdateAction extends Action {
constructor(client) {
super(client);
this.deleted = {};
this.timeouts = [];
}
constructor(client) {
super(client);
this.deleted = {};
this.timeouts = [];
}
handle(data) {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.id);
let client = this.client;
let guild = client.store.get('guilds', data.id);
if (guild) {
const oldGuild = cloneObject(guild);
guild.setup(data);
if (guild) {
let oldGuild = CloneObject(guild);
guild.setup(data);
if (!oldGuild.equals(data)) {
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
}
if (!oldGuild.equals(data)) {
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
}
return {
old: oldGuild,
updated: guild,
};
}
return {
old: oldGuild,
updated: guild,
};
}
return {
old: null,
updated: null,
};
}
};
return {
old: null,
updated: null,
};
}
}
module.exports = GuildUpdateAction;

View File

@@ -1,31 +1,23 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class MessageCreateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
handle(data) {
if (channel) {
const message = channel._cacheMessage(new Message(channel, data, client));
return {
m: message,
};
}
let client = this.client;
let channel = client.store.get('channels', data.channel_id);
if (channel) {
let message = channel._cacheMessage(new Message(channel, data, client));
return {
m: message,
};
}
return {
m: null,
};
}
};
return {
m: null,
};
}
}
module.exports = MessageCreateAction;

View File

@@ -1,51 +1,43 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class MessageDeleteAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
handle(data) {
let client = this.client;
let channel = client.store.get('channels', data.channel_id);
if (channel) {
let message = channel.store.get('messages', data.id);
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
if (channel) {
let message = channel.store.get('messages', data.id);
if (message) {
if (message) {
channel.store.remove('messages', message.id);
this.deleted[channel.id + message.id] = message;
this.scheduleForDeletion(channel.id, message.id);
} else if (this.deleted[channel.id + data.id]) {
message = this.deleted[channel.id + data.id];
}
channel.store.remove('messages', message.id);
this.deleted[channel.id + message.id] = message;
this.scheduleForDeletion(channel.id, message.id);
return {
m: message,
};
}
} else if (this.deleted[channel.id + data.id]) {
return {
m: null,
};
}
message = this.deleted[channel.id + data.id];
}
return {
m: message,
};
}
return {
m: null,
};
}
scheduleForDeletion(channelID, messageID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[channelID + messageID],
this.client.options.rest_ws_bridge_timeout)
);
}
};
scheduleForDeletion(channelID, messageID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[channelID + messageID],
this.client.options.rest_ws_bridge_timeout)
);
}
}
module.exports = MessageDeleteAction;

View File

@@ -1,44 +1,36 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
const cloneObject = require('../../util/CloneObject');
class MessageUpdateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
handle(data) {
if (channel) {
const message = channel.store.get('messages', data.id);
if (message && !message.equals(data, true)) {
const oldMessage = cloneObject(message);
message.patch(data);
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
return {
old: oldMessage,
updated: message,
};
}
let client = this.client;
let channel = client.store.get('channels', data.channel_id);
return {
old: message,
updated: message,
};
}
if (channel) {
let message = channel.store.get('messages', data.id);
if (message && !message.equals(data, true)) {
let oldMessage = CloneObject(message);
message.patch(data);
return {
old: oldMessage,
updated: message,
};
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
}
return {
old: message,
updated: message,
};
}
return {
old: null,
updated: null,
};
}
};
return {
old: null,
updated: null,
};
}
}
module.exports = MessageUpdateAction;

View File

@@ -1,44 +1,36 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const CloneObject = require('../../util/CloneObject');
const Message = require('../../structures/Message');
const cloneObject = require('../../util/CloneObject');
class UserUpdateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
const client = this.client;
handle(data) {
if (client.store.user) {
if (client.store.user.equals(data)) {
return {
old: client.store.user,
updated: client.store.user,
};
}
let client = this.client;
const oldUser = cloneObject(client.store.user);
client.store.user.setup(data);
if (client.store.user) {
if (client.store.user.equals(data)) {
return {
old: client.store.user,
updated: client.store.user,
};
}
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
let oldUser = CloneObject(client.store.user);
client.store.user.setup(data);
return {
old: oldUser,
updated: client.store.user,
};
}
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
return {
old: oldUser,
updated: client.store.user,
};
}
return {
old: null,
updated: null,
};
}
};
return {
old: null,
updated: null,
};
}
}
module.exports = UserUpdateAction;

View File

@@ -1,105 +1,110 @@
'use strict';
const request = require('superagent');
const Constants = require('../../util/Constants');
const UserAgentManager = require('./UserAgentManager');
const RESTMethods = require('./RESTMethods');
class RESTManager{
class RESTManager {
constructor(client) {
this.client = client;
this.queue = [];
this.userAgentManager = new UserAgentManager(this);
this.methods = new RESTMethods(this);
this.rateLimitedEndpoints = {};
}
constructor(client) {
this.client = client;
this.queue = [];
this.userAgentManager = new UserAgentManager(this);
this.methods = new RESTMethods(this);
this.rateLimitedEndpoints = {};
}
addRequestToQueue(method, url, auth, data, file, resolve, reject) {
let endpoint = url.replace(/\/[0-9]+/g, '/:id');
addRequestToQueue(method, url, auth, data, file, resolve, reject) {
const endpoint = url.replace(/\/[0-9]+/g, '/:id');
let rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint];
const rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint];
rateLimitedEndpoint.queue = rateLimitedEndpoint.queue || [];
rateLimitedEndpoint.queue = rateLimitedEndpoint.queue || [];
rateLimitedEndpoint.queue.push({
method, url, auth, data, file, resolve, reject,
});
}
rateLimitedEndpoint.queue.push({
method,
url,
auth,
data,
file,
resolve,
reject,
});
}
processQueue(endpoint) {
let rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint];
processQueue(endpoint) {
const rateLimitedEndpoint = this.rateLimitedEndpoints[endpoint];
// prevent multiple queue processes
if (!rateLimitedEndpoint.timeout) {
return;
}
// prevent multiple queue processes
if (!rateLimitedEndpoint.timeout) {
return;
}
// lock the queue
clearTimeout(rateLimitedEndpoint.timeout);
rateLimitedEndpoint.timeout = null;
// lock the queue
clearTimeout(rateLimitedEndpoint.timeout);
rateLimitedEndpoint.timeout = null;
for (let item of rateLimitedEndpoint.queue) {
this.makeRequest(item.method, item.url, item.auth, item.data, item.file)
.then(item.resolve)
.catch(item.reject);
}
for (const item of rateLimitedEndpoint.queue) {
this.makeRequest(item.method, item.url, item.auth, item.data, item.file)
.then(item.resolve)
.catch(item.reject);
}
rateLimitedEndpoint.queue = [];
}
rateLimitedEndpoint.queue = [];
}
makeRequest(method, url, auth, data, file, fromQueue) {
/*
file is {file, name}
*/
let apiRequest = request[method](url);
makeRequest(method, url, auth, data, file) {
/*
file is {file, name}
*/
console.log(url);
const apiRequest = request[method](url);
let endpoint = url.replace(/\/[0-9]+/g, '/:id');
const endpoint = url.replace(/\/[0-9]+/g, '/:id');
if (this.rateLimitedEndpoints[endpoint] && this.rateLimitedEndpoints[endpoint].timeout) {
return new Promise((resolve, reject) => {
this.addRequestToQueue(method, url, auth, data, file, resolve, reject);
});
}
if (this.rateLimitedEndpoints[endpoint] && this.rateLimitedEndpoints[endpoint].timeout) {
return new Promise((resolve, reject) => {
this.addRequestToQueue(method, url, auth, data, file, resolve, reject);
});
}
if (auth) {
if (this.client.store.token) {
apiRequest.set('authorization', this.client.store.token);
} else {
throw Constants.Errors.NO_TOKEN;
}
}
if (auth) {
if (this.client.store.token) {
apiRequest.set('authorization', this.client.store.token);
} else {
throw Constants.Errors.NO_TOKEN;
}
}
if (data) {
apiRequest.send(data);
}
if (data) {
apiRequest.send(data);
}
if (file) {
apiRequest.attach('file', file.file, file.name);
}
if (file) {
apiRequest.attach('file', file.file, file.name);
}
apiRequest.set('User-Agent', this.userAgentManager.userAgent);
apiRequest.set('User-Agent', this.userAgentManager.userAgent);
return new Promise((resolve, reject) => {
apiRequest.end((err, res) => {
if (err) {
let retry = res.headers['retry-after'] || res.headers['Retry-After'];
if (retry) {
this.rateLimitedEndpoints[endpoint] = {};
this.addRequestToQueue(method, url, auth, data, file, resolve, reject);
this.rateLimitedEndpoints[endpoint].timeout = setTimeout(() => {
this.processQueue(endpoint);
}, retry);
return;
}
return new Promise((resolve, reject) => {
apiRequest.end((err, res) => {
if (err) {
const retry = res.headers['retry-after'] || res.headers['Retry-After'];
if (retry) {
this.rateLimitedEndpoints[endpoint] = {};
this.addRequestToQueue(method, url, auth, data, file, resolve, reject);
this.rateLimitedEndpoints[endpoint].timeout = setTimeout(() => {
this.processQueue(endpoint);
}, retry);
return;
}
reject(err);
} else {
resolve(res ? res.body || {} : {});
}
});
});
}
};
reject(err);
} else {
resolve(res ? res.body || {} : {});
}
});
});
}
}
module.exports = RESTManager;

View File

@@ -1,329 +1,328 @@
'use strict';
const Constants = require('../../util/Constants');
const Structure = name => require('../../structures/' + name);
const User = Structure('User');
const GuildMember = Structure('GuildMember');
const Message = Structure('Message');
class RESTMethods{
constructor(restManager) {
this.rest = restManager;
}
const getStructure = name => require(`../../structures/${name}`);
const User = getStructure('User');
const GuildMember = getStructure('GuildMember');
LoginEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.rest.client.store.email = email;
this.rest.client.store.password = password;
this.rest.makeRequest('post', Constants.Endpoints.LOGIN, false, { email, password })
.then(data => {
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
})
.catch(reject);
class RESTMethods {
constructor(restManager) {
this.rest = restManager;
}
});
}
loginEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.rest.client.store.email = email;
this.rest.client.store.password = password;
this.rest.makeRequest('post', Constants.Endpoints.login, false, { email, password })
.then(data => {
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
})
.catch(reject);
});
}
LoginToken(token) {
return new Promise((resolve, reject) => {
this.rest.client.manager.connectToWebSocket(token, resolve, reject);
});
}
loginToken(token) {
return new Promise((resolve, reject) => {
this.rest.client.manager.connectToWebSocket(token, resolve, reject);
});
}
GetGateway() {
return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.GATEWAY, true)
.then(res => resolve(res.url))
.catch(reject);
});
}
getGateway() {
return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
.then(res => resolve(res.url))
.catch(reject);
});
}
SendMessage(channel, content, tts, nonce) {
return new Promise((resolve, reject) => {
sendMessage($channel, content, tts, nonce) {
return new Promise((resolve, reject) => {
const $this = this;
let channel = $channel;
var _this = this;
function req() {
$this.rest.makeRequest('post', Constants.Endpoints.channelMessages(channel.id), true, {
content, tts, nonce,
})
.then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).m))
.catch(reject);
}
if (channel instanceof User || channel instanceof GuildMember) {
this.CreateDM(channel).then(chan => {
channel = chan;
req();
})
.catch(reject);
} else {
req();
}
if (channel instanceof User || channel instanceof GuildMember) {
this.createDM(channel).then(chan => {
channel = chan;
req();
})
.catch(reject);
} else {
req();
}
});
}
function req() {
_this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content, tts, nonce,
})
.then(data => resolve(_this.rest.client.actions.MessageCreate.handle(data).m))
.catch(reject);
}
});
}
deleteMessage(message) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.channelMessage(message.channel.id, message.id), true)
.then(() => {
resolve(this.rest.client.actions.MessageDelete.handle({
id: message.id,
channel_id: message.channel.id,
}).m);
})
.catch(reject);
});
}
DeleteMessage(message) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true)
.then(data => {
resolve(this.rest.client.actions.MessageDelete.handle({
id: message.id,
channel_id: message.channel.id,
}).m);
})
.catch(reject);
});
}
updateMessage(message, content) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('patch', Constants.Endpoints.channelMessage(message.channel.id, message.id), true, {
content,
})
.then(data => {
resolve(this.rest.client.actions.MessageUpdate.handle(data).updated);
})
.catch(reject);
});
}
UpdateMessage(message, content) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true, {
content,
})
.then(data => {
resolve(this.rest.client.actions.MessageUpdate.handle(data).updated);
})
.catch(reject);
});
}
createChannel(guild, channelName, channelType) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.guildChannels(guild.id), true, {
name: channelName,
type: channelType,
})
.then(data => {
resolve(this.rest.client.actions.ChannelCreate.handle(data).channel);
})
.catch(reject);
});
}
CreateChannel(guild, channelName, channelType) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.GUILD_CHANNELS(guild.id), true, {
name: channelName,
type: channelType,
})
.then(data => {
resolve(this.rest.client.actions.ChannelCreate.handle(data).channel);
})
.catch(reject);
});
}
getExistingDM(recipient) {
const dmChannel = this.rest.client.store.getAsArray('channels')
.filter(channel => channel.recipient)
.filter(channel => channel.recipient.id === recipient.id);
GetExistingDM(recipient) {
let dmChannel = this.rest.client.store.getAsArray('channels')
.filter(channel => channel.recipient)
.filter(channel => channel.recipient.id === recipient.id);
return dmChannel[0];
}
return dmChannel[0];
}
createDM(recipient) {
return new Promise((resolve, reject) => {
const dmChannel = this.getExistingDM(recipient);
CreateDM(recipient) {
return new Promise((resolve, reject) => {
if (dmChannel) {
return resolve(dmChannel);
}
let dmChannel = this.GetExistingDM(recipient);
return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.store.user.id), true, {
recipient_id: recipient.id,
})
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
.catch(reject);
});
}
if (dmChannel) {
return resolve(dmChannel);
}
deleteChannel($channel) {
let channel = $channel;
return new Promise((resolve, reject) => {
if (channel instanceof User || channel instanceof GuildMember) {
channel = this.getExistingDM(channel);
}
this.rest.makeRequest('post', Constants.Endpoints.USER_CHANNELS(this.rest.client.store.user.id), true, {
recipient_id: recipient.id,
})
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
.catch(reject);
});
}
this.rest.makeRequest('del', Constants.Endpoints.channel(channel.id), true)
.then($data => {
const data = $data;
data.id = channel.id;
resolve(this.rest.client.actions.ChannelDelete.handle(data).channel);
})
.catch(reject);
});
}
DeleteChannel(channel) {
return new Promise((resolve, reject) => {
if (channel instanceof User || channel instanceof GuildMember) {
channel = this.GetExistingDM(channel);
}
updateChannel(channel, $data) {
const data = $data;
return new Promise((resolve, reject) => {
data.name = (data.name || channel.name).trim();
data.topic = data.topic || channel.topic;
data.position = data.position || channel.position;
data.bitrate = data.bitrate || channel.bitrate;
this.rest.makeRequest('del', Constants.Endpoints.CHANNEL(channel.id), true)
.then(data => {
data.id = channel.id;
resolve(this.rest.client.actions.ChannelDelete.handle(data).channel);
})
.catch(reject);
});
}
this.rest.makeRequest('patch', Constants.Endpoints.channel(channel.id), true, data)
.then(newData => {
resolve(this.rest.client.actions.ChannelUpdate.handle(newData).updated);
})
.catch(reject);
});
}
UpdateChannel(channel, data) {
return new Promise((resolve, reject) => {
data.name = (data.name || channel.name).trim();
data.topic = data.topic || channel.topic;
data.position = data.position || channel.position;
data.bitrate = data.bitrate || channel.bitrate;
leaveGuild(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.meGuild(guild.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id: guild.id }).guild);
})
.catch(reject);
});
}
this.rest.makeRequest('patch', Constants.Endpoints.CHANNEL(channel.id), true, data)
.then(data => {
resolve(this.rest.client.actions.ChannelUpdate.handle(data).updated);
})
.catch(reject);
});
}
// untested but probably will work
deleteGuild(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.guild(guild.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id: guild.id }).guild);
})
.catch(reject);
});
}
LeaveGuild(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.ME_GUILD(guild.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
})
.catch(reject);
});
}
updateCurrentUser(_data) {
return new Promise((resolve, reject) => {
const user = this.rest.client.store.user;
const data = {};
// untested but probably will work
DeleteGuild(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.GUILD(guild.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildDelete.handle({ id:guild.id }).guild);
})
.catch(reject);
});
}
data.username = _data.username || user.username;
data.avatar = this.rest.client.resolver.resolveBase64(_data.avatar) || user.avatar;
if (!user.bot) {
data.password = this.rest.client.store.password;
data.email = _data.email || this.rest.client.store.email;
data.new_password = _data.newPassword;
}
UpdateCurrentUser(_data) {
return new Promise((resolve, reject) => {
let user = this.rest.client.store.user;
let data = {};
this.rest.makeRequest('patch', Constants.Endpoints.me, true, data)
.then(newData => resolve(this.rest.client.actions.UserUpdate.handle(newData).updated))
.catch(reject);
});
}
data.username = _data.username || user.username;
data.avatar = this.rest.client.resolver.ResolveBase64(_data.avatar) || user.avatar;
if (!user.bot) {
data.password = this.rest.client.store.password;
data.email = _data.email || this.rest.client.store.email;
data.new_password = _data.newPassword;
}
updateGuild(guild, _data) {
return new Promise((resolve, reject) => {
/*
can contain:
name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash
*/
this.rest.makeRequest('patch', Constants.Endpoints.ME, true, data)
.then(data => resolve(this.rest.client.actions.UserUpdate.handle(data).updated))
.catch(reject);
});
}
const data = {};
UpdateGuild(guild, _data) {
return new Promise((resolve, reject) => {
/*
can contain:
name, region, verificationLevel, afkChannel, afkTimeout, icon, owner, splash
*/
if (_data.name) {
data.name = _data.name;
}
let data = {};
if (_data.region) {
data.region = _data.region;
}
if (_data.name) {
data.name = _data.name;
}
if (_data.verificationLevel) {
data.verification_level = Number(_data.verificationLevel);
}
if (_data.region) {
data.region = _data.region;
}
if (_data.afkChannel) {
data.afk_channel_id = this.rest.client.resolver.resolveChannel(_data.afkChannel).id;
}
if (_data.verificationLevel) {
data.verification_level = Number(_data.verificationLevel);
}
if (_data.afkTimeout) {
data.afk_timeout = Number(_data.afkTimeout);
}
if (_data.afkChannel) {
data.afk_channel_id = this.rest.client.resolver.ResolveChannel(_data.afkChannel).id;
}
if (_data.icon) {
data.icon = this.rest.client.resolver.resolveBase64(_data.icon);
}
if (_data.afkTimeout) {
data.afk_timeout = Number(_data.afkTimeout);
}
if (_data.owner) {
data.owner_id = this.rest.client.resolver.resolveUser(_data.owner).id;
}
if (_data.icon) {
data.icon = this.rest.client.resolver.ResolveBase64(_data.icon);
}
if (_data.splash) {
data.splash = this.rest.client.resolver.resolveBase64(_data.splash);
}
if (_data.owner) {
data.owner_id = this.rest.client.resolver.ResolveUser(_data.owner).id;
}
this.rest.makeRequest('patch', Constants.Endpoints.guild(guild.id), true, data)
.then(newData => resolve(this.rest.client.actions.GuildUpdate.handle(newData).updated))
.catch(reject);
});
}
if (_data.splash) {
data.splash = this.rest.client.resolver.ResolveBase64(_data.splash);
}
kickGuildMember(guild, member) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.guildMember(guild.id, member.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildMemberRemove.handle({
guild_id: guild.id,
user: member.user,
}).m);
})
.catch(reject);
});
}
this.rest.makeRequest('patch', Constants.Endpoints.GUILD(guild.id), true, data)
.then(data => resolve(this.rest.client.actions.GuildUpdate.handle(data).updated))
.catch(reject);
});
}
createGuildRole(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.guildRoles(guild.id), true)
.then(role => {
resolve(this.rest.client.actions.GuildRoleCreate.handle({
guild_id: guild.id,
role,
}).role);
})
.catch(reject);
});
}
KickGuildMember(guild, member) {
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);
});
}
deleteGuildRole(role) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.guildRole(role.guild.id, role.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildRoleDelete.handle({
guild_id: role.guild.id,
role_id: role.id,
}).role);
})
.catch(reject);
});
}
CreateGuildRole(guild) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('post', Constants.Endpoints.GUILD_ROLES(guild.id), true)
.then(role => {
resolve(this.rest.client.actions.GuildRoleCreate.handle({
guild_id: guild.id,
role,
}).role);
})
.catch(reject);
});
}
updateGuildRole(role, _data) {
return new Promise((resolve, reject) => {
/*
can contain:
name, position, permissions, color, hoist
*/
DeleteGuildRole(role) {
return new Promise((resolve, reject) => {
this.rest.makeRequest('del', Constants.Endpoints.GUILD_ROLE(role.guild.id, role.id), true)
.then(() => {
resolve(this.rest.client.actions.GuildRoleDelete.handle({
guild_id: role.guild.id,
role_id: role.id,
}).role);
})
.catch(reject);
});
}
const data = {};
UpdateGuildRole(role, _data) {
return new Promise((resolve, reject) => {
/*
can contain:
name, position, permissions, color, hoist
*/
data.name = _data.name || role.name;
data.position = _data.position || role.position;
data.color = _data.color || role.color;
let data = {};
if (typeof _data.hoist !== 'undefined') {
data.hoist = _data.hoist;
} else {
data.hoist = role.hoist;
}
data.name = _data.name || role.name;
data.position = _data.position || role.position;
data.color = _data.color || role.color;
if (_data.permissions) {
let perms = 0;
for (let perm of _data.permissions) {
if (perm instanceof String || typeof perm === 'string') {
perm = Constants.PermissionFlags[perm];
}
perms |= perm;
}
data.permissions = perms;
} else {
data.permissions = role.permissions;
}
if (typeof _data.hoist !== 'undefined') {
data.hoist = _data.hoist;
} else {
data.hoist = role.hoist;
}
if (_data.permissions) {
let perms = 0;
for (let perm of _data.permissions) {
if (perm instanceof String || typeof perm === 'string') {
perm = Constants.PermissionFlags[perm];
}
perms |= perm;
}
data.permissions = perms;
} else {
data.permissions = role.permissions;
}
console.log(data);
this.rest.makeRequest('patch', Constants.Endpoints.GUILD_ROLE(role.guild.id, role.id), true, data)
.then(_role => {
resolve(this.rest.client.actions.GuildRoleUpdate.handle({
role: _role,
guild_id: role.guild.id,
}).updated);
})
.catch(reject);
});
}
this.rest.makeRequest('patch', Constants.Endpoints.guildRole(role.guild.id, role.id), true, data)
.then(_role => {
resolve(this.rest.client.actions.GuildRoleUpdate.handle({
role: _role,
guild_id: role.guild.id,
}).updated);
})
.catch(reject);
});
}
}
module.exports = RESTMethods;

View File

@@ -1,24 +1,22 @@
'use strict';
const Constants = require('../../util/Constants');
class UserAgentManager{
constructor(restManager) {
this.restManager = restManager;
this._userAgent = {
url: 'https://github.com/hydrabolt/discord.js',
version: Constants.Package.version,
};
}
class UserAgentManager {
constructor(restManager) {
this.restManager = restManager;
this._userAgent = {
url: 'https://github.com/hydrabolt/discord.js',
version: Constants.Package.version,
};
}
set(info) {
this._userAgent.url = info.url || 'https://github.com/hydrabolt/discord.js';
this._userAgent.version = info.version || Constants.Package.version;
}
set(info) {
this._userAgent.url = info.url || 'https://github.com/hydrabolt/discord.js';
this._userAgent.version = info.version || Constants.Package.version;
}
get userAgent() {
return `DiscordBot (${this._userAgent.url}, ${this._userAgent.version})`;
}
get userAgent() {
return `DiscordBot (${this._userAgent.url}, ${this._userAgent.version})`;
}
}
module.exports = UserAgentManager;

View File

@@ -1,5 +1,3 @@
'use strict';
const WebSocket = require('ws');
const Constants = require('../../util/Constants');
const zlib = require('zlib');
@@ -8,111 +6,111 @@ const WebSocketManagerDataStore = require('../../structures/datastore/WebSocketM
class WebSocketManager {
constructor(client) {
this.client = client;
this.ws = null;
this.packetManager = new PacketManager(this);
this.store = new WebSocketManagerDataStore();
this.status = Constants.Status.IDLE;
}
constructor(client) {
this.client = client;
this.ws = null;
this.packetManager = new PacketManager(this);
this.store = new WebSocketManagerDataStore();
this.status = Constants.Status.IDLE;
}
connect(gateway) {
this.status = Constants.Status.CONNECTING;
this.store.gateway = gateway;
gateway += `/?v=${this.client.options.protocol_version}`;
this.ws = new WebSocket(gateway);
this.ws.onopen = () => this.EventOpen();
this.ws.onclose = () => this.EventClose();
this.ws.onmessage = (e) => this.EventMessage(e);
this.ws.onerror = (e) => this.EventError(e);
}
connect(gateway) {
this.status = Constants.Status.CONNECTING;
this.store.gateway = `${gateway}/?v=${this.client.options.protocol_version}`;
this.ws = new WebSocket(gateway);
this.ws.onopen = () => this.eventOpen();
this.ws.onclose = () => this.eventClose();
this.ws.onmessage = (e) => this.eventMessage(e);
this.ws.onerror = (e) => this.eventError(e);
}
send(data) {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
}
}
send(data) {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
}
}
EventOpen() {
if (this.reconnecting) {
this._sendResume();
} else {
this._sendNewIdentify();
}
}
eventOpen() {
if (this.reconnecting) {
this._sendResume();
} else {
this._sendNewIdentify();
}
}
_sendResume() {
let payload = {
token: this.client.store.token,
session_id: this.store.sessionID,
seq: this.store.sequence,
};
_sendResume() {
const payload = {
token: this.client.store.token,
session_id: this.store.sessionID,
seq: this.store.sequence,
};
this.send({
op: Constants.OPCodes.RESUME,
d: payload,
});
}
this.send({
op: Constants.OPCodes.RESUME,
d: payload,
});
}
_sendNewIdentify() {
this.reconnecting = false;
let payload = this.client.options.ws;
payload.token = this.client.store.token;
_sendNewIdentify() {
this.reconnecting = false;
const payload = this.client.options.ws;
payload.token = this.client.store.token;
this.send({
op: Constants.OPCodes.IDENTIFY,
d: payload,
});
}
this.send({
op: Constants.OPCodes.IDENTIFY,
d: payload,
});
}
EventClose() {
if (!this.reconnecting) {
this.tryReconnect();
}
}
eventClose() {
if (!this.reconnecting) {
this.tryReconnect();
}
}
EventMessage(event) {
let packet;
try {
if (event.binary) {
event.data = zlib.inflateSync(event.data).toString();
}
eventMessage($event) {
let packet;
const event = $event;
try {
if (event.binary) {
event.data = zlib.inflateSync(event.data).toString();
}
packet = JSON.parse(event.data);
} catch (e) {
return this.EventError(Constants.Errors.BAD_WS_MESSAGE);
}
packet = JSON.parse(event.data);
} catch (e) {
return this.eventError(Constants.Errors.BAD_WS_MESSAGE);
}
this.packetManager.handle(packet);
}
return this.packetManager.handle(packet);
}
EventError(e) {
this.tryReconnect();
}
EventError() {
this.tryReconnect();
}
checkIfReady() {
if (this.status !== Constants.Status.READY) {
let unavailableCount = 0;
checkIfReady() {
if (this.status !== Constants.Status.READY) {
let unavailableCount = 0;
for (let guildID in this.client.store.data.guilds) {
unavailableCount += this.client.store.data.guilds[guildID].available ? 0 : 1;
}
for (const guildID in this.client.store.data.guilds) {
unavailableCount += this.client.store.data.guilds[guildID].available ? 0 : 1;
}
if (unavailableCount === 0) {
this.status = Constants.Status.READY;
this.client.emit(Constants.Events.READY);
this.packetManager.handleQueue();
}
}
}
if (unavailableCount === 0) {
this.status = Constants.Status.READY;
this.client.emit(Constants.Events.READY);
this.packetManager.handleQueue();
}
}
}
tryReconnect() {
this.status = Constants.Status.RECONNECTING;
this.ws.close();
this.packetManager.handleQueue();
this.client.emit(Constants.Events.RECONNECTING);
this.connect(this.store.gateway);
}
tryReconnect() {
this.status = Constants.Status.RECONNECTING;
this.ws.close();
this.packetManager.handleQueue();
this.client.emit(Constants.Events.RECONNECTING);
this.connect(this.store.gateway);
}
}
module.exports = WebSocketManager;

View File

@@ -1,100 +1,97 @@
'use strict';
const Constants = require('../../../util/Constants');
const BeforeReadyWhitelist = [
Constants.WSEvents.READY,
Constants.WSEvents.GUILD_CREATE,
Constants.WSEvents.GUILD_DELETE,
Constants.WSEvents.READY,
Constants.WSEvents.GUILD_CREATE,
Constants.WSEvents.GUILD_DELETE,
];
class WebSocketPacketManager {
constructor(websocketManager) {
this.ws = websocketManager;
this.handlers = {};
this.queue = [];
constructor(websocketManager) {
this.ws = websocketManager;
this.handlers = {};
this.queue = [];
this.register(Constants.WSEvents.READY, 'Ready');
this.register(Constants.WSEvents.GUILD_CREATE, 'GuildCreate');
this.register(Constants.WSEvents.GUILD_DELETE, 'GuildDelete');
this.register(Constants.WSEvents.GUILD_UPDATE, 'GuildUpdate');
this.register(Constants.WSEvents.GUILD_BAN_ADD, 'GuildBanAdd');
this.register(Constants.WSEvents.GUILD_BAN_REMOVE, 'GuildBanRemove');
this.register(Constants.WSEvents.GUILD_MEMBER_ADD, 'GuildMemberAdd');
this.register(Constants.WSEvents.GUILD_MEMBER_REMOVE, 'GuildMemberRemove');
this.register(Constants.WSEvents.GUILD_MEMBER_UPDATE, 'GuildMemberUpdate');
this.register(Constants.WSEvents.GUILD_ROLE_CREATE, 'GuildRoleCreate');
this.register(Constants.WSEvents.GUILD_ROLE_DELETE, 'GuildRoleDelete');
this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, 'GuildRoleUpdate');
this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, 'GuildMembersChunk');
this.register(Constants.WSEvents.CHANNEL_CREATE, 'ChannelCreate');
this.register(Constants.WSEvents.CHANNEL_DELETE, 'ChannelDelete');
this.register(Constants.WSEvents.CHANNEL_UPDATE, 'ChannelUpdate');
this.register(Constants.WSEvents.PRESENCE_UPDATE, 'PresenceUpdate');
this.register(Constants.WSEvents.USER_UPDATE, 'UserUpdate');
this.register(Constants.WSEvents.VOICE_STATE_UPDATE, 'VoiceStateUpdate');
this.register(Constants.WSEvents.TYPING_START, 'TypingStart');
this.register(Constants.WSEvents.MESSAGE_CREATE, 'MessageCreate');
this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete');
this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate');
}
this.register(Constants.WSEvents.READY, 'Ready');
this.register(Constants.WSEvents.GUILD_CREATE, 'GuildCreate');
this.register(Constants.WSEvents.GUILD_DELETE, 'GuildDelete');
this.register(Constants.WSEvents.GUILD_UPDATE, 'GuildUpdate');
this.register(Constants.WSEvents.GUILD_BAN_ADD, 'GuildBanAdd');
this.register(Constants.WSEvents.GUILD_BAN_REMOVE, 'GuildBanRemove');
this.register(Constants.WSEvents.GUILD_MEMBER_ADD, 'GuildMemberAdd');
this.register(Constants.WSEvents.GUILD_MEMBER_REMOVE, 'GuildMemberRemove');
this.register(Constants.WSEvents.GUILD_MEMBER_UPDATE, 'GuildMemberUpdate');
this.register(Constants.WSEvents.GUILD_ROLE_CREATE, 'GuildRoleCreate');
this.register(Constants.WSEvents.GUILD_ROLE_DELETE, 'GuildRoleDelete');
this.register(Constants.WSEvents.GUILD_ROLE_UPDATE, 'GuildRoleUpdate');
this.register(Constants.WSEvents.GUILD_MEMBERS_CHUNK, 'GuildMembersChunk');
this.register(Constants.WSEvents.CHANNEL_CREATE, 'ChannelCreate');
this.register(Constants.WSEvents.CHANNEL_DELETE, 'ChannelDelete');
this.register(Constants.WSEvents.CHANNEL_UPDATE, 'ChannelUpdate');
this.register(Constants.WSEvents.PRESENCE_UPDATE, 'PresenceUpdate');
this.register(Constants.WSEvents.USER_UPDATE, 'UserUpdate');
this.register(Constants.WSEvents.VOICE_STATE_UPDATE, 'VoiceStateUpdate');
this.register(Constants.WSEvents.TYPING_START, 'TypingStart');
this.register(Constants.WSEvents.MESSAGE_CREATE, 'MessageCreate');
this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete');
this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate');
}
get client() {
return this.ws.client;
}
get client() {
return this.ws.client;
}
register(event, handle) {
let Handler = require(`./handlers/${handle}`);
this.handlers[event] = new Handler(this);
}
register(event, handle) {
const Handler = require(`./handlers/${handle}`);
this.handlers[event] = new Handler(this);
}
handleQueue() {
for (let packetIndex in this.queue) {
this.handle(this.queue[packetIndex]);
this.queue.splice(packetIndex, 1);
}
}
handleQueue() {
this.queue.forEach((element, index) => {
this.handle(this.queue[index]);
this.queue.splice(index, 1);
});
}
setSequence(s) {
if (s && s > this.ws.store.sequence) {
this.ws.store.sequence = s;
}
}
setSequence(s) {
if (s && s > this.ws.store.sequence) {
this.ws.store.sequence = s;
}
}
handle(packet) {
handle(packet) {
if (packet.op === Constants.OPCodes.RECONNECT) {
this.setSequence(packet.s);
this.ws.tryReconnect();
return false;
}
if (packet.op === Constants.OPCodes.RECONNECT) {
this.setSequence(packet.s);
this.ws.tryReconnect();
return;
}
if (packet.op === Constants.OPCodes.INVALID_SESSION) {
this.ws._sendNewIdentify();
return false;
}
if (packet.op === Constants.OPCodes.INVALID_SESSION) {
this.ws._sendNewIdentify();
return;
}
if (this.ws.reconnecting) {
this.ws.reconnecting = false;
this.ws.checkIfReady();
}
if (this.ws.reconnecting) {
this.ws.reconnecting = false;
this.ws.checkIfReady();
}
this.setSequence(packet.s);
this.setSequence(packet.s);
if (this.ws.status !== Constants.Status.READY) {
if (BeforeReadyWhitelist.indexOf(packet.t) === -1) {
this.queue.push(packet);
return false;
}
}
if (this.ws.status !== Constants.Status.READY) {
if (BeforeReadyWhitelist.indexOf(packet.t) === -1) {
this.queue.push(packet);
return;
}
}
if (this.handlers[packet.t]) {
return this.handlers[packet.t].handle(packet);
}
if (this.handlers[packet.t]) {
return this.handlers[packet.t].handle(packet);
}
return false;
}
return false;
}
}

View File

@@ -1,14 +1,12 @@
'use strict';
class AbstractHandler {
constructor(packetManager) {
this.packetManager = packetManager;
}
constructor(packetManager) {
this.packetManager = packetManager;
}
handle(packet) {
}
handle(packet) {
return packet;
}
}
module.exports = AbstractHandler;

View File

@@ -1,32 +1,20 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const DMChannel = Structure('DMChannel');
const Constants = require('../../../../util/Constants');
class ChannelCreateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const response = client.actions.ChannelCreate.handle(data);
let response = client.actions.ChannelCreate.handle(data);
if (response.channel) {
client.emit(Constants.Events.CHANNEL_CREATE, response.channel);
}
}
if (response.channel) {
client.emit(Constants.Events.CHANNEL_CREATE, response.channel);
}
}
};
}
module.exports = ChannelCreateHandler;

View File

@@ -1,31 +1,20 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const ServerChannel = Structure('ServerChannel');
const Constants = require('../../../../util/Constants');
class ChannelDeleteHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const response = client.actions.ChannelDelete.handle(data);
let response = client.actions.ChannelDelete.handle(data);
if (response.channel) {
client.emit(Constants.Events.CHANNEL_DELETE, response.channel);
}
}
if (response.channel) {
client.emit(Constants.Events.CHANNEL_DELETE, response.channel);
}
}
};
}
module.exports = ChannelDeleteHandler;

View File

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

View File

@@ -1,33 +1,22 @@
'use strict';
// ##untested handler##
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildBanAddHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const user = client.store.get('users', data.user.id);
let guild = client.store.get('guilds', data.guild_id);
let user = client.store.get('users', data.user.id);
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_ADD, guild, user);
}
}
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_ADD, guild, user);
}
}
};
}
module.exports = GuildBanAddHandler;

View File

@@ -1,33 +1,23 @@
'use strict';
// ##untested handler##
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildBanRemoveHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const user = client.store.get('users', data.user.id);
let guild = client.store.get('guilds', data.guild_id);
let user = client.store.get('users', data.user.id);
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
}
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
}
};
}
module.exports = GuildBanRemoveHandler;

View File

@@ -1,38 +1,25 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildCreateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const guild = client.store.get('guilds', data.id);
let guild = client.store.get('guilds', data.id);
if (guild) {
if (!guild.available && !data.unavailable) {
// a newly available guild
guild.setup(data);
this.packetManager.ws.checkIfReady();
}
} else {
// a new guild
client.store.newGuild(data);
}
}
if (guild) {
if (!guild.available && !data.unavailable) {
// a newly available guild
guild.setup(data);
this.packetManager.ws.checkIfReady();
}
} else {
// a new guild
client.store.NewGuild(data);
}
}
};
}
module.exports = GuildCreateHandler;

View File

@@ -1,31 +1,19 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildDeleteHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const response = client.actions.GuildDelete.handle(data);
let response = client.actions.GuildDelete.handle(data);
if (response.guild) {
client.emit(Constants.Events.GUILD_DELETE, response.guild);
}
}
if (response.guild) {
client.emit(Constants.Events.GUILD_DELETE, response.guild);
}
}
};
}
module.exports = GuildDeleteHandler;

View File

@@ -1,32 +1,20 @@
'use strict';
// ##untested handler##
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildMemberAddHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
guild._addMember(data);
}
}
if (guild) {
guild._addMember(data);
}
}
};
}
module.exports = GuildMemberAddHandler;

View File

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

View File

@@ -1,35 +1,23 @@
'use strict';
// ##untested handler##
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const Constants = require('../../../../util/Constants');
class GuildMemberUpdateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
const member = guild.store.get('members', data.user.id);
if (member) {
guild._updateMember(member, data);
}
}
}
if (guild) {
let member = guild.store.get('members', data.user.id);
if (member) {
guild._updateMember(member, data);
}
}
}
};
}
module.exports = GuildMemberUpdateHandler;

View File

@@ -1,33 +1,25 @@
'use strict';
// ##untested##
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject');
class GuildMembersChunkHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id);
const members = [];
if (guild) {
for (const member of guild.members) {
members.push(guild._addMember(member, true));
}
}
let members = [];
if (guild) {
for (let member of guild.members) {
members.push(guild._addMember(member, true));
}
}
client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, guild, members);
}
client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, guild, members);
}
};
}
module.exports = GuildMembersChunkHandler;

View File

@@ -1,25 +1,14 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Role = Structure('Role');
const Guild = Structure('Guild');
class GuildRoleCreateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
client.actions.GuildRoleCreate.handle(data);
}
let response = client.actions.GuildRoleCreate.handle(data);
}
};
}
module.exports = GuildRoleCreateHandler;

View File

@@ -1,25 +1,14 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Role = Structure('Role');
const Guild = Structure('Guild');
class GuildRoleDeleteHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let response = client.actions.GuildRoleDelete.handle(data);
}
};
module.exports = GuildRoleDeleteHandler;
const AbstractHandler = require('./AbstractHandler');
class GuildRoleDeleteHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
client.actions.GuildRoleDelete.handle(data);
}
}
module.exports = GuildRoleDeleteHandler;

View File

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

View File

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

View File

@@ -1,29 +1,19 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Message = Structure('Message');
const Guild = Structure('Guild');
class MessageCreateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const response = client.actions.MessageCreate.handle(data);
let response = client.actions.MessageCreate.handle(data);
if (response.m) {
client.emit(Constants.Events.MESSAGE_CREATE, response.m);
}
}
if (response.m) {
client.emit(Constants.Events.MESSAGE_CREATE, response.m);
}
}
};
}
module.exports = MessageCreateHandler;

View File

@@ -1,29 +1,19 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const Message = Structure('Message');
const Guild = Structure('Guild');
class MessageDeleteHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
const response = client.actions.MessageDelete.handle(data);
let response = client.actions.MessageDelete.handle(data);
if (response.m) {
client.emit(Constants.Events.MESSAGE_DELETE, response.m);
}
}
if (response.m) {
client.emit(Constants.Events.MESSAGE_DELETE, response.m);
}
}
};
}
module.exports = MessageDeleteHandler;

View File

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

View File

@@ -1,75 +1,66 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject');
const Role = Structure('User');
const cloneObject = require('../../../../util/CloneObject');
class PresenceUpdateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
let user = client.store.get('users', data.user.id);
const guild = client.store.get('guilds', data.guild_id);
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let user = client.store.get('users', data.user.id);
let guild = client.store.get('guilds', data.guild_id);
function makeUser(newUser) {
return client.store.newUser(newUser);
}
function makeUser(user) {
return client.store.NewUser(user);
}
// step 1
if (!user) {
if (data.user.username) {
user = makeUser(data.user);
} else {
return;
}
}
// step 1
if (!user) {
if (data.user.username) {
user = makeUser(data.user);
}else {
return;
}
}
if (guild) {
const memberInGuild = guild.store.get('members', user.id);
if (!memberInGuild) {
const member = guild._addMember({
user,
roles: data.roles,
deaf: false,
mute: false,
}, true);
client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, guild, member);
}
}
if (guild) {
let memberInGuild = guild.store.get('members', user.id);
if (!memberInGuild) {
let member = guild._addMember({
user,
roles: data.roles,
deaf: false,
mute: false,
}, true);
client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, guild, member);
}
}
data.user.username = data.user.username || user.username;
data.user.id = data.user.id || user.id;
data.user.discriminator = data.user.discriminator || user.discriminator;
data.user.username = data.user.username || user.username;
data.user.id = data.user.id || user.id;
data.user.discriminator = data.user.discriminator || user.discriminator;
// comment out avatar patching as it causes bugs (see #297)
// data.user.avatar = data.user.avatar || user.avatar;
data.user.status = data.status || user.status;
data.user.game = data.game;
// comment out avatar patching as it causes bugs (see #297)
// data.user.avatar = data.user.avatar || user.avatar;
data.user.status = data.status || user.status;
data.user.game = data.game;
const same = (
data.user.username === user.username &&
data.user.id === user.id &&
data.user.discriminator === user.discriminator &&
data.user.avatar === user.avatar &&
data.user.status === user.status &&
JSON.stringify(data.user.game) === JSON.stringify(user.game)
);
let same = (
data.user.username === user.username &&
data.user.id === user.id &&
data.user.discriminator === user.discriminator &&
data.user.avatar === user.avatar &&
data.user.status === user.status &&
JSON.stringify(data.user.game) === JSON.stringify(user.game)
);
if (!same) {
const oldUser = cloneObject(user);
user.setup(data.user);
client.emit(Constants.Events.PRESENCE_UPDATE, oldUser, user);
}
}
if (!same) {
let oldUser = CloneObject(user);
user.setup(data.user);
client.emit(Constants.Events.PRESENCE_UPDATE, oldUser, user);
}
}
};
}
module.exports = PresenceUpdateHandler;

View File

@@ -1,39 +1,30 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const ClientUser = Structure('ClientUser');
const Guild = Structure('Guild');
const DMChannel = Structure('DMChannel');
const getStructure = name => require(`../../../../structures/${name}`);
const ClientUser = getStructure('ClientUser');
class ReadyHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
client.manager.setupKeepAlive(data.heartbeat_interval);
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
client.manager.setupKeepAlive(data.heartbeat_interval);
client.store.user = client.store.add('users', new ClientUser(client, data.user));
client.store.user = client.store.add('users', new ClientUser(client, data.user));
for (const guild of data.guilds) {
client.store.newGuild(guild);
}
for (let guild of data.guilds) {
client.store.NewGuild(guild);
}
for (const privateDM of data.private_channels) {
client.store.newChannel(privateDM);
}
for (let privateDM of data.private_channels) {
client.store.NewChannel(privateDM);
}
this.packetManager.ws.store.sessionID = data.session_id;
this.packetManager.ws.store.sessionID = data.session_id;
this.packetManager.ws.checkIfReady();
}
this.packetManager.ws.checkIfReady();
}
};
}
module.exports = ReadyHandler;

View File

@@ -1,60 +1,52 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject');
class TypingData {
constructor(since, lastTimestamp, _timeout) {
this.since = since;
this.lastTimestamp = lastTimestamp;
this._timeout = _timeout;
}
constructor(since, lastTimestamp, _timeout) {
this.since = since;
this.lastTimestamp = lastTimestamp;
this._timeout = _timeout;
}
resetTimeout(_timeout) {
clearTimeout(this._timeout);
this._timeout = _timeout;
}
resetTimeout(_timeout) {
clearTimeout(this._timeout);
this._timeout = _timeout;
}
get elapsedTime() {
return Date.now() - this.since;
}
get elapsedTime() {
return Date.now() - this.since;
}
}
class TypingStartHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const channel = client.store.get('channels', data.channel_id);
const user = client.store.get('users', data.user_id);
const timestamp = new Date(data.timestamp * 1000);
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let channel = client.store.get('channels', data.channel_id);
let user = client.store.get('users', data.user_id);
let timestamp = new Date(data.timestamp * 1000);
function tooLate() {
return setTimeout(() => {
client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]);
delete channel.typingMap[user.id];
}, 6000);
}
if (channel && user) {
if (channel.typingMap[user.id]) {
// already typing, renew
let mapping = channel.typingMap[user.id];
mapping.lastTimestamp = timestamp;
mapping.resetTimeout(tooLate());
} else {
channel.typingMap[user.id] = new TypingData(timestamp, timestamp, tooLate());
client.emit(Constants.Events.TYPING_START, channel, user);
}
}
if (channel && user) {
if (channel.typingMap[user.id]) {
// already typing, renew
const mapping = channel.typingMap[user.id];
mapping.lastTimestamp = timestamp;
mapping.resetTimeout(tooLate());
} else {
channel.typingMap[user.id] = new TypingData(timestamp, timestamp, tooLate());
client.emit(Constants.Events.TYPING_START, channel, user);
}
}
}
function tooLate() {
return setTimeout(() => {
client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]);
delete channel.typingMap[user.id];
}, 6000);
}
}
};
}
module.exports = TypingStartHandler;

View File

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

View File

@@ -1,43 +1,34 @@
'use strict';
const AbstractHandler = require('./AbstractHandler');
const Structure = name => require(`../../../../structures/${name}`);
const Constants = require('../../../../util/Constants');
const CloneObject = require('../../../../util/CloneObject');
const Role = Structure('User');
const Constants = require('../../../../util/Constants');
const cloneObject = require('../../../../util/CloneObject');
class VoiceStateUpdateHandler extends AbstractHandler {
constructor(packetManager) {
super(packetManager);
}
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
handle(packet) {
let data = packet.d;
let client = this.packetManager.client;
let guild = client.store.get('guilds', data.guild_id);
if (guild) {
const member = guild.store.get('members', data.user_id);
if (member) {
const oldVoiceChannelMember = cloneObject(member);
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
member.voiceChannel.store.remove('members', oldVoiceChannelMember);
}
if (guild) {
let member = guild.store.get('members', data.user_id);
let channel = guild.store.get('channels', data.channel_id);
if (member) {
let oldVoiceChannelMember = CloneObject(member);
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
member.voiceChannel.store.remove('members', oldVoiceChannelMember);
}
member.serverMute = data.mute;
member.serverDeaf = data.deaf;
member.selfMute = data.self_mute;
member.selfDeaf = data.self_deaf;
member.voiceSessionID = data.session_id;
member.voiceChannelID = data.channel_id;
client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member);
}
}
}
member.serverMute = data.mute;
member.serverDeaf = data.deaf;
member.selfMute = data.self_mute;
member.selfDeaf = data.self_deaf;
member.voiceSessionID = data.session_id;
member.voiceChannelID = data.channel_id;
client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member);
}
}
}
};
}
module.exports = VoiceStateUpdateHandler;