简体   繁体   中英

Discord.js: client.login() promise never resolves, client.on("ready") never fires

I made a very simple Discord bot with discord.js; the bot replies with "Good morning to you too" every time someone sends a message containing the text "good morning". This used to work but I noticed that the bot stopped replying to messages today. My code is below; the bot is hosted on Replit.

const { Client, IntentsBitField } = require("discord.js");

const intents = new IntentsBitField();
intents.add(IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent);

const client = new Client({
  intents
});

client.on("ready", () => {
  console.log("Client ready.");
});

client.on("messageCreate", async (message) => {
  try {
    if (message.type === 0 && !message.author.bot) {
      if (message.content.trim().toUpperCase().includes("GOOD MORNING")) {
        await message.reply("Good morning to you too!");
      }
    }
  }
  catch (error) {
    console.log(error);
  }
});

client.login(process.env.TOKEN);

When I run this code, the client.login() promise never resolves and the ready event of the client never fires. I have checked for other possible causes, such as the process.env.TOKEN being wrong. Any idea what may cause this and how to fix it?

This is most likely because of a ratelimit. Replit is shared IP, making it easier to get ratelimited.

You can check if you are getting ratelimited with the debug event. It's a good idea to add this code, outside of any listeners. This will most likely answer why your code may seem to be doing "nothing".

client
    .on("debug", console.log)
    .on("warn", console.log)

If you get output, saying you hit a 429, you got ratelimited. Something that usually fixes it is running this in your terminal, then running the code again:

kill 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM