SyntaxError:意外令牌}当给定命令节点bot.js时

时间:2019-08-07 11:28:29

标签: javascript bots discord discord.js

因此,我正在尝试编写对JavaScript几乎一无所知的Discord机器人。每次我运行“ node bot.js”时,都会出现以下内容:

  

C:\ Users \ Indu Sreejith \ Desktop \ Card_Kingdom_Jest> node bot.js   C:\ Users \ Indu Sreejith \ Desktop \ Card_Kingdom_Jest \ bot.js:40}); ^

     

SyntaxError:意外令牌}       在Module._compile(内部/模块/cjs/loader.js:723:23)       在Object.Module._extensions..js(内部/模块/cjs/loader.js:789:10)       在Module.load(internal / modules / cjs / loader.js:653:32)       在tryModuleLoad(内部/模块/cjs/loader.js:593:12)       在Function.Module._load(内部/模块/cjs/loader.js:585:3)       在Function.Module.runMain(内部/模块/cjs/loader.js:831:12)       在启动时(internal / bootstrap / node.js:283:19)       在bootstrapNodeJSCore(internal / bootstrap / node.js:622:3)

我已经拥有Microsoft Visual Studio,并且校对已经完成。下面粘贴的是“ bot.js”中的代码。

require('dotenv').config();
const Discord = require('discord.js');

// Our client - some people call it bot.
const client = new Discord.Client(); 

// Constant Variables
let prefix = process.env.PREFIX; // Make sure to create a .env file!

// Message event 
client.on('message', message => {

    // Variables
    let args = message.content.slice(prefix.length).trim().split(' ');
    let cmd = args.shift().toLowerCase();

    // Return Statements
    if (message.author.bot) return; 
    if (!message.content.startsWith(prefix)) return; 

    // Command Handler
    try {

        let commandFile = require(`./commands/${cmd}.js`); 
        commandFile.run(client, message, args); 

    } catch (e) {
        console.log(e.stack);
    }

});

// Ready Event - Bot online / Bot started
client.on('ready', () => console.log('Bot Launched!'));
client.user.setActivity("STATUS_HERE", {
        type: "PLAYING", // You can also use watching or streaming
        //url: "https://www.twitch.tv/YOUR_CHANNEL" 
        // For streaming option
        });
});

// Discord Login
client.login(process.env.TOKEN);

编辑

我将代码更改为

require('dotenv').config();
const Discord = require('discord.js');

// Our client - some people call it bot.
const client = new Discord.Client(); 

// Constant Variables
let prefix = process.env.PREFIX; // Make sure to create a .env file!

// Message event 
client.on('message', message => {

    // Variables
    let args = message.content.slice(prefix.length).trim().split(' ');
    let cmd = args.shift().toLowerCase();

    // Return Statements
    if (message.author.bot) return; 
    if (!message.content.startsWith(prefix)) return; 

    // Command Handler
    try {

        let commandFile = require(`./commands/${cmd}.js`); 
        commandFile.run(client, message, args); 

    } catch (e) {
        console.log(e.stack);
    }

});

// Ready Event - Bot online / Bot started
client.on('ready', () => console.log('Bot Launched!'));





// Discord Login
client.login(process.env.TOKEN); 

我遇到以下错误:

C:\Users\Indu Sreejith\Desktop\Card_Kingdom_Jest>node bot.js
(node:13256) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
    at Promise (C:\Users\Indu Sreejith\Desktop\Card_Kingdom_Jest\node_modules\discord.js\src\client\rest\RESTMethods.js:34:54)
    at new Promise (<anonymous>)
    at RESTMethods.login (C:\Users\Indu Sreejith\Desktop\Card_Kingdom_Jest\node_modules\discord.js\src\client\rest\RESTMethods.js:33:12)
    at Client.login (C:\Users\Indu Sreejith\Desktop\Card_Kingdom_Jest\node_modules\discord.js\src\client\Client.js:280:30)
    at Object.<anonymous> (C:\Users\Indu Sreejith\Desktop\Card_Kingdom_Jest\bot.js:41:8)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
(node:13256) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:13256) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我是新手,所以请给我建议如何进行...

1 个答案:

答案 0 :(得分:0)

当它显示an invalid token was provided时,最有可能意味着您在调用client.login()时没有在环境文件中为您的机器人提供正确的令牌。要查找机器人的令牌,请转到https://discordapp.com/developers/applications/,然后单击您的应用。在左侧,单击Bot,然后会有一个选项可以显示或复制您的令牌。