#Attempting to react to messages older than bot does not work

5 messages · Page 1 of 1 (latest)

low gale
#

I'm trying to make my bot react with an emoji to all messages that are in a server. However, the reactions do go back by one day, but don't go farther than that.
Here's the code:

const { Client, Events, GatewayIntentBits, Partials, TextChannel } = require("discord.js")
const env = require("dotenv")
env.config()

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
partials: [ Partials.Channel, Partials.Message ] });
client.once(Events.ClientReady, async (readyClient) => {
    console.log(`Ready! Logged in as ${readyClient.user.tag}`);
    let server = await client.guilds.fetch("1155619424020213801");
    let channels = await server.channels.fetch(); // Get ALL channels
    for (let channel of channels){
        if (channel[1].type == 4){continue}
        let messages = await fetchAllMessages(channel[1],server)
        //const filteredMessages = messages.filter(m => m.reactions.cache.me == false);
        let i = 0;
        messages.forEach(function(e){
            e.react("🍉");
            i++;
        })
        console.log("Finished with channel " + channel[1].name+" and count "+i)
    }
    console.log("Finished reacting.")
});

// Log in to Discord with your client's token
client.login(process.env.token);

async function fetchAllMessages(id,server) {
  const channel = id
  if (channel.size == 38){
    console.log("Defaulted?")
    return []
  }
  let messages = [];
  let message = channel.messages.fetch({ limit: 1 })
    .then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
  while (message) {
    await channel.messages
      .fetch({ limit: 100, before: message.id })
      .then(messagePage => {
        messagePage.forEach(msg => messages.push(msg));
        message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
      });
  }

  return messages // Print all messages
}
crystal bluffBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
low gale
wicked turtleBOT
#

Ratelimits are dynamically assigned by the API based on current load and may change at any point.

  • The scale from okay to API-spam is sliding and depends heavily on the action you are taking
  • Rainbow roles, clock and counter channels, and DM'ing advertisements to all members are all examples of things that are not okay
raw dome
#

Trying to React to all messages sent in the past definitely falls into „not okay“ category here