Skip to content

Commit 4dfdcdc

Browse files
committed
feat(MessageManager)!: new pinned messages routes
1 parent 7e3d4e5 commit 4dfdcdc

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

packages/discord.js/src/managers/MessageManager.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,61 @@ class MessageManager extends CachedManager {
123123
return data.reduce((_data, message) => _data.set(message.id, this._add(message, options.cache)), new Collection());
124124
}
125125

126+
/**
127+
* Options used to fetch pinned messages.
128+
*
129+
* @typedef {Object} FetchPinnedMessagesOptions
130+
* @property {DateResolvable} [before] Consider only pinned messages before this time
131+
* @property {number} [limit] The maximum number of pinned messages to return
132+
* @property {boolean} [cache] Whether to cache the pinned fetched messages
133+
*/
134+
135+
/**
136+
* Data returned from fetching pinned messages.
137+
*
138+
* @typedef {FetchedThreads} FetchPinnedMessagesResponse
139+
* @property {MessagePin[]} items The pinned messages
140+
* @property {boolean} hasMore Whether there are additional pinned messages that require a subsequent call
141+
*/
142+
143+
/**
144+
* Pinned message data returned from fetching pinned messages.
145+
*
146+
* @typedef {Object} MessagePin
147+
* @property {Date} pinnedAt The time the message was pinned at
148+
* @property {number} pinnedTimestamp The timestamp the message was pinned at
149+
* @property {Message} message The pinned message
150+
*/
151+
126152
/**
127153
* Fetches the pinned messages of this channel and returns a collection of them.
128154
* <info>The returned Collection does not contain any reaction data of the messages.
129155
* Those need to be fetched separately.</info>
130156
*
131-
* @param {boolean} [cache=true] Whether to cache the message(s)
132-
* @returns {Promise<Collection<Snowflake, Message>>}
157+
* @param {FetchPinnedMessagesOptions} [options] Options for fetching pinned messages
158+
* @returns {Promise<FetchPinnedMessagesResponse>}
133159
* @example
134160
* // Get pinned messages
135-
* channel.messages.fetchPinned()
136-
* .then(messages => console.log(`Received ${messages.size} messages`))
161+
* channel.messages.fetchPins()
162+
* .then(messages => console.log(`Received ${messages.items.length} messages`))
137163
* .catch(console.error);
138164
*/
139-
async fetchPinned(cache = true) {
140-
const data = await this.client.rest.get(Routes.channelPins(this.channel.id));
141-
const messages = new Collection();
142-
for (const message of data) messages.set(message.id, this._add(message, cache));
143-
return messages;
165+
async fetchPins(options = {}) {
166+
const data = await this.client.rest.get(Routes.channelMessagesPins(this.channel.id), {
167+
query: makeURLSearchParams({
168+
...options,
169+
before: options.before && new Date(options.before).toISOString(),
170+
}),
171+
});
172+
173+
return {
174+
items: data.items.map(item => ({
175+
pinnedAt: new Date(item.pinned_at),
176+
pinnedTimestamp: Date.parse(item.pinned_at),
177+
message: this._add(item.message, options.cache),
178+
})),
179+
hasMore: data.has_more,
180+
};
144181
}
145182

146183
/**
@@ -221,7 +258,7 @@ class MessageManager extends CachedManager {
221258
const messageId = this.resolveId(message);
222259
if (!messageId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'message', 'MessageResolvable');
223260

224-
await this.client.rest.put(Routes.channelPin(this.channel.id, messageId), { reason });
261+
await this.client.rest.put(Routes.channelMessagesPin(this.channel.id, messageId), { reason });
225262
}
226263

227264
/**
@@ -235,7 +272,7 @@ class MessageManager extends CachedManager {
235272
const messageId = this.resolveId(message);
236273
if (!messageId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'message', 'MessageResolvable');
237274

238-
await this.client.rest.delete(Routes.channelPin(this.channel.id, messageId), { reason });
275+
await this.client.rest.delete(Routes.channelMessagesPin(this.channel.id, messageId), { reason });
239276
}
240277

241278
/**

packages/discord.js/typings/index.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4503,7 +4503,7 @@ export abstract class MessageManager<InGuild extends boolean = boolean> extends
45034503
): Promise<Message<InGuild>>;
45044504
public fetch(options: FetchMessageOptions | MessageResolvable): Promise<Message<InGuild>>;
45054505
public fetch(options?: FetchMessagesOptions): Promise<Collection<Snowflake, Message<InGuild>>>;
4506-
public fetchPinned(cache?: boolean): Promise<Collection<Snowflake, Message<InGuild>>>;
4506+
public fetchPins(options?: FetchPinnedMessagesOptions): Promise<FetchPinnedMessagesResponse<InGuild>>;
45074507
public react(message: MessageResolvable, emoji: EmojiIdentifierResolvable): Promise<void>;
45084508
public pin(message: MessageResolvable, reason?: string): Promise<void>;
45094509
public unpin(message: MessageResolvable, reason?: string): Promise<void>;
@@ -5787,6 +5787,22 @@ export interface FetchMessagesOptions {
57875787
limit?: number;
57885788
}
57895789

5790+
export interface FetchPinnedMessagesOptions {
5791+
before?: DateResolvable;
5792+
limit?: number;
5793+
}
5794+
5795+
export interface FetchPinnedMessagesResponse<InGuild extends boolean = boolean> {
5796+
hasMore: boolean;
5797+
items: readonly MessagePin<InGuild>[];
5798+
}
5799+
5800+
export interface MessagePin<InGuild extends boolean = boolean> {
5801+
message: Message<InGuild>;
5802+
pinnedAt: Date;
5803+
pinnedTimestamp: number;
5804+
}
5805+
57905806
export interface FetchReactionUsersOptions {
57915807
after?: Snowflake;
57925808
limit?: number;

packages/discord.js/typings/index.test-d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import type {
9999
Entitlement,
100100
FetchedThreads,
101101
FetchedThreadsMore,
102+
FetchPinnedMessagesResponse,
102103
FileComponentData,
103104
ForumChannel,
104105
Guild,
@@ -1756,7 +1757,7 @@ declare const guildChannelManager: GuildChannelManager;
17561757
expectType<Promise<Message<true>>>(messages.crosspost('1234567890'));
17571758
expectType<Promise<Message<true>>>(messages.edit('1234567890', 'text'));
17581759
expectType<Promise<Message<true>>>(messages.fetch('1234567890'));
1759-
expectType<Promise<Collection<Snowflake, Message<true>>>>(messages.fetchPinned());
1760+
expectType<Promise<FetchPinnedMessagesResponse<true>>>(messages.fetchPins());
17601761
expectType<Guild>(message.guild);
17611762
expectType<Snowflake>(message.guildId);
17621763
expectType<GuildTextBasedChannel>(message.channel.messages.channel);
@@ -1769,7 +1770,7 @@ declare const guildChannelManager: GuildChannelManager;
17691770
expectType<DMMessageManager>(messages);
17701771
expectType<Promise<Message>>(messages.edit('1234567890', 'text'));
17711772
expectType<Promise<Message>>(messages.fetch('1234567890'));
1772-
expectType<Promise<Collection<Snowflake, Message>>>(messages.fetchPinned());
1773+
expectType<Promise<FetchPinnedMessagesResponse>>(messages.fetchPins());
17731774
expectType<Guild | null>(message.guild);
17741775
expectType<Snowflake | null>(message.guildId);
17751776
expectType<DMChannel | GuildTextBasedChannel | PartialGroupDMChannel>(message.channel.messages.channel);

0 commit comments

Comments
 (0)