【问题标题】:I am getting an error message for a line that isnt there我收到一条不存在的行的错误消息
【发布时间】:2019-02-22 22:36:55
【问题描述】:

我目前正在用 javascript 制作一个不和谐的机器人,但它不会运行。每当我尝试运行它时,它都会显示此错误消息

C:\Users\user\Documents\--Discord robo--\bot.js:114
});
 ^

SyntaxError: Unexpected token )
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:499:15)

这是说错误在第 114 行,但我的代码只有 113 行。 https://pastebin.com/AwEz01Nt

   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, 2) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];
        var in1 = args[1];}

        args = args.splice(1);
        switch(cmd) {


            // !ping
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: 'Pong!'
                });
            break;


            // !commands
            case 'commands':
                bot.sendMessage({
                    to: channelID,
                    message: 'The current commands are: ping, commands, random and rps and the current prefix is h.'
                });
            break;


            // !random
        case 'random':
        var randomnumber; // setting the randomnumber variable
        var upper;
        upper = in1 ;
        randomnumber = Math.floor(Math.random() * Math.floor(in1));
        bot.sendMessage({to: channelID, message: 'Your random number from 1 to ' + in1 + ' is ' + randomnumber});
        break;


     // !rps
        case 'rps': //if the code is rps
            var in2;
            rpsrandom = Math.random();
            if ( rpsrandom <= 1/3 ) { in2 = 'rock' ;}
            if ( rpsrandom >= 2/3 ) {in2 = 'scissors' ;}
            else {in2 = 'paper';}
            bot.sendMessage({to: channelID, message: in2});
                if (in1 == 'r')//if in1 is rock
                { 
                    if (in2 == 'paper'){
                        bot.sendMessage({to: channelID, message: 'You lose!'});
                        break;
                    }
                    if (in2 == 'scissors'){
                        bot.sendMessage({to: channelID, message: 'You win!'});
                        break;
                    }
                    else {bot.sendMessage({to: channelID, message: 'It is a tie!'});
                    break;
                }
                if (in1 == 'p') //if in1 is rock
                { 
                    if (in2 == 'scissors'){
                        bot.sendMessage({to: channelID, message: 'You lose!'});
                        break;
                    }
                    if (in2 == 'rock'){
                        bot.sendMessage({to: channelID, message: 'You win!'});
                        break;
                    }
                    else {bot.sendMessage({to: channelID, message: 'It is a tie!'});
                    break;
                }
                if (in1 == 's')//if in1 is rock
                { 
                    if (in2 == 'rock'){
                        bot.sendMessage({to: channelID, message: 'You lose!'});
                        break;
                    }
                    if (in2 == 'paper'){
                        bot.sendMessage({to: channelID, message: 'You win!'});
                        break;
                    }
                    else {bot.sendMessage({to: channelID, message: 'It is a tie!'});
                    break;
                }
        break;  
            // Just add any case commands if you want to..
         }
    }
;

我对 java 脚本和不和谐机器人的编码非常陌生,所以我不知道为什么这不起作用

【问题讨论】:

  • 您在这一行缺少一个右大括号:else {bot.sendMessage({to: channelID, message: 'It is a tie!'});你永远不会关闭 else。
  • 但是行号不正确可能是因为您的浏览器缓存了您的文件副本。

标签: javascript node.js discord discord.js


【解决方案1】:

您在最后一个分号之前缺少一个右括号

代码的最后几行应该是

break;  
            // Just add any case commands if you want to..
         }
    }
);

通常,您的格式是一团糟,很难发现此类错误。考虑使用具有自动格式化功能的 IDE。

【讨论】:

  • 我已将其添加进去,除了第 113 行出现错误外,错误消息没有改变
  • 你也有什么建议
【解决方案2】:

在两者中

else {bot.sendMessage({to: channelID, message: 'It is a tie!'});

缺少右括号的行。

请稍微清理一下该代码,您会自己发现错误。 :)

【讨论】:

    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多