Skip to content

Commit

Permalink
all sort of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed May 17, 2017
1 parent 1e0e773 commit 86bd061
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/client/rest/APIRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const paramable = [
'channels', 'users', 'guilds', 'members',
'bans', 'emojis', 'pins', 'permissions',
'reactions', 'webhooks', 'messages',
'notes', 'roles', 'applications',
'invite',
];
const reflectors = ['toString', 'valueOf', 'inspect', Symbol.toPrimitive, util.inspect.custom];

Expand Down
19 changes: 9 additions & 10 deletions src/structures/ClientUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ClientUser extends User {
return this.edit({ avatar });
} else {
return this.client.resolver.resolveBuffer(avatar)
.then(data => this.edit({ avatar: data }));
.then(data => this.edit({ avatar: this.client.resolver.resolveBase64(data) || null }));
}
}

Expand Down Expand Up @@ -282,33 +282,32 @@ class ClientUser extends User {
if (options.guild instanceof Guild) options.guild = options.guild.id;
Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options);

return this.client.api.users('@me').mentions({ query: options }).get()
.then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client)));
return this.client.api.users('@me').mentions.get({ query: options })
.then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client)));
}

/**
* Creates a guild.
* <warn>This is only available when using a user account.</warn>
* @param {string} name The name of the guild
* @param {string} region The region for the server
* @param {string} [region] The region for the server
* @param {BufferResolvable|Base64Resolvable} [icon=null] The icon for the guild
* @returns {Promise<Guild>} The guild that was created
*/
createGuild(name, region, icon = null) {
if (!icon || (typeof icon === 'string' && icon.startsWith('data:'))) {
icon = this.client.resolver.resolveBase64(icon) || null;
return this.client.api.guilds.post({ data: { name, region, icon } })
.then(data => this.client.dataManager.newGuild(data));
.then(data => this.client.dataManager.newGuild(data));
} else {
return this.client.resolver.resolveBuffer(icon)
.then(data => this.createGuild(name, region, data));
.then(data => this.createGuild(name, region, this.client.resolver.resolveBase64(data) || null));
}
}

/**
* An object containing either a user or access token, and an optional nickname.
* @typedef {Object} GroupDMRecipientOptions
* @property {UserResolvable|Snowflake} [user] User to add to the Group DM
* @property {UserResolvable} [user] User to add to the Group DM
* (only available if a user is creating the DM)
* @property {string} [accessToken] Access token to use to add a user to the Group DM
* (only available if a bot is creating the DM)
Expand All @@ -324,8 +323,8 @@ class ClientUser extends User {
const data = this.bot ? {
access_tokens: recipients.map(u => u.accessToken),
nicks: recipients.map(u => u.nick),
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user)) };
return this.client.api.post({ data })
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u)) };
return this.client.api.users('@me').channels.post({ data })
.then(res => new GroupDMChannel(this.client, res));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Guild {
size = format;
format = 'default';
}
return Constants.Endpoints.Guild(this).Icon(this.client.options.http.cdn, this.icon, format, size);
return Constants.Endpoints.CDN(this.client.options.http.cdn).Icon(this.id, this.icon, format, size);
}

/**
Expand All @@ -289,7 +289,7 @@ class Guild {
*/
get splashURL() {
if (!this.splash) return null;
return Constants.Endpoints.Guild(this).Splash(this.client.options.http.cdn, this.splash);
return Constants.Endpoints.CDN(this.client.options.http.cdn).Splash(this.id, this.splash);
}

/**
Expand Down Expand Up @@ -437,7 +437,7 @@ class Guild {
* @param {string|number} [options.type] Only show entries involving this action type
* @returns {Promise<GuildAuditLogs>}
*/
fetchAuditLogs(options) {
fetchAuditLogs(options = {}) {
if (options.before && options.before instanceof GuildAuditLogs.Entry) options.before = options.before.id;
if (options.after && options.after instanceof GuildAuditLogs.Entry) options.after = options.after.id;
if (typeof options.type === 'string') options.type = GuildAuditLogs.Actions[options.type];
Expand Down Expand Up @@ -810,7 +810,7 @@ class Guild {
* .then(pruned => console.log(`I just pruned ${pruned} people!`))
* .catch(console.error);
*/
pruneMembers({ days = 7, dry = false, reason }) {
pruneMembers({ days = 7, dry = false, reason } = {}) {
if (typeof days !== 'number') throw new TypeError('Days must be a number.');
return this.client.api.guilds(this.id).prune[dry ? 'get' : 'post']({ query: { days }, reason })
.then(data => data.pruned);
Expand Down Expand Up @@ -838,7 +838,7 @@ class Guild {
* .then(channel => console.log(`Created new channel ${channel}`))
* .catch(console.error);
*/
createChannel(name, type, { overwrites, reason }) {
createChannel(name, type, { overwrites, reason } = {}) {
if (overwrites instanceof Collection) overwrites = overwrites.array();
return this.client.api.guilds(this.id).channels.post({
data: {
Expand Down Expand Up @@ -907,7 +907,7 @@ class Guild {
* .then(role => console.log(`Created role ${role}`))
* .catch(console.error)
*/
createRole({ data = {}, reason }) {
createRole({ data = {}, reason } = {}) {
if (data.color) data.color = this.client.resolver.resolveColor(data.color);
if (data.permissions) data.permissions = Permissions.resolve(data.permissions);

Expand Down
7 changes: 5 additions & 2 deletions src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,13 @@ class GuildMember {
data.channel = null;
}
if (data.roles) data.roles = data.roles.map(role => role instanceof Role ? role.id : role);
let endpoint = this.client.api.guilds(this.guild.id).members(this.id);
let endpoint = this.client.api.guilds(this.guild.id);
if (this.user.id === this.client.user.id) {
const keys = Object.keys(data);
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.nickname;
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.members('@me').nick;
else endpoint = endpoint.members(this.id);
} else {
endpoint = endpoint.members(this.id);
}
return endpoint.patch({ data, reason }).then(newData => this.guild._updateMember(this, newData).mem);
}
Expand Down
4 changes: 2 additions & 2 deletions src/structures/Invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Invite {
* @readonly
*/
get url() {
return Constants.Endpoints.inviteLink(this.code);
return Constants.Endpoints.invite(this.code);
}

/**
Expand All @@ -154,7 +154,7 @@ class Invite {
* @returns {Promise<Invite>}
*/
delete(reason) {
return this.client.api.invites(this.code).delete({ reason }).then(() => this);
return this.client.api.invite(this.code).delete({ reason }).then(() => this);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ class Message {
emoji = this.client.resolver.resolveEmojiIdentifier(emoji);
if (!emoji) throw new TypeError('Emoji must be a string or Emoji/ReactionEmoji');

return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji)
.users('@me')
return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji)['@me']
.put()
.then(() => this._addReaction(Util.parseEmoji(emoji), this.client.user));
}
Expand All @@ -457,7 +456,7 @@ class Message {
* .then(msg => console.log(`Deleted message from ${msg.author}`))
* .catch(console.error);
*/
delete({ timeout = 0, reason }) {
delete({ timeout = 0, reason } = {}) {
if (timeout <= 0) {
return this.client.api.channels(this.channel.id).messages(this.id)
.delete({ reason })
Expand Down Expand Up @@ -502,7 +501,7 @@ class Message {
* @returns {Promise<Message>}
*/
acknowledge() {
return this.client.api.channels(this.channel.id).messages(this.message.id).ack
return this.client.api.channels(this.channel.id).messages(this.id).ack
.post({ data: { token: this.client.rest._ackToken } })
.then(res => {
if (res.token) this.client.rest._ackToken = res.token;
Expand Down
5 changes: 2 additions & 3 deletions src/structures/MessageReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ class MessageReaction {
remove(user = this.message.client.user) {
const userID = this.message.client.resolver.resolveUserID(user);
if (!userID) return Promise.reject(new Error('Couldn\'t resolve the user ID to remove from the reaction.'));
return this.client.api.channels(this.message.channel.id).messages(this.message.id)
.reactions(this.emoji.identifier)
.users(userID === this.client.user.id ? '@me' : userID)
return this.message.client.api.channels(this.message.channel.id).messages(this.message.id)
.reactions(this.emoji.identifier)[userID === this.message.client.user.id ? '@me' : userID]
.delete()
.then(() =>
this.message.client.actions.MessageReactionRemove.handle({
Expand Down
4 changes: 2 additions & 2 deletions src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Role {
edit(data, reason) {
if (data.permissions) data.permissions = Permissions.resolve(data.permissions);
else data.permissions = this.permissions;
return this.client.api.guild(this.guild.id).roles(this.id).patch({
return this.client.api.guilds(this.guild.id).roles(this.id).patch({
data: {
name: data.name || this.name,
position: typeof data.position !== 'undefined' ? data.position : this.position,
Expand Down Expand Up @@ -310,7 +310,7 @@ class Role {
* .catch(console.error);
*/
delete(reason) {
return this.client.api.guilds(this.guild.id).roles(this.role.id).delete({ reason })
return this.client.api.guilds(this.guild.id).roles(this.id).delete({ reason })
.then(() =>
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id }).role
);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/TextChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class TextChannel extends GuildChannel {
name, avatar,
} }).then(data => new Webhook(this.client, data));
} else {
return this.client.resolver.resolveBuffer(avatar).then(data => this.createWebhook(name, data));
return this.client.resolver.resolveBuffer(avatar).then(data =>
this.createWebhook(name, this.client.resolver.resolveBase64(data) || null));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class User {
size = format;
format = 'default';
}
return Constants.Endpoints.User(this).Avatar(this.client.options.http.cdn, this.avatar, format, size);
return Constants.Endpoints.CDN(this.client.options.http.cdn).Avatar(this.id, this.avatar, format, size);
}

/**
Expand Down Expand Up @@ -213,10 +213,9 @@ class User {
*/
deleteDM() {
if (!this.dmChannel) return Promise.reject(new Error('No DM Channel exists!'));
return this.client.api.channels(this.dmChannel.id).delete().then(data => {
data.id = this.dmChannel.id;
return this.client.actions.ChannelDelete.handle(data).channel;
});
return this.client.api.channels(this.dmChannel.id).delete().then(data =>
this.client.actions.ChannelDelete.handle(data).channel
);
}

/**
Expand All @@ -235,7 +234,8 @@ class User {
* @returns {Promise<User>}
*/
setNote(note) {
return this.client.api.users(this.id).note.put({ data: { note } }).then(() => this);
return this.client.api.users('@me').notes(this.id).put({ data: { note } })
.then(() => this);
}

/**
Expand Down
35 changes: 23 additions & 12 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ class Webhook {
options = {};
}

if (!options.username) options.username = this.name;

if (options.avatarURL) {
options.avatar_url = options.avatarURL;
options.avatarURL = null;
}

if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
if (content) {
if (options.disableEveryone ||
(typeof options.disableEveryone === 'undefined' && this.client.options.disableEveryone)
) {
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
}
}
options.content = content;

if (options.file) {
if (options.files) options.files.push(options.file);
else options.files = [options.file];
Expand All @@ -132,18 +149,12 @@ class Webhook {
file.file = buffer;
return file;
})
)).then(files => this.client.rest.methods.sendWebhookMessage(this, content, options, files));
}

if (!options.username) options.username = this.name;

if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
if (content) {
if (options.disableEveryone ||
(typeof options.disableEveryone === 'undefined' && this.client.options.disableEveryone)
) {
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
}
)).then(files => this.client.api.webhooks(this.id, this.token).post({
data: options,
query: { wait: true },
files,
auth: false,
}));
}

return this.client.api.webhooks(this.id, this.token).post({
Expand Down
16 changes: 8 additions & 8 deletions src/structures/shared/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = function search(target, options) {
options.minID = long.fromNumber(t).shiftLeft(22).toString();
options.maxID = long.fromNumber(t + 86400000).shiftLeft(22).toString();
}
if (options.channel) options.channel = this.client.resolver.resolveChannelID(options.channel);
if (options.author) options.author = this.client.resolver.resolveUserID(options.author);
if (options.mentions) options.mentions = this.client.resolver.resolveUserID(options.options.mentions);
if (options.channel) options.channel = target.client.resolver.resolveChannelID(options.channel);
if (options.author) options.author = target.client.resolver.resolveUserID(options.author);
if (options.mentions) options.mentions = target.client.resolver.resolveUserID(options.options.mentions);
options = {
content: options.content,
max_id: options.maxID,
Expand All @@ -42,18 +42,18 @@ module.exports = function search(target, options) {
};

// Lazy load these because some of them use util
const Channel = require('../structures/Channel');
const Guild = require('../structures/Guild');
const Message = require('../structures/Message');
const Channel = require('../Channel');
const Guild = require('../Guild');
const Message = require('../Message');

if (!(target instanceof Channel || target instanceof Guild)) {
throw new TypeError('Target must be a TextChannel, DMChannel, GroupDMChannel, or Guild.');
}

let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages.search;
let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search;
return endpoint.get({ query: options }).then(body => {
const messages = body.messages.map(x =>
x.map(m => new Message(this.target.client.channels.get(m.channel_id), m, this.target.client))
x.map(m => new Message(target.client.channels.get(m.channel_id), m, target.client))
);
return {
totalResults: body.total_results,
Expand Down

0 comments on commit 86bd061

Please sign in to comment.