首页 > 解决方案 > SyntaxError:Discord.js v12 中的令牌无效或意外

问题描述

我已经在 Discord.js 中启动了一个新机器人,当我尝试运行我的 index.js 文件时,SyntaxError: Invalid or unexpected token会发生该错误。我的 index.js 文件如下图...(代码太多,我只好缩短了)

const fs = require("fs");
const Discord = require('discord.js');
const { prefix, token } = require("./config.json");

const modules = fs.readdirSync("./commands")
client.commands = new Discord.Collection()

for(const cmds of modules) {
    const command = require(`./commands/${cmds}`)
    client.commands.set(command.name, command);
    }

client.on('ready', () => {
    // Status Code Block
}
});


client.on('message', async message => {
    const args = message.content.slice(prefix.length).split(/ +/g)
    const cmdName = args.shift()

    if(!client.commands.has(cmdName)) return

    const command = client.commands.get(cmdName)
    command.run(message, args, client)
})

client.login(token);

不知道有什么问题。有人可以帮忙吗?

标签: node.jsdiscord.js

解决方案


我在您的代码中缺少的是客户端定义。

const client = new Discord.Client();

但是让我们假设您在索引文件中定义了一个客户端。您是否尝试过在您的不和谐开发者门户上重新生成令牌?也许它以某种方式泄露并被撤销。


推荐阅读