Moved all error strings to src/errors/messages and a few other things (#1695)

* Added missing error messages
As well as `Guild#setRolePosition` and `Guild#setChannelPosition`'s first arg validation
And fixed a typo in `Guild#setChannelPosition`
`roles` -> `channels`

* Reverted collection and Util constructors

* Removed leftover messages
Should have been in the second commit.

* It's a single invalid permission and removed unused flag error

* Fix INVALID_TOKEN -> TOKEN_INVALID as of #1703
This commit is contained in:
SpaceEEC
2017-07-21 02:27:19 +02:00
committed by Crawl
parent 7a27b12b2b
commit 11556c0b3b
11 changed files with 49 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
const Constants = require('../util/Constants');
const Util = require('../util/Util');
const { Error } = require('../errors');
/**
* A wrapper around the ClientUser's settings.
@@ -54,7 +55,7 @@ class ClientUserSettings {
*/
addRestrictedGuild(guild) {
const temp = Object.assign([], this.restrictedGuilds);
if (temp.includes(guild.id)) return Promise.reject(new Error('Guild is already restricted'));
if (temp.includes(guild.id)) return Promise.reject(new Error('GUILD_RESTRICTED', true));
temp.push(guild.id);
return this.update('restricted_guilds', temp).then(() => guild);
}
@@ -67,7 +68,7 @@ class ClientUserSettings {
removeRestrictedGuild(guild) {
const temp = Object.assign([], this.restrictedGuilds);
const index = temp.indexOf(guild.id);
if (index < 0) return Promise.reject(new Error('Guild is not restricted'));
if (index < 0) return Promise.reject(new Error('GUILD_RESTRICTED'));
temp.splice(index, 1);
return this.update('restricted_guilds', temp).then(() => guild);
}

View File

@@ -6,6 +6,7 @@ const Invite = require('./Invite');
const GuildAuditLogs = require('./GuildAuditLogs');
const Webhook = require('./Webhook');
const { Presence } = require('./Presence');
const GuildChannel = require('./GuildChannel');
const GuildMember = require('./GuildMember');
const VoiceRegion = require('./VoiceRegion');
const Constants = require('../util/Constants');
@@ -487,7 +488,7 @@ class Guild {
*/
fetchMember(user, cache = true) {
user = this.client.resolver.resolveUser(user);
if (!user) return Promise.reject(new Error('User is not cached. Use Client.fetchUser first.'));
if (!user) return Promise.reject(new Error('USER_NOT_CACHED'));
if (this.members.has(user.id)) return Promise.resolve(this.members.get(user.id));
return this.client.api.guilds[this.id].members[user.id].get()
.then(data => {
@@ -532,7 +533,7 @@ class Guild {
this.client.on(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
this.client.setTimeout(() => {
this.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
reject(new Error('Members didn\'t arrive in time.'));
reject(new Error('GUILD_MEMBERS_TIMEOUT'));
}, 120e3);
});
}
@@ -730,7 +731,7 @@ class Guild {
*/
setPosition(position, relative) {
if (this.client.user.bot) {
return Promise.reject(new Error('Setting guild position is only available for user accounts'));
return Promise.reject(new Error('FEATURE_USER_ONLY'));
}
return this.client.user.settings.setGuildPosition(this, position, relative);
}
@@ -779,7 +780,7 @@ class Guild {
ban(user, options = { days: 0 }) {
if (options.days) options['delete-message-days'] = options.days;
const id = this.client.resolver.resolveUserID(user);
if (!id) return Promise.reject(new Error('Couldn\'t resolve the user ID to ban.'));
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID', true));
return this.client.api.guilds[this.id].bans[id].put({ query: options })
.then(() => {
if (user instanceof GuildMember) return user;
@@ -996,7 +997,7 @@ class Guild {
* .catch(console.error);
*/
leave() {
if (this.ownerID === this.client.user.id) return Promise.reject(new Error('Guild is owned by the client.'));
if (this.ownerID === this.client.user.id) return Promise.reject(new Error('GUILD_OWNED'));
return this.client.api.users['@me'].guilds[this.id].delete()
.then(() => this.client.actions.GuildDelete.handle({ id: this.id }).guild);
}
@@ -1158,11 +1159,11 @@ class Guild {
setRolePosition(role, position, relative = false) {
if (typeof role === 'string') {
role = this.roles.get(role);
if (!role) return Promise.reject(new Error('Supplied role is not a role or snowflake.'));
}
if (!(role instanceof Role)) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
position = Number(position);
if (isNaN(position)) return Promise.reject(new Error('Supplied position is not a number.'));
if (isNaN(position)) return Promise.reject(new TypeError('INVALID_TYPE', 'position', 'number'));
let updatedRoles = this._sortedRoles.array();
@@ -1188,11 +1189,13 @@ class Guild {
setChannelPosition(channel, position, relative = false) {
if (typeof channel === 'string') {
channel = this.channels.get(channel);
if (!channel) return Promise.reject(new Error('Supplied channel is not a channel or snowflake.'));
}
if (!(channel instanceof GuildChannel)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'channel', 'GuildChannel nor a Snowflake'));
}
position = Number(position);
if (isNaN(position)) return Promise.reject(new Error('Supplied position is not a number.'));
if (isNaN(position)) return Promise.reject(new TypeError('INVALID_TYPE', 'position', 'number'));
let updatedChannels = this._sortedChannels(channel.type).array();
@@ -1203,7 +1206,7 @@ class Guild {
.then(() =>
this.client.actions.GuildChannelsPositionUpdate.handle({
guild_id: this.id,
roles: updatedChannels,
channels: updatedChannels,
}).guild
);
}

View File

@@ -4,6 +4,7 @@ const Invite = require('./Invite');
const PermissionOverwrites = require('./PermissionOverwrites');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
const { TypeError } = require('../errors');
/**
* Represents a guild channel (i.e. text channels and voice channels).
@@ -163,7 +164,7 @@ class GuildChannel extends Channel {
} else {
userOrRole = this.client.resolver.resolveUser(userOrRole);
payload.type = 'member';
if (!userOrRole) return Promise.reject(new TypeError('Supplied parameter was neither a User nor a Role.'));
if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role', true));
}
payload.id = userOrRole.id;

View File

@@ -3,7 +3,7 @@ const Role = require('./Role');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
const { Presence } = require('./Presence');
const { Error } = require('../errors');
const { Error, TypeError } = require('../errors');
/**
* Represents a member of a guild on Discord.
@@ -396,7 +396,7 @@ class GuildMember {
*/
addRole(role) {
if (!(role instanceof Role)) role = this.guild.roles.get(role);
if (!role) return Promise.reject(new TypeError('Supplied parameter was neither a Role nor a Snowflake.'));
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
if (this._roles.includes(role.id)) return Promise.resolve(this);
return this.client.api.guilds[this.guild.id].members[this.user.id].roles[role.id]
.put()
@@ -426,7 +426,7 @@ class GuildMember {
*/
removeRole(role) {
if (!(role instanceof Role)) role = this.guild.roles.get(role);
if (!role) return Promise.reject(new TypeError('Supplied parameter was neither a Role nor a Snowflake.'));
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
return this.client.api.guilds[this.guild.id].members[this.user.id].roles[role.id]
.delete()
.then(() => this);

View File

@@ -7,7 +7,7 @@ const Util = require('../util/Util');
const Collection = require('../util/Collection');
const Constants = require('../util/Constants');
const Permissions = require('../util/Permissions');
const { TypeError } = require('../errors');
const { Error, TypeError } = require('../errors');
let GuildMember;
/**
@@ -519,7 +519,7 @@ class Message {
* @returns {Promise<?Webhook>}
*/
fetchWebhook() {
if (!this.webhookID) return Promise.reject(new Error('The message was not sent by a webhook.'));
if (!this.webhookID) return Promise.reject(new Error('WEBHOOK_MESSAGE'));
return this.client.fetchWebhook(this.webhookID);
}

View File

@@ -1,6 +1,7 @@
const Collection = require('../util/Collection');
const Emoji = require('./Emoji');
const ReactionEmoji = require('./ReactionEmoji');
const { Error } = require('../errors');
/**
* Represents a reaction to a message.
@@ -62,7 +63,7 @@ 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.'));
if (!userID) return Promise.reject(new Error('REACTION_RESOLVE_USER'));
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()

View File

@@ -3,6 +3,7 @@ const Constants = require('../util/Constants');
const { Presence } = require('./Presence');
const UserProfile = require('./UserProfile');
const Snowflake = require('../util/Snowflake');
const { Error } = require('../errors');
/**
* Represents a user on Discord.
@@ -212,7 +213,7 @@ class User {
* @returns {Promise<DMChannel>}
*/
deleteDM() {
if (!this.dmChannel) return Promise.reject(new Error('No DM Channel exists!'));
if (!this.dmChannel) return Promise.reject(new Error('USER_NO_DMCHANNEL'));
return this.client.api.channels[this.dmChannel.id].delete()
.then(data => this.client.actions.ChannelDelete.handle(data).channel);
}

View File

@@ -1,5 +1,6 @@
const GuildChannel = require('./GuildChannel');
const Collection = require('../util/Collection');
const { Error } = require('../errors');
/**
* Represents a guild voice channel on Discord.
@@ -113,7 +114,7 @@ class VoiceChannel extends GuildChannel {
* .catch(console.error);
*/
join() {
if (this.client.browser) return Promise.reject(new Error('Voice connections are not available in browsers.'));
if (this.client.browser) return Promise.reject(new Error('VOICE_NO_BROWSER'));
return this.client.voice.joinChannel(this);
}