【问题标题】:Discord bot yields error Error[TOKEN_INVALID] An invalid token was providedDiscord bot 产生错误 Error[TOKEN_INVALID] 提供了无效令牌
【发布时间】:2021-06-24 03:01:57
【问题描述】:

我正在制作一个不和谐的机器人。完成所有代码后,我运行node index.js 来启动机器人。它返回了这个错误:

PS F:\DISCORDBOTS\Ultron> node index.js
F:\DISCORDBOTS\Ultron\node_modules\discord.js\src\client\Client.js:206
    if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
                                                   ^

Error [TOKEN_INVALID]: An invalid token was provided.
    at Client.login (F:\DISCORDBOTS\Ultron\node_modules\discord.js\src\client\Client.js:206:52)
    at Object.<anonymous> (F:\DISCORDBOTS\Ultron\index.js:98:5)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  [Symbol(code)]: 'TOKEN_INVALID'
}

我使用.env 作为机器人的令牌。我什至多次重新生成令牌,但它仍然返回相同的错误。我可以使用config.json 文件,但之后repl.it 上的所有人都可以看到令牌。如何解决此错误?

【问题讨论】:

  • 您是否尝试在将令牌传递给函数之前打印令牌,并确保它与您从 Discord 获得的令牌相同?可能是您未能正确加载它,或者它在某处被覆盖。
  • 打印令牌??
  • 是的,在你的代码中添加一个console.log(token) 并检查它是否正确(假设你在本地运行它并且你是唯一看到它的人)。
  • 没用 :(
  • 您是否使用 dotenv 从您的 .env 文件中加载变量?如果是,您如何要求和配置它?

标签: javascript node.js discord discord.js bots


【解决方案1】:

我们必须以字符串格式提供令牌。在客户端代码中它是这样写的(if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');)。所以试试这个代码。当我更改为这段代码然后我的代码运行良好。

    const { Client, Intents } = require('discord.js');
    require("dotenv").config();
    const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });                                         
    const mySecret = process.env['TOKEN'];
     client.on("ready", () => {
     console.log(`Logged is as ${client.user.tag}!`);
    });


   client.on("message", (msg) => {
    if(msg.content === "ping"){
    msg.reply("pong");
   }
   });

   client.login(mySecret);

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2020-09-10
    • 2021-11-16
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多