11'use strict' ;
22
3+ const process = require ( 'node:process' ) ;
34const { Collection } = require ( '@discordjs/collection' ) ;
45const { makeURLSearchParams } = require ( '@discordjs/rest' ) ;
56const { Routes } = require ( 'discord-api-types/v10' ) ;
@@ -10,6 +11,8 @@ const MessagePayload = require('../structures/MessagePayload');
1011const { MakeCacheOverrideSymbol } = require ( '../util/Symbols' ) ;
1112const { resolvePartialEmoji } = require ( '../util/Util' ) ;
1213
14+ let deprecationEmittedForFetchPinned = false ;
15+
1316/**
1417 * Manages API methods for Messages and holds their cache.
1518 * @extends {CachedManager }
@@ -116,19 +119,83 @@ class MessageManager extends CachedManager {
116119 return data . reduce ( ( _data , message ) => _data . set ( message . id , this . _add ( message , options . cache ) ) , new Collection ( ) ) ;
117120 }
118121
122+ /**
123+ * Options used to fetch pinned messages.
124+ *
125+ * @typedef {Object } FetchPinnedMessagesOptions
126+ * @property {DateResolvable } [before] Consider only pinned messages before this time
127+ * @property {number } [limit] The maximum number of pinned messages to return
128+ * @property {boolean } [cache] Whether to cache the pinned messages
129+ */
130+
131+ /**
132+ * Data returned from fetching pinned messages.
133+ *
134+ * @typedef {Object } FetchPinnedMessagesResponse
135+ * @property {MessagePin[] } items The pinned messages
136+ * @property {boolean } hasMore Whether there are additional pinned messages that require a subsequent call
137+ */
138+
139+ /**
140+ * Pinned message data returned from fetching pinned messages.
141+ *
142+ * @typedef {Object } MessagePin
143+ * @property {Date } pinnedAt The time the message was pinned at
144+ * @property {number } pinnedTimestamp The timestamp the message was pinned at
145+ * @property {Message } message The pinned message
146+ */
147+
119148 /**
120149 * Fetches the pinned messages of this channel and returns a collection of them.
121150 * <info>The returned Collection does not contain any reaction data of the messages.
122151 * Those need to be fetched separately.</info>
123- * @param {boolean } [cache=true] Whether to cache the message(s)
124- * @returns {Promise<Collection<Snowflake, Message>> }
152+ *
153+ * @param {FetchPinnedMessagesOptions } [options={}] Options for fetching pinned messages
154+ * @returns {Promise<FetchPinnedMessagesResponse> }
125155 * @example
126156 * // Get pinned messages
127- * channel.messages.fetchPinned ()
128- * .then(messages => console.log(`Received ${messages.size } messages`))
157+ * channel.messages.fetchPins ()
158+ * .then(messages => console.log(`Received ${messages.items.length } messages`))
129159 * .catch(console.error);
130160 */
161+ async fetchPins ( options = { } ) {
162+ const data = await this . client . rest . get ( Routes . channelMessagesPins ( this . channel . id ) , {
163+ query : makeURLSearchParams ( {
164+ ...options ,
165+ before : options . before && new Date ( options . before ) . toISOString ( ) ,
166+ } ) ,
167+ } ) ;
168+
169+ return {
170+ items : data . items . map ( item => ( {
171+ pinnedTimestamp : Date . parse ( item . pinned_at ) ,
172+ get pinnedAt ( ) {
173+ return new Date ( this . pinnedTimestamp ) ;
174+ } ,
175+ message : this . _add ( item . message , options . cache ) ,
176+ } ) ) ,
177+ hasMore : data . has_more ,
178+ } ;
179+ }
180+
181+ /**
182+ * Fetches the pinned messages of this channel and returns a collection of them.
183+ * <info>The returned Collection does not contain any reaction data of the messages.
184+ * Those need to be fetched separately.</info>
185+ * @param {boolean } [cache=true] Whether to cache the message(s)
186+ * @deprecated Use {@link MessageManager#fetchPins} instead.
187+ * @returns {Promise<Collection<Snowflake, Message>> }
188+ */
131189 async fetchPinned ( cache = true ) {
190+ if ( ! deprecationEmittedForFetchPinned ) {
191+ process . emitWarning (
192+ 'The MessageManager#fetchPinned() method is deprecated. Use MessageManager#fetchPins() instead.' ,
193+ 'DeprecationWarning' ,
194+ ) ;
195+
196+ deprecationEmittedForFetchPinned = true ;
197+ }
198+
132199 const data = await this . client . rest . get ( Routes . channelPins ( this . channel . id ) ) ;
133200 const messages = new Collection ( ) ;
134201 for ( const message of data ) messages . set ( message . id , this . _add ( message , cache ) ) ;
@@ -219,7 +286,7 @@ class MessageManager extends CachedManager {
219286 message = this . resolveId ( message ) ;
220287 if ( ! message ) throw new DiscordjsTypeError ( ErrorCodes . InvalidType , 'message' , 'MessageResolvable' ) ;
221288
222- await this . client . rest . put ( Routes . channelPin ( this . channel . id , message ) , { reason } ) ;
289+ await this . client . rest . put ( Routes . channelMessagesPin ( this . channel . id , message ) , { reason } ) ;
223290 }
224291
225292 /**
@@ -232,7 +299,7 @@ class MessageManager extends CachedManager {
232299 message = this . resolveId ( message ) ;
233300 if ( ! message ) throw new DiscordjsTypeError ( ErrorCodes . InvalidType , 'message' , 'MessageResolvable' ) ;
234301
235- await this . client . rest . delete ( Routes . channelPin ( this . channel . id , message ) , { reason } ) ;
302+ await this . client . rest . delete ( Routes . channelMessagesPin ( this . channel . id , message ) , { reason } ) ;
236303 }
237304
238305 /**
0 commit comments