fix(Message): make #channel and #guild getters (#6271)

This commit is contained in:
Souji
2021-08-04 22:47:32 +02:00
committed by GitHub
parent 5b0621fb3a
commit 6e3236ab64
11 changed files with 41 additions and 25 deletions

View File

@@ -28,7 +28,7 @@ class MessageManager extends CachedManager {
*/ */
_add(data, cache) { _add(data, cache) {
return super._add(data, cache, { extras: [this.channel] }); return super._add(data, cache);
} }
/** /**

View File

@@ -58,7 +58,7 @@ class ReactionManager extends CachedManager {
* @returns {Promise<Message>} * @returns {Promise<Message>}
*/ */
async removeAll() { async removeAll() {
await this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete(); await this.client.api.channels(this.message.channelId).messages(this.message.id).reactions.delete();
return this.message; return this.message;
} }
} }

View File

@@ -40,7 +40,7 @@ class ReactionUserManager extends CachedManager {
*/ */
async fetch({ limit = 100, after } = {}) { async fetch({ limit = 100, after } = {}) {
const message = this.reaction.message; const message = this.reaction.message;
const data = await this.client.api.channels[message.channel.id].messages[message.id].reactions[ const data = await this.client.api.channels[message.channelId].messages[message.id].reactions[
this.reaction.emoji.identifier this.reaction.emoji.identifier
].get({ query: { limit, after } }); ].get({ query: { limit, after } });
const users = new Collection(); const users = new Collection();
@@ -61,7 +61,7 @@ class ReactionUserManager extends CachedManager {
const userId = this.client.users.resolveId(user); const userId = this.client.users.resolveId(user);
if (!userId) throw new Error('REACTION_RESOLVE_USER'); if (!userId) throw new Error('REACTION_RESOLVE_USER');
const message = this.reaction.message; const message = this.reaction.message;
await this.client.api.channels[message.channel.id].messages[message.id].reactions[this.reaction.emoji.identifier][ await this.client.api.channels[message.channelId].messages[message.id].reactions[this.reaction.emoji.identifier][
userId === this.client.user.id ? '@me' : userId userId === this.client.user.id ? '@me' : userId
].delete(); ].delete();
return this.reaction; return this.reaction;

View File

@@ -27,16 +27,21 @@ class Message extends Base {
/** /**
* @param {Client} client The instantiating client * @param {Client} client The instantiating client
* @param {APIMessage} data The data for the message * @param {APIMessage} data The data for the message
* @param {TextBasedChannels} channel The channel the message was sent in
*/ */
constructor(client, data, channel) { constructor(client, data) {
super(client); super(client);
/** /**
* The channel that the message was sent in * The id of the channel the message was sent in
* @type {TextBasedChannels} * @type {Snowflake}
*/ */
this.channel = channel; this.channelId = data.channel_id;
/**
* The id of the guild the message was sent in, if any
* @type {?Snowflake}
*/
this.guildId = data.guild_id ?? null;
/** /**
* Whether this message has been deleted * Whether this message has been deleted
@@ -299,7 +304,7 @@ class Message extends Base {
} }
if (data.referenced_message) { if (data.referenced_message) {
this.channel.messages._add(data.referenced_message); this.channel?.messages._add(data.referenced_message);
} }
/** /**
@@ -333,6 +338,15 @@ class Message extends Base {
return clone; return clone;
} }
/**
* The channel that the message was sent in
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}
* @readonly
*/
get channel() {
return this.client.channels.resolve(this.channelId);
}
/** /**
* Whether or not this message is a partial * Whether or not this message is a partial
* @type {boolean} * @type {boolean}
@@ -376,7 +390,7 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get guild() { get guild() {
return this.channel.guild ?? null; return this.client.guilds.resolve(this.guildId);
} }
/** /**
@@ -396,7 +410,7 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get thread() { get thread() {
return this.channel.threads.resolve(this.id); return this.channel?.threads.resolve(this.id) ?? null;
} }
/** /**
@@ -405,7 +419,7 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get url() { get url() {
return `https://discord.com/channels/${this.guild ? this.guild.id : '@me'}/${this.channel.id}/${this.id}`; return `https://discord.com/channels/${this.guildId ?? '@me'}/${this.channelId}/${this.id}`;
} }
/** /**

View File

@@ -70,7 +70,7 @@ class MessageCollector extends Collector {
* @event MessageCollector#collect * @event MessageCollector#collect
* @param {Message} message The message that was collected * @param {Message} message The message that was collected
*/ */
if (message.channel.id !== this.channel.id) return null; if (message.channelId !== this.channel.id) return null;
this.received++; this.received++;
return message.id; return message.id;
} }
@@ -86,7 +86,7 @@ class MessageCollector extends Collector {
* @event MessageCollector#dispose * @event MessageCollector#dispose
* @param {Message} message The message that was disposed of * @param {Message} message The message that was disposed of
*/ */
return message.channel.id === this.channel.id ? message.id : null; return message.channelId === this.channel.id ? message.id : null;
} }
/** /**

View File

@@ -69,8 +69,9 @@ class MessageMentions {
this.roles = new Collection(roles); this.roles = new Collection(roles);
} else { } else {
this.roles = new Collection(); this.roles = new Collection();
const guild = message.guild;
for (const mention of roles) { for (const mention of roles) {
const role = message.channel.guild.roles.cache.get(mention); const role = guild.roles.cache.get(mention);
if (role) this.roles.set(role.id, role); if (role) this.roles.set(role.id, role);
} }
} }

View File

@@ -167,9 +167,8 @@ class MessagePayload {
let message_reference; let message_reference;
if (typeof this.options.reply === 'object') { if (typeof this.options.reply === 'object') {
const message_id = this.isMessage const reference = this.options.reply.messageReference;
? this.target.channel.messages.resolveId(this.options.reply.messageReference) const message_id = this.isMessage ? reference.id ?? reference : this.target.messages.resolveId(reference);
: this.target.messages.resolveId(this.options.reply.messageReference);
if (message_id) { if (message_id) {
message_reference = { message_reference = {
message_id, message_id,

View File

@@ -63,7 +63,7 @@ class MessageReaction {
*/ */
async remove() { async remove() {
await this.client.api await this.client.api
.channels(this.message.channel.id) .channels(this.message.channelId)
.messages(this.message.id) .messages(this.message.id)
.reactions(this._emoji.identifier) .reactions(this._emoji.identifier)
.delete(); .delete();

View File

@@ -176,7 +176,7 @@ class ReactionCollector extends Collector {
* @returns {void} * @returns {void}
*/ */
_handleChannelDeletion(channel) { _handleChannelDeletion(channel) {
if (channel.id === this.message.channel.id) { if (channel.id === this.message.channelId) {
this.stop('channelDelete'); this.stop('channelDelete');
} }
} }

View File

@@ -234,12 +234,12 @@ client.on('messageCreate', msg => {
}); });
client.on('messageReactionAdd', (reaction, user) => { client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channel.id !== '222086648706498562') return; if (reaction.message.channelId !== '222086648706498562') return;
reaction.message.channel.send(`${user.username} added reaction ${reaction.emoji}, count is now ${reaction.count}`); reaction.message.channel.send(`${user.username} added reaction ${reaction.emoji}, count is now ${reaction.count}`);
}); });
client.on('messageReactionRemove', (reaction, user) => { client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.channel.id !== '222086648706498562') return; if (reaction.message.channelId !== '222086648706498562') return;
reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`); reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`);
}); });

6
typings/index.d.ts vendored
View File

@@ -1048,7 +1048,7 @@ export class LimitedCollection<K, V> extends Collection<K, V> {
} }
export class Message extends Base { export class Message extends Base {
public constructor(client: Client, data: RawMessageData, channel: TextBasedChannels); public constructor(client: Client, data: RawMessageData);
private _patch(data: RawPartialMessageData, partial: true): Message; private _patch(data: RawPartialMessageData, partial: true): Message;
private _patch(data: RawMessageData, partial?: boolean): Message; private _patch(data: RawMessageData, partial?: boolean): Message;
private _update(data: RawPartialMessageData, partial: true): Message; private _update(data: RawPartialMessageData, partial: true): Message;
@@ -1058,7 +1058,8 @@ export class Message extends Base {
public applicationId: Snowflake | null; public applicationId: Snowflake | null;
public attachments: Collection<Snowflake, MessageAttachment>; public attachments: Collection<Snowflake, MessageAttachment>;
public author: User; public author: User;
public channel: TextBasedChannels; public readonly channel: TextBasedChannels;
public channelId: Snowflake;
public readonly cleanContent: string; public readonly cleanContent: string;
public components: MessageActionRow[]; public components: MessageActionRow[];
public content: string; public content: string;
@@ -1072,6 +1073,7 @@ export class Message extends Base {
public editedTimestamp: number | null; public editedTimestamp: number | null;
public embeds: MessageEmbed[]; public embeds: MessageEmbed[];
public groupActivityApplication: ClientApplication | null; public groupActivityApplication: ClientApplication | null;
public guildId: Snowflake | null;
public readonly guild: Guild | null; public readonly guild: Guild | null;
public readonly hasThread: boolean; public readonly hasThread: boolean;
public id: Snowflake; public id: Snowflake;