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

feat(MessageManager): extend API coverage #4869

Merged
merged 32 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dfb2a20
feat(MessageManager): move Message#edit API calls to Manager
monbrey Sep 14, 2020
e9855f4
fix(MessageEditOptions): update method reference
monbrey Sep 14, 2020
909b7d7
feat(MessageManager): move Message#crosspost API calls to Manager
monbrey Sep 14, 2020
0afb292
feat(MessageManager): move Message#pin API calls to Manager
monbrey Sep 15, 2020
ffb94ca
feat(MessageManager): move Message#unpin API calls to Manager
monbrey Sep 15, 2020
dcd5246
feat(MessageManager): move Message#react API calls to Manager
monbrey Sep 16, 2020
ae2ffab
fix(MessageManager): throw TypeError on non-MessageResolvable params
monbrey Sep 16, 2020
85646ca
chore: typings
monbrey Sep 30, 2020
c1e831c
Merge remote-tracking branch 'upstream/master' into message-manager-a…
monbrey Sep 30, 2020
1e84854
fix(MessageManager): consistent API call notation
monbrey Sep 30, 2020
96570d9
chore: linting
monbrey Sep 30, 2020
fbf5f3c
fix: apply suggestions from code review
monbrey Oct 5, 2020
d712d29
fix: rename shadowed variable
monbrey Oct 5, 2020
4e49e04
feat: typings for APIRawMessage overloads
monbrey Oct 16, 2020
bb22ae1
fix(MessageManager): return Message instance from MessageManager#edit
monbrey Dec 15, 2020
78ce3b1
feat(MessageManager): return Message from MessageManager#crosspost
monbrey Dec 15, 2020
609b0e0
fix(Message): return result of Manager call directly on crosspost
monbrey Dec 15, 2020
149af15
fix(Typings): edit and crosspost no longer return raw api messages
monbrey Dec 15, 2020
f2f2182
Merge branch 'master' into message-manager-additions
monbrey Dec 28, 2020
77d3019
fix: linting
monbrey Dec 28, 2020
8436588
fix: tslint
monbrey Dec 28, 2020
64b77d7
feat: return Message from MessageManager#edit
monbrey Feb 4, 2021
7bf12fb
fix: suggestions
monbrey Feb 4, 2021
6793fe5
fix: remove unused interface
monbrey Feb 14, 2021
0d49034
fix(Typings): convert MessageManager#edit to overloads
monbrey Apr 30, 2021
667895d
fix(Message): use async/await
monbrey Apr 30, 2021
7f41309
Merge branch 'master' into message-manager-additions
monbrey Apr 30, 2021
85b8fa6
fix: drop unsupported options.reason
monbrey May 2, 2021
09af53f
Merge branch 'message-manager-additions' of https://github.com/monbre…
monbrey May 2, 2021
c7cfd6e
fix(Message): remove unsupport options.reason from pin/unpin
monbrey May 2, 2021
6fc2592
fix: typings
monbrey May 2, 2021
3946c8b
chore: change MessageManager#edit to accept a single options object
monbrey May 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/managers/MessageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors');
const APIMessage = require('../structures/APIMessage');
const Message = require('../structures/Message');
const Collection = require('../util/Collection');
const LimitedCollection = require('../util/LimitedCollection');
Expand Down Expand Up @@ -113,6 +114,95 @@ class MessageManager extends BaseManager {
* @returns {?Snowflake}
*/

/**
* Edits a message, even if it's not cached.
* @param {MessageResolvable} message The message to edit
* @param {StringResolvable|APIMessage} [content] The new content for the message
* @param {MessageEditOptions|MessageEmbed} [options] The options to provide
* @returns {Promise<void>}
monbrey marked this conversation as resolved.
Show resolved Hide resolved
*/
async edit(message, content, options) {
monbrey marked this conversation as resolved.
Show resolved Hide resolved
message = this.resolveID(message);
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');
const { data } =
content instanceof APIMessage ? content.resolveData() : APIMessage.create(this, content, options).resolveData();
await this.client.api
monbrey marked this conversation as resolved.
Show resolved Hide resolved
.channels(this.channel.id)
.messages(message)
.patch({ data })
.then(d => {
if (this.cache.has(message)) {
const clone = this.cache.get(message)._clone();
clone._patch(d);
}
});
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Publishes a message in an announcement channel to all channels following it, even if it's not cached.
* @param {MessageResolvable} message The message to publish
* @returns {Promise<void>}
monbrey marked this conversation as resolved.
Show resolved Hide resolved
*/
async crosspost(message) {
message = this.resolveID(message);
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');

await this.client.api.channels(this.channel.id).messages(message).crosspost.post();
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Pins a message to the channel's pinned messages, even if it's not cached.
* @param {MessageResolvable} message The message to pin
* @param {Object} [options] Options for pinning
* @param {string} [options.reason] Reason for pinning
* @returns {Promise<void>}
*/
async pin(message, options) {
message = this.resolveID(message);
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');

await this.client.api
.channels(this.channel.id)
.pins(message)
.put(options)
.then(() => this);
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Unins a message from the channel's pinned messages, even if it's not cached.
* @param {MessageResolvable} message The message to unpin
* @param {Object} [options] Options for unpinning
* @param {string} [options.reason] Reason for unpinning
* @returns {Promise<void>}
*/
async unpin(message, options) {
message = this.resolveID(message);
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');

await this.client.api
.channels(this.channel.id)
.pins(message)
.delete(options)
.then(() => this);
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Adds a reaction to the message, even if it's not cached.
monbrey marked this conversation as resolved.
Show resolved Hide resolved
* @param {MessageResolvable} message The messag to react to
* @param {EmojiIdentifierResolvable} emoji The emoji to react with
* @returns {Promise<void>}
*/
async react(message, emoji) {
message = this.resolveID(message);
if (!message) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');

emoji = this.client.emojis.resolveIdentifier(emoji);
if (!emoji) throw new TypeError('EMOJI_TYPE', 'emoji', 'EmojiIdentifierResolvable');

// eslint-disable-next-line newline-per-chained-call
kyranet marked this conversation as resolved.
Show resolved Hide resolved
await this.client.api.channels(this.channel.id).messages(message).reactions(emoji, '@me').put();
}

/**
* Deletes a message, even if it's not cached.
* @param {MessageResolvable} message The message to delete
Expand Down
53 changes: 15 additions & 38 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class Message extends Base {
}

/**
* Options that can be passed into editMessage.
* Options that can be passed into Message#edit.
monbrey marked this conversation as resolved.
Show resolved Hide resolved
* @typedef {Object} MessageEditOptions
* @property {string} [content] Content to be edited
* @property {MessageEmbed|Object} [embed] An embed to be added/edited
Expand All @@ -439,13 +439,7 @@ class Message extends Base {
* .catch(console.error);
*/
edit(content, options) {
const { data } =
content instanceof APIMessage ? content.resolveData() : APIMessage.create(this, content, options).resolveData();
return this.client.api.channels[this.channel.id].messages[this.id].patch({ data }).then(d => {
const clone = this._clone();
clone._patch(d);
return clone;
});
return this.channel.messages.edit(this.id, content, options).then(() => this);
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -459,9 +453,8 @@ class Message extends Base {
* .catch(console.error);
* }
*/
async crosspost() {
await this.client.api.channels(this.channel.id).messages(this.id).crosspost.post();
return this;
crosspost() {
return this.channel.messages.crosspost(this.id).then(() => this);
}

/**
Expand All @@ -476,11 +469,7 @@ class Message extends Base {
* .catch(console.error)
*/
pin(options) {
return this.client.api
.channels(this.channel.id)
.pins(this.id)
.put(options)
.then(() => this);
return this.channel.messages.pin(this.id, options).then(() => this);
}

/**
Expand All @@ -495,11 +484,7 @@ class Message extends Base {
* .catch(console.error)
*/
unpin(options) {
return this.client.api
.channels(this.channel.id)
.pins(this.id)
.delete(options)
.then(() => this);
return this.channel.messages.unpin(this.id, options).then(() => this);
}

/**
Expand All @@ -518,23 +503,15 @@ class Message extends Base {
* .catch(console.error);
*/
react(emoji) {
emoji = this.client.emojis.resolveIdentifier(emoji);
if (!emoji) throw new TypeError('EMOJI_TYPE');

return this.client.api
.channels(this.channel.id)
.messages(this.id)
.reactions(emoji, '@me')
.put()
.then(
() =>
this.client.actions.MessageReactionAdd.handle({
user: this.client.user,
channel: this.channel,
message: this,
emoji: Util.parseEmoji(emoji),
}).reaction,
);
return this.channel.messages.react(this.id, emoji).then(
() =>
this.client.actions.MessageReactionAdd.handle({
user: this.client.user,
channel: this.channel,
message: this,
emoji: Util.parseEmoji(emoji),
}).reaction,
);
monbrey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
15 changes: 14 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1982,14 +1982,27 @@ declare module 'discord.js' {
constructor(channel: TextChannel | DMChannel, iterable?: Iterable<any>);
public channel: TextBasedChannelFields;
public cache: Collection<Snowflake, Message>;
public crosspost(message: MessageResolvable): Promise<void>;
monbrey marked this conversation as resolved.
Show resolved Hide resolved
public delete(message: MessageResolvable, reason?: string): Promise<void>;
public edit(
message: MessageResolvable,
content: APIMessageContentResolvable | MessageEditOptions | MessageEmbed | APIMessage,
): Promise<void>;
monbrey marked this conversation as resolved.
Show resolved Hide resolved
public edit(
message: MessageResolvable,
content: StringResolvable,
options: MessageEditOptions | MessageEmbed,
): Promise<void>;
monbrey marked this conversation as resolved.
Show resolved Hide resolved
public fetch(message: Snowflake, cache?: boolean, force?: boolean): Promise<Message>;
public fetch(
options?: ChannelLogsQueryOptions,
cache?: boolean,
force?: boolean,
): Promise<Collection<Snowflake, Message>>;
public fetchPinned(cache?: boolean): Promise<Collection<Snowflake, Message>>;
public delete(message: MessageResolvable, reason?: string): Promise<void>;
public react(message: MessageResolvable, emoji: EmojiIdentifierResolvable): Promise<void>;
public pin(message: MessageResolvable, options?: { reason?: string }): Promise<void>;
public unpin(message: MessageResolvable, options?: { reason?: string }): Promise<void>;
}

// Hacky workaround because changing the signature of an overridden method errors
Expand Down