Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/managers/MessageManager.js
Expand Up @@ -28,7 +28,7 @@ class MessageManager extends CachedManager {
*/

_add(data, cache) {
return super._add(data, cache, { extras: [this.channel] });
return super._add(data, cache);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/managers/ReactionManager.js
Expand Up @@ -58,7 +58,7 @@ class ReactionManager extends CachedManager {
* @returns {Promise<Message>}
*/
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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/managers/ReactionUserManager.js
Expand Up @@ -40,7 +40,7 @@ class ReactionUserManager extends CachedManager {
*/
async fetch({ limit = 100, after } = {}) {
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
].get({ query: { limit, after } });
const users = new Collection();
Expand All @@ -61,7 +61,7 @@ class ReactionUserManager extends CachedManager {
const userId = this.client.users.resolveId(user);
if (!userId) throw new Error('REACTION_RESOLVE_USER');
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
].delete();
return this.reaction;
Expand Down
32 changes: 23 additions & 9 deletions src/structures/Message.js
Expand Up @@ -27,16 +27,21 @@ class Message extends Base {
/**
* @param {Client} client The instantiating client
* @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);

/**
* The channel that the message was sent in
* @type {TextBasedChannels}
* The id of the channel the message was sent in
* @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
Expand Down Expand Up @@ -299,7 +304,7 @@ class Message extends Base {
}

if (data.referenced_message) {
this.channel.messages._add(data.referenced_message);
this.channel?.messages._add(data.referenced_message);
}

/**
Expand Down Expand Up @@ -333,6 +338,15 @@ class Message extends Base {
return clone;
}

/**
* The channel that the message was sent in
* @type {TextChannel|DMChannel|NewsChannel|ThreadChannel}
iCrawl marked this conversation as resolved.
Show resolved Hide resolved
* @readonly
*/
get channel() {
return this.client.channels.resolve(this.channelId);
}

/**
* Whether or not this message is a partial
* @type {boolean}
Expand Down Expand Up @@ -376,7 +390,7 @@ class Message extends Base {
* @readonly
*/
get guild() {
return this.channel.guild ?? null;
return this.client.guilds.resolve(this.guildId);
}

/**
Expand All @@ -396,7 +410,7 @@ class Message extends Base {
* @readonly
*/
get thread() {
return this.channel.threads.resolve(this.id);
return this.channel?.threads.resolve(this.id) ?? null;
almostSouji marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -405,7 +419,7 @@ class Message extends Base {
* @readonly
*/
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}`;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/structures/MessageCollector.js
Expand Up @@ -70,7 +70,7 @@ class MessageCollector extends Collector {
* @event MessageCollector#collect
* @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++;
return message.id;
}
Expand All @@ -86,7 +86,7 @@ class MessageCollector extends Collector {
* @event MessageCollector#dispose
* @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;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/structures/MessageMentions.js
Expand Up @@ -69,8 +69,9 @@ class MessageMentions {
this.roles = new Collection(roles);
} else {
this.roles = new Collection();
const guild = message.guild;
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);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/structures/MessagePayload.js
Expand Up @@ -167,9 +167,8 @@ class MessagePayload {

let message_reference;
if (typeof this.options.reply === 'object') {
const message_id = this.isMessage
? this.target.channel.messages.resolveId(this.options.reply.messageReference)
: this.target.messages.resolveId(this.options.reply.messageReference);
const reference = this.options.reply.messageReference;
const message_id = this.isMessage ? reference.id ?? reference : this.target.messages.resolveId(reference);
almostSouji marked this conversation as resolved.
Show resolved Hide resolved
if (message_id) {
message_reference = {
message_id,
Expand Down
2 changes: 1 addition & 1 deletion src/structures/MessageReaction.js
Expand Up @@ -63,7 +63,7 @@ class MessageReaction {
*/
async remove() {
await this.client.api
.channels(this.message.channel.id)
.channels(this.message.channelId)
.messages(this.message.id)
.reactions(this._emoji.identifier)
.delete();
Expand Down
2 changes: 1 addition & 1 deletion src/structures/ReactionCollector.js
Expand Up @@ -176,7 +176,7 @@ class ReactionCollector extends Collector {
* @returns {void}
*/
_handleChannelDeletion(channel) {
if (channel.id === this.message.channel.id) {
if (channel.id === this.message.channelId) {
this.stop('channelDelete');
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/random.js
Expand Up @@ -234,12 +234,12 @@ client.on('messageCreate', msg => {
});

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}`);
});

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}`);
});

Expand Down
6 changes: 4 additions & 2 deletions typings/index.d.ts
Expand Up @@ -1048,7 +1048,7 @@ export class LimitedCollection<K, V> extends Collection<K, V> {
}

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