简体   繁体   English

Discord.js:client.login() promise 永远不会解析,client.on("ready") 永远不会触发

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

I made a very simple Discord bot with discord.js;我用 discord.js 做了一个非常简单的 Discord 机器人; 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.该机器人托管在 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.当我运行此代码时, client.login() promise 永远不会解析,并且clientready事件永远不会触发。 I have checked for other possible causes, such as the process.env.TOKEN being wrong.我检查了其他可能的原因,例如process.env.TOKEN错误。 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. Replit 是共享的 IP,更容易获得速率限制。

You can check if you are getting ratelimited with the debug event.您可以通过debug事件检查您是否受到速率限制。 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.如果你得到 output,说你达到了 429,你得到了速率限制。 Something that usually fixes it is running this in your terminal, then running the code again:通常修复它的方法是在您的终端中运行它,然后再次运行代码:

kill 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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