@@ -10,6 +10,8 @@ const MessagePayload = require('../structures/MessagePayload');
1010const { MakeCacheOverrideSymbol } = require ( '../util/Symbols' ) ;
1111const { resolvePartialEmoji } = require ( '../util/Util' ) ;
1212
13+ let deprecationEmittedForFetchPinned = false ;
14+
1315/**
1416 * Manages API methods for Messages and holds their cache.
1517 * @extends {CachedManager }
@@ -116,19 +118,83 @@ class MessageManager extends CachedManager {
116118 return data . reduce ( ( _data , message ) => _data . set ( message . id , this . _add ( message , options . cache ) ) , new Collection ( ) ) ;
117119 }
118120
121+ /**
122+ * Options used to fetch pinned messages.
123+ *
124+ * @typedef {Object } FetchPinnedMessagesOptions
125+ * @property {DateResolvable } [before] Consider only pinned messages before this time
126+ * @property {number } [limit] The maximum number of pinned messages to return
127+ * @property {boolean } [cache] Whether to cache the pinned messages
128+ */
129+
130+ /**
131+ * Data returned from fetching pinned messages.
132+ *
133+ * @typedef {Object } FetchPinnedMessagesResponse
134+ * @property {MessagePin[] } items The pinned messages
135+ * @property {boolean } hasMore Whether there are additional pinned messages that require a subsequent call
136+ */
137+
138+ /**
139+ * Pinned message data returned from fetching pinned messages.
140+ *
141+ * @typedef {Object } MessagePin
142+ * @property {Date } pinnedAt The time the message was pinned at
143+ * @property {number } pinnedTimestamp The timestamp the message was pinned at
144+ * @property {Message } message The pinned message
145+ */
146+
119147 /**
120148 * Fetches the pinned messages of this channel and returns a collection of them.
121149 * <info>The returned Collection does not contain any reaction data of the messages.
122150 * Those need to be fetched separately.</info>
123- * @param {boolean } [cache=true] Whether to cache the message(s)
124- * @returns {Promise<Collection<Snowflake, Message>> }
151+ *
152+ * @param {FetchPinnedMessagesOptions } [options={}] Options for fetching pinned messages
153+ * @returns {Promise<FetchPinnedMessagesResponse> }
125154 * @example
126155 * // Get pinned messages
127- * channel.messages.fetchPinned ()
128- * .then(messages => console.log(`Received ${messages.size } messages`))
156+ * channel.messages.fetchPins ()
157+ * .then(messages => console.log(`Received ${messages.items.length } messages`))
129158 * .catch(console.error);
130159 */
160+ async fetchPins ( options = { } ) {
161+ const data = await this . client . rest . get ( Routes . channelMessagesPins ( this . channel . id ) , {
162+ query : makeURLSearchParams ( {
163+ ...options ,
164+ before : options . before && new Date ( options . before ) . toISOString ( ) ,
165+ } ) ,
166+ } ) ;
167+
168+ return {
169+ items : data . items . map ( item => ( {
170+ pinnedTimestamp : Date . parse ( item . pinned_at ) ,
171+ get pinnedAt ( ) {
172+ return new Date ( this . pinnedTimestamp ) ;
173+ } ,
174+ message : this . _add ( item . message , options . cache ) ,
175+ } ) ) ,
176+ hasMore : data . has_more ,
177+ } ;
178+ }
179+
180+ /**
181+ * Fetches the pinned messages of this channel and returns a collection of them.
182+ * <info>The returned Collection does not contain any reaction data of the messages.
183+ * Those need to be fetched separately.</info>
184+ * @param {boolean } [cache=true] Whether to cache the message(s)
185+ * @deprecated Use {@link MessageManager#fetchPins} instead.
186+ * @returns {Promise<Collection<Snowflake, Message>> }
187+ */
131188 async fetchPinned ( cache = true ) {
189+ if ( ! deprecationEmittedForFetchPinned ) {
190+ process . emitWarning (
191+ 'The MessageManager#fetchPinned() method is deprecated. Use MessageManager#fetchPins() instead.' ,
192+ 'DeprecationWarning' ,
193+ ) ;
194+
195+ deprecationEmittedForFetchPinned = true ;
196+ }
197+
132198 const data = await this . client . rest . get ( Routes . channelPins ( this . channel . id ) ) ;
133199 const messages = new Collection ( ) ;
134200 for ( const message of data ) messages . set ( message . id , this . _add ( message , cache ) ) ;
@@ -219,7 +285,7 @@ class MessageManager extends CachedManager {
219285 message = this . resolveId ( message ) ;
220286 if ( ! message ) throw new DiscordjsTypeError ( ErrorCodes . InvalidType , 'message' , 'MessageResolvable' ) ;
221287
222- await this . client . rest . put ( Routes . channelPin ( this . channel . id , message ) , { reason } ) ;
288+ await this . client . rest . put ( Routes . channelMessagesPin ( this . channel . id , messageId ) , { reason } ) ;
223289 }
224290
225291 /**
@@ -232,7 +298,7 @@ class MessageManager extends CachedManager {
232298 message = this . resolveId ( message ) ;
233299 if ( ! message ) throw new DiscordjsTypeError ( ErrorCodes . InvalidType , 'message' , 'MessageResolvable' ) ;
234300
235- await this . client . rest . delete ( Routes . channelPin ( this . channel . id , message ) , { reason } ) ;
301+ await this . client . rest . delete ( Routes . channelMessagesPin ( this . channel . id , messageId ) , { reason } ) ;
236302 }
237303
238304 /**
0 commit comments