每当我运行机器人代码时,都会出现SyntaxError:输入意外结束。
我没有看到方括号,所以我想知道你们中是否有人可以看到这个问题。
我的代码如下:
const request = require('request');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
if (msg.author.bot) return;
if (msg.content === `${prefix}ping`) {
msg.reply('Pong!');
}
if (msg.content === `${prefix}rule`) {
msg.reply('rules are not yet implemented, check out https://5thsrd.org/ for rules in the meantime');
}
if (msg.content.startswith(`${prefix}spell`)) {
request(`http://www.dnd5eapi.co/api/spells/${args}`, { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
else if (command === 'spell')
msg.channel.send(`Name: ${body.name}`);
msg.channel.send(`${body.desc}`);
msg.channel.send(`At Higher Levels: ${body.higher_level}`)
if (msg.content.startsWith(`${prefix}class`)) {
request(`http://www.dnd5eapi.co/api/classes/${args}`, { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
else if (command === 'class')
msg.channel.send(`Name: ${body.name}`);
msg.channel.send(`Hit Dice:${body.hit_die}`);
msg.channel.send(`Proficiency Options: ${body.proficiency_choices}`);
msg.channel.send(`Starting Equipment: ${body.starting_equipment}`);
msg.channel.send(`Proficiencies: ${body.proficiencies}`);
msg.channel.send(`Subclasses: ${body.subclasses}`);
});
}
})
}
});
client.login(token);
答案 0 :(得分:1)
如果您使用任何online JavaScript "beautifier",linter或任何重量合适的IDE,您很快就会意识到您实际上并没有正确关闭函数。
您缺少2件事:
}
语句的大括号(if (msg.content.startswith(`${prefix}spell`))
)。}
),大括号()
)和分号(;
)来封闭client.on('message', msg => {
块。总而言之,您固定的代码应类似于:
const request = require('request');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
if (msg.author.bot) return;
if (msg.content === `${prefix}ping`) {
msg.reply('Pong!');
}
if (msg.content === `${prefix}rule`) {
msg.reply('rules are not yet implemented, check out https://5thsrd.org/ for rules in the meantime');
}
if (msg.content.startswith(`${prefix}spell`)) {
request(`http://www.dnd5eapi.co/api/spells/${args}`, {
json: true
}, (err, res, body) => {
if (err) {
return console.log(err);
} else if (command === 'spell')
msg.channel.send(`Name: ${body.name}`);
msg.channel.send(`${body.desc}`);
msg.channel.send(`At Higher Levels: ${body.higher_level}`)
if (msg.content.startsWith(`${prefix}class`)) {
request(`http://www.dnd5eapi.co/api/classes/${args}`, {
json: true
}, (err, res, body) => {
if (err) {
return console.log(err);
} else if (command === 'class')
msg.channel.send(`Name: ${body.name}`);
msg.channel.send(`Hit Dice:${body.hit_die}`);
msg.channel.send(`Proficiency Options: ${body.proficiency_choices}`);
msg.channel.send(`Starting Equipment: ${body.starting_equipment}`);
msg.channel.send(`Proficiencies: ${body.proficiencies}`);
msg.channel.send(`Subclasses: ${body.subclasses}`);
});
}
});
}
});
client.login(token);