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,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;