尝试使用js创建Discord bot出现错误

时间:2020-05-09 23:01:24

标签: javascript json

您好,我正在遵循digitaltrends网站上的教程,该教程是关于如何创建出现一些错误的Discord机器人

我遵循的教程位于https://www.digitaltrends.com/gaming/how-to-make-a-discord-bot/

尝试编译和下载必要的依赖项时出现错误

**C:\discordbots>npm install discord.io winston -save**
npm ERR! code EJSONPARSE
npm ERR! file C:\discordbots\package.json
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected token “ in JSON at position 3 while parsing near '{
npm ERR! JSON.parse “name”: “PUAGirls-bo...'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\eugen\AppData\Roaming\npm-cache\_logs\2020-05-09T21_06_55_511Z-debug.log

**C:\discordbots>npm install discord.io winston -save**
npm WARN saveError ENOENT: no such file or directory, open 'C:\discordbots\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'C:\discordbots\package.json'
npm WARN discordbots No description
npm WARN discordbots No repository field.
npm WARN discordbots No README data
npm WARN discordbots No license field.

+ winston@3.2.1
+ discord.io@2.5.3
added 43 packages from 35 contributors and audited 49 packages in 25.961s
found 0 vulnerabilities


**C:\discordbots>npm install https://github.com/woor/discord.io/tarball/gateway_v6**
npm WARN saveError ENOENT: no such file or directory, open 'C:\discordbots\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\discordbots\package.json'
npm WARN discordbots No description
npm WARN discordbots No repository field.
npm WARN discordbots No README data
npm WARN discordbots No license field.

+ discord.io@2.5.3
updated 1 package and audited 144 packages in 6.168s
found 0 vulnerabilities


C:\discordbots>node bot.js
internal/modules/cjs/loader.js:1193
    throw err;
    ^

SyntaxError: C:\discordbots\auth.json: Unexpected token “ in JSON at position 3
at parse (<anonymous>)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:1190:22)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\discordbots\bot.js:3:12)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)

结束错误

我该如何解决呢?我也已经完成了必要的.json文件,但是主要的auth.json文件中包含我的客户ID,实际上并不想发出

该机器人的JavaScript代码是

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, 
{
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '!') {
    var args = message.substring(1).split(' ');
    var cmd = args[0];

    args = args.splice(1);
    switch(cmd) {
        // !lucy
        case 'lucy':
            bot.sendMessage({
                to: channelID,
                message: 'Whatever you do DO NOT buy this girl perfume'
            });
        break;
        // Just add any case commands if you want to..
     }
 }
});

我还有36个这样的命令需要做,而且可能还有更多

0 个答案:

没有答案