mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
Errors Standardization (#1246)
* errors and stuff * more errors * all the errors * fix build
This commit is contained in:
@@ -6,6 +6,7 @@ const Util = require('../util/Util');
|
||||
const Guild = require('./Guild');
|
||||
const Message = require('./Message');
|
||||
const GroupDMChannel = require('./GroupDMChannel');
|
||||
const { TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents the logged in client's Discord user.
|
||||
@@ -193,7 +194,7 @@ class ClientUser extends User {
|
||||
}
|
||||
|
||||
if (data.status) {
|
||||
if (typeof data.status !== 'string') throw new TypeError('Status must be a string');
|
||||
if (typeof data.status !== 'string') throw new TypeError('STATUS_TYPE');
|
||||
if (this.bot) {
|
||||
status = data.status;
|
||||
} else {
|
||||
|
||||
@@ -14,6 +14,7 @@ const Util = require('../util/Util');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Shared = require('./shared');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents a guild (or a server) on Discord.
|
||||
@@ -804,8 +805,7 @@ class Guild {
|
||||
*/
|
||||
unban(user, reason) {
|
||||
const id = this.client.resolver.resolveUserID(user);
|
||||
if (!id) throw new Error('Couldn\'t resolve the user ID to unban.');
|
||||
|
||||
if (!id) throw new Error('BAN_RESOLVE_ID');
|
||||
return this.client.api.guilds(this.id).bans(id).delete({ reason })
|
||||
.then(() => user);
|
||||
}
|
||||
@@ -828,7 +828,7 @@ class Guild {
|
||||
* .catch(console.error);
|
||||
*/
|
||||
pruneMembers({ days = 7, dry = false, reason } = {}) {
|
||||
if (typeof days !== 'number') throw new TypeError('Days must be a number.');
|
||||
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');
|
||||
return this.client.api.guilds(this.id).prune[dry ? 'get' : 'post']({ query: { days }, reason })
|
||||
.then(data => data.pruned);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ const Role = require('./Role');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Collection = require('../util/Collection');
|
||||
const Presence = require('./Presence').Presence;
|
||||
const { Error } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents a member of a guild on Discord.
|
||||
@@ -287,7 +288,7 @@ class GuildMember {
|
||||
*/
|
||||
permissionsIn(channel) {
|
||||
channel = this.client.resolver.resolveChannel(channel);
|
||||
if (!channel || !channel.guild) throw new Error('Could not resolve channel to a guild channel.');
|
||||
if (!channel || !channel.guild) throw new Error('GUILD_CHANNEL_RESOLVE');
|
||||
return channel.permissionsFor(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +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');
|
||||
let GuildMember;
|
||||
|
||||
/**
|
||||
@@ -428,7 +429,7 @@ class Message {
|
||||
*/
|
||||
react(emoji) {
|
||||
emoji = this.client.resolver.resolveEmojiIdentifier(emoji);
|
||||
if (!emoji) throw new TypeError('Emoji must be a string or Emoji/ReactionEmoji');
|
||||
if (!emoji) throw new TypeError('EMOJI_TYPE');
|
||||
|
||||
return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji)['@me']
|
||||
.put()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Util = require('../util/Util');
|
||||
const { RangeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents an embed in a message (image/video preview, rich embed, etc.)
|
||||
@@ -165,13 +166,11 @@ class MessageEmbed {
|
||||
* @returns {MessageEmbed} This embed
|
||||
*/
|
||||
addField(name, value, inline = false) {
|
||||
if (this.fields.length >= 25) throw new RangeError('MessageEmbeds may not exceed 25 fields.');
|
||||
if (this.fields.length >= 25) throw new RangeError('EMBED_FIELD_COUNT');
|
||||
name = Util.resolveString(name);
|
||||
if (name.length > 256) throw new RangeError('MessageEmbed field names may not exceed 256 characters.');
|
||||
if (!/\S/.test(name)) throw new RangeError('MessageEmbed field names may not be empty.');
|
||||
if (!String(name) || name.length > 256) throw new RangeError('EMBED_FIELD_NAME');
|
||||
value = Util.resolveString(value);
|
||||
if (value.length > 1024) throw new RangeError('MessageEmbed field values may not exceed 1024 characters.');
|
||||
if (!/\S/.test(value)) throw new RangeError('MessageEmbed field values may not be empty.');
|
||||
if (!String(name) || value.length > 1024) throw new RangeError('EMBED_FIELD_VALUE');
|
||||
this.fields.push({ name, value, inline });
|
||||
return this;
|
||||
}
|
||||
@@ -183,7 +182,7 @@ class MessageEmbed {
|
||||
* @returns {MessageEmbed} This embed
|
||||
*/
|
||||
attachFile(file) {
|
||||
if (this.file) throw new RangeError('You may not upload more than one file at once.');
|
||||
if (this.file) throw new RangeError('EMBED_FILE_LIMIT');
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
@@ -217,7 +216,7 @@ class MessageEmbed {
|
||||
*/
|
||||
setDescription(description) {
|
||||
description = Util.resolveString(description);
|
||||
if (description.length > 2048) throw new RangeError('MessageEmbed descriptions may not exceed 2048 characters.');
|
||||
if (description.length > 2048) throw new RangeError('EMBED_DESCRIPTION');
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
@@ -230,7 +229,7 @@ class MessageEmbed {
|
||||
*/
|
||||
setFooter(text, iconURL) {
|
||||
text = Util.resolveString(text);
|
||||
if (text.length > 2048) throw new RangeError('MessageEmbed footer text may not exceed 2048 characters.');
|
||||
if (text.length > 2048) throw new RangeError('EMBED_FOOTER_TEXT');
|
||||
this.footer = { text, iconURL };
|
||||
return this;
|
||||
}
|
||||
@@ -272,7 +271,7 @@ class MessageEmbed {
|
||||
*/
|
||||
setTitle(title) {
|
||||
title = Util.resolveString(title);
|
||||
if (title.length > 256) throw new RangeError('MessageEmbed titles may not exceed 256 characters.');
|
||||
if (title.length > 256) throw new RangeError('EMBED_TITLE');
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ const MessageCollector = require('../MessageCollector');
|
||||
const Shared = require('../shared');
|
||||
const Collection = require('../../util/Collection');
|
||||
const Snowflake = require('../../util/Snowflake');
|
||||
const { Error, RangeError, TypeError } = require('../../errors');
|
||||
|
||||
/**
|
||||
* Interface for classes that have text-channel-like features.
|
||||
@@ -136,7 +137,7 @@ class TextBasedChannel {
|
||||
return this.fetchMessages({ limit: 1, around: messageID })
|
||||
.then(messages => {
|
||||
const msg = messages.get(messageID);
|
||||
if (!msg) throw new Error('Message not found.');
|
||||
if (!msg) throw new Error('MESSAGE_MISSING');
|
||||
return msg;
|
||||
});
|
||||
}
|
||||
@@ -227,7 +228,7 @@ class TextBasedChannel {
|
||||
* channel.startTyping();
|
||||
*/
|
||||
startTyping(count) {
|
||||
if (typeof count !== 'undefined' && count < 1) throw new RangeError('Count must be at least 1.');
|
||||
if (typeof count !== 'undefined' && count < 1) throw new RangeError('TYPING_COUNT');
|
||||
if (!this.client.user._typing.has(this.id)) {
|
||||
const endpoint = this.client.api.channels(this.id).typing;
|
||||
this.client.user._typing.set(this.id, {
|
||||
@@ -361,7 +362,7 @@ class TextBasedChannel {
|
||||
}).messages
|
||||
);
|
||||
}
|
||||
throw new TypeError('The messages must be an Array, Collection, or number.');
|
||||
throw new TypeError('MESSAGE_BULK_DELETE_TYPE');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const long = require('long');
|
||||
const { TypeError } = require('../../errors');
|
||||
|
||||
/**
|
||||
* @typedef {Object} MessageSearchOptions
|
||||
@@ -84,9 +85,7 @@ module.exports = function search(target, options) {
|
||||
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.');
|
||||
}
|
||||
if (!(target instanceof Channel || target instanceof Guild)) throw new TypeError('SEARCH_CHANNEL_TYPE');
|
||||
|
||||
let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search;
|
||||
return endpoint.get({ query: options }).then(body => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Util = require('../../util/Util');
|
||||
const { RangeError } = require('../../errors');
|
||||
|
||||
module.exports = function sendMessage(channel, options) {
|
||||
const User = require('../User');
|
||||
@@ -8,7 +9,7 @@ module.exports = function sendMessage(channel, options) {
|
||||
|
||||
if (typeof nonce !== 'undefined') {
|
||||
nonce = parseInt(nonce);
|
||||
if (isNaN(nonce) || nonce < 0) throw new RangeError('Message nonce must fit in an unsigned 64-bit integer.');
|
||||
if (isNaN(nonce) || nonce < 0) throw new RangeError('MESSAGE_NONCE_TYPE');
|
||||
}
|
||||
|
||||
if (content) {
|
||||
|
||||
Reference in New Issue
Block a user