refactor: errors (#8068)

Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com>
Co-authored-by: A. Román <kyradiscord@gmail.com>
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
DD
2022-06-20 15:46:35 +03:00
committed by GitHub
parent 358c3f4a46
commit e68effa822
62 changed files with 923 additions and 426 deletions

View File

@@ -18,7 +18,7 @@ const Mentions = require('./MessageMentions');
const MessagePayload = require('./MessagePayload');
const ReactionCollector = require('./ReactionCollector');
const { Sticker } = require('./Sticker');
const { Error } = require('../errors');
const { Error, ErrorCodes } = require('../errors');
const ReactionManager = require('../managers/ReactionManager');
const { createComponent } = require('../util/Components');
const { NonSystemMessageTypes } = require('../util/Constants');
@@ -541,7 +541,7 @@ class Message extends Base {
collector.once('end', (interactions, reason) => {
const interaction = interactions.first();
if (interaction) resolve(interaction);
else reject(new Error('INTERACTION_COLLECTOR_ERROR', reason));
else reject(new Error(ErrorCodes.InteractionCollectorError, reason));
});
});
}
@@ -607,10 +607,10 @@ class Message extends Base {
* @returns {Promise<Message>}
*/
async fetchReference() {
if (!this.reference) throw new Error('MESSAGE_REFERENCE_MISSING');
if (!this.reference) throw new Error(ErrorCodes.MessageReferenceMissing);
const { channelId, messageId } = this.reference;
const channel = this.client.channels.resolve(channelId);
if (!channel) throw new Error('GUILD_CHANNEL_RESOLVE');
if (!channel) throw new Error(ErrorCodes.GuildChannelResolve);
const message = await channel.messages.fetch(messageId);
return message;
}
@@ -661,7 +661,7 @@ class Message extends Base {
* .catch(console.error);
*/
edit(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!this.channel) return Promise.reject(new Error(ErrorCodes.ChannelNotCached));
return this.channel.messages.edit(this, options);
}
@@ -677,7 +677,7 @@ class Message extends Base {
* }
*/
crosspost() {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!this.channel) return Promise.reject(new Error(ErrorCodes.ChannelNotCached));
return this.channel.messages.crosspost(this.id);
}
@@ -692,7 +692,7 @@ class Message extends Base {
* .catch(console.error)
*/
async pin(reason) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
if (!this.channel) throw new Error(ErrorCodes.ChannelNotCached);
await this.channel.messages.pin(this.id, reason);
return this;
}
@@ -708,7 +708,7 @@ class Message extends Base {
* .catch(console.error)
*/
async unpin(reason) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
if (!this.channel) throw new Error(ErrorCodes.ChannelNotCached);
await this.channel.messages.unpin(this.id, reason);
return this;
}
@@ -729,7 +729,7 @@ class Message extends Base {
* .catch(console.error);
*/
async react(emoji) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
if (!this.channel) throw new Error(ErrorCodes.ChannelNotCached);
await this.channel.messages.react(this.id, emoji);
return this.client.actions.MessageReactionAdd.handle(
@@ -753,7 +753,7 @@ class Message extends Base {
* .catch(console.error);
*/
async delete() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
if (!this.channel) throw new Error(ErrorCodes.ChannelNotCached);
await this.channel.messages.delete(this.id);
return this;
}
@@ -777,7 +777,7 @@ class Message extends Base {
* .catch(console.error);
*/
reply(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!this.channel) return Promise.reject(new Error(ErrorCodes.ChannelNotCached));
let data;
if (options instanceof MessagePayload) {
@@ -820,11 +820,11 @@ class Message extends Base {
* @returns {Promise<ThreadChannel>}
*/
startThread(options = {}) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!this.channel) return Promise.reject(new Error(ErrorCodes.ChannelNotCached));
if (![ChannelType.GuildText, ChannelType.GuildNews].includes(this.channel.type)) {
return Promise.reject(new Error('MESSAGE_THREAD_PARENT'));
return Promise.reject(new Error(ErrorCodes.MessageThreadParent));
}
if (this.hasThread) return Promise.reject(new Error('MESSAGE_EXISTING_THREAD'));
if (this.hasThread) return Promise.reject(new Error(ErrorCodes.MessageExistingThread));
return this.channel.threads.create({ ...options, startMessage: this });
}
@@ -834,7 +834,7 @@ class Message extends Base {
* @returns {Promise<Message>}
*/
fetch(force = true) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!this.channel) return Promise.reject(new Error(ErrorCodes.ChannelNotCached));
return this.channel.messages.fetch({ message: this.id, force });
}
@@ -843,8 +843,8 @@ class Message extends Base {
* @returns {Promise<?Webhook>}
*/
fetchWebhook() {
if (!this.webhookId) return Promise.reject(new Error('WEBHOOK_MESSAGE'));
if (this.webhookId === this.applicationId) return Promise.reject(new Error('WEBHOOK_APPLICATION'));
if (!this.webhookId) return Promise.reject(new Error(ErrorCodes.WebhookMessage));
if (this.webhookId === this.applicationId) return Promise.reject(new Error(ErrorCodes.WebhookApplication));
return this.client.fetchWebhook(this.webhookId);
}