Message History

Fetch messages, pin history, search, and bulk delete. Bots use preloadMessages, not bulkFetchMessages.

Fetch one message

If you already have a text-based channel:

javascript
const msg = await channel.messages.fetch(messageId);

From IDs only:

javascript
const msg = await client.channels.fetchMessage(channelId, messageId);

Fetch recent messages

javascript
const page = await channel.messages.fetch({ limit: 50 });
const older = await channel.messages.fetch({ limit: 50, before: lastId });

Exact option names follow the manager (limit, before, after, around).

Preload several channels (bots)

client.preloadMessages is the bot-legal way to warm recent messages across channels. client.bulkFetchMessages is a user-account API (DefaultUserOnly). Bots receive AccessDeniedError.

javascript
const latest = await client.preloadMessages(['111', '222']);
for (const [channelId, message] of Object.entries(latest)) {
  console.log(channelId, message?.id ?? 'empty');
}

Then page with channel.messages.fetch as usual.

Search (bots)

Search in the current bot scope. scope: current is sent for you. Other scopes return BOT_SEARCH_SCOPE_UNAVAILABLE.

javascript
const results = await client.searchMessages({
  content: 'hello',
  contextGuildId: message.guildId,
  hitsPerPage: 25,
});
if ('indexing' in results) {
  // Wait and retry; this channel is still being indexed.
} else {
  console.log(results.total, results.hitsPerPage, results.page, results.cursor);
  for (const hit of results.messages) {
    console.log(hit.id, hit.content);
  }
}

Bulk delete

channel.bulkDelete accepts a count or an ID list:

javascript
await channel.bulkDelete(20);
await channel.bulkDelete([id1, id2, id3]);

Out-of-range counts throw FluxerError with INVALID_FETCH_LIMIT (or the bulk-delete equivalent). Catch and reply helpfully. See Errors.

Pins

javascript
const pinned = await channel.fetchPinnedMessages();
const page = await channel.fetchPinnedMessagesPage({ limit: 50 });

Permissions

History and bulk delete need View Channel, Read Message History, and Manage Messages (for deleting others). Always check before deleting other people's messages.

Runnable demo: history-bot.

Questions?Join the Fluxer community for help with the SDK.Join Fluxer