You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Repl.it中Discord.js的TokenInvalid错误?

问题描述

我是Node.js新手,经朋友推荐首次使用该技术,因本地资源不足选择在Repl.it上部署脚本。该脚本旨在让我的账号退出所有非白名单ID的Discord服务器,具体代码如下:

global.print = console.log

var {Client, IntentsBitField} = require("discord.js")
var bits = Object.values(IntentsBitField.Flags).filter(val => { return Number.isInteger(val)})
const client = new Client({ intents: bits })
const whitelist = [ // Array of guild IDs to NOT leave

 'id1'
 'id2'
 'id3'

]

async function main() {
var proms = []
var guild_collection = await client.guilds.fetch({limit: 200})
guild_collection = guild_collection.filter(guild => (!whitelist.includes(guild.id))).toJSON()

guild_collection.forEach(guild => { proms.push(guild.leave()) })

await Promise.all(proms) // Waits for all of them to be done tho...
print("Left every server..")
}

client.login(process.env['token']).then(() => {
  print("Moving forwards...")
  main()
})

我使用Repl.it的Secrets功能存储token,但收到TokenInvalid错误,错误信息如下:

/home/runner/Leave-Servers/node_modules/discord.js/src/client/websocket/WebSocketManager.js:136
    const invalidToken = new DiscordjsError(ErrorCodes.TokenInvalid);
                         ^

Error [TokenInvalid]: An invalid token was provided.
    at WebSocketManager.connect (/home/runner/Leave-Servers/node_modules/discord.js/src/client/websocket/WebSocketManager.js:136:26)
    at Client.login (/home/runner/Leave-Servers/node_modules/discord.js/src/client/Client.js:226:21)
    at Object.<anonymous> (/home/runner/Leave-Servers/index.js:21:8)
    at Module._compile (node:internal/modules/cjs/loader:1159:14) {
  code: 'TokenInvalid'
}

Node.js v18.12.1
repl process died unexpectedly: exit status 1

我尝试直接在代码中填写token(client.login("YOUR_DISCORD_BOT_TOKEN");),仍出现相同错误,且存在token泄露风险。请问我哪里操作有误?

解决方案

核心问题:混淆了Discord用户token与机器人token

你想用脚本操作个人Discord账号退出服务器,但当前代码用的是discord.js的Client类——这是专门给Discord机器人用的接口,机器人token无法登录个人用户账号,反之亦然,两者完全不兼容,这就是你触发TokenInvalid错误的根本原因。

修复步骤

1. 更换适配个人账号的库

discord.js不支持个人账号的自动化操作(且Discord服务条款不鼓励此类行为,使用需自行承担账号封禁风险),你需要改用支持用户token的库,比如discord.js-selfbot-v13。在Repl.it的终端执行安装命令:

npm install discord.js-selfbot-v13

2. 修改代码适配个人账号

替换原有代码,注意补充白名单服务器ID并修正语法问题:

global.print = console.log

const { Client } = require("discord.js-selfbot-v13");
const client = new Client();

const whitelist = [ 
  '你的白名单服务器ID1',
  '你的白名单服务器ID2',
  '你的白名单服务器ID3'
];

async function main() {
  const guilds = await client.guilds.fetch({ limit: 200 });
  // 过滤出非白名单的服务器
  const guildsToLeave = guilds.filter(guild => !whitelist.includes(guild.id));
  
  // 批量退出服务器
  await Promise.all(guildsToLeave.map(guild => guild.leave()));
  print("已退出所有非白名单服务器");
}

client.login(process.env['token']).then(() => {
  print("登录成功,开始处理...");
  main();
});

注:原代码的白名单数组元素之间缺少逗号,会引发语法错误,上述代码已修复该问题。

3. 确保使用正确的用户token

  • 获取个人Discord用户token:在Discord客户端/网页版开启开发者模式,打开控制台输入localStorage.getItem('token'),复制结果时注意去掉首尾的引号,不要包含多余空格或换行。
  • 在Repl.it的Secrets中添加token字段,粘贴获取到的用户token,确保环境变量配置正确。

重要提醒

  • Discord明确禁止自机器人(selfbot)行为,使用此类脚本可能导致账号永久封禁,请谨慎操作。
  • 永远不要在代码中硬写token,始终使用Repl.it的Secrets功能存储敏感信息,避免泄露。

内容的提问来源于stack exchange,提问作者Peyton S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:10:37