我目前正在用JavaScript制作Discord机器人,但它无法运行。每当我尝试运行它时,都会显示此错误消息
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脚本和不和谐的bot编码非常陌生,所以我不知道为什么这不起作用
答案 0 :(得分:2)
在最后一个分号之前您缺少右括号
代码的最后几行应该是
break;
// Just add any case commands if you want to..
}
}
);
通常,您的格式很乱,很难发现此类错误。考虑使用具有自动格式化功能的IDE。
答案 1 :(得分:1)
两者
else {bot.sendMessage({to: channelID, message: 'It is a tie!'});
行缺少右括号。
请稍微清理一下该代码,您将自己发现所有错误。 :)