使用discord.js开发Discord机器人报SyntaxError: unexpected end of input如何修复
问题原因
SyntaxError: Unexpected end of input属于JavaScript语法错误,本质是代码中的块级符号(花括号、圆括号等)没有成对闭合,解析器运行到文件末尾还没找到预期的闭合符号,所以抛出该错误。
你代码里的问题是:client.on("message", function(message) { 注册消息监听事件时传入的回调函数,只写了开头的左花括号{,全程没有补充对应的右花括号}闭合函数作用域。
修复方法
在require('http')代码行的前面,补充一个右花括号},闭合client.on的回调函数作用域即可。
修复后完整代码:
const Discord = require("discord.js"); const config = require("./config.json"); const client = new Discord.Client(); const prefix = "!"; client.on("message", function(message) { if (message.author.bot) return; if (!message.content.startsWith(prefix)) return; const commandBody = message.content.slice(prefix.length); const args = commandBody.split(' '); const command = args.shift().toLowerCase(); if (command === "help") { message.reply(`Discord Revolution Upon hitting Ctrl + / to bring up the Discord Key Combos menu, four directional arrows will appear in the upper-right-hand corner of the dialog. Pressing the arrows on your keyboard will play a designated tone for each direction. You can press multiple keys at a time to create chords. Error Page If you get to the discord 404 ERROR page and press the button to the right of the hamster or enter in the Konami code, you can play "SNEK". SNEK is essentially the classic Snake. Copied Username When you click on your profile number/ID, Discord copies it for you and displays a little green text box with "Copied!" in it. Discord_Name_Copy_Easter_Egg Discord Name Copy Easter Egg Repeatedly clicking your ID will prompt the following messages: Copied! Double Copy! Triple Copy! Dominating!! Rampage!! Mega Copy!! Unstoppable!! Wicked Sick!! Monster Copy!!! GODLIKE!!!! BEYOND GODLIKE!!!! Note: 'GODLIKE!!!' and 'BEYOND GODLIKE!!!!' have a shaking text box with a red background. This is a reference to kill streaks in Dota 2, where each of these lines is played in succession after each kill, and then stopping at BEYOND GODLIKE!!!! and repeating it until the streak ends. New_Discord_Ringtone New Discord Ringtone`) } // 此处补充回调函数的闭合花括号 }) require('http').createServer((_, res) => res.end("Nothing here! This is for a discord bot. Join My discord server: https://discord.com/invite/SrGJhjbqaY")).listen(8080) client.login(config.BOT_TOKEN);
额外提示
如果你使用的是discord.js v13及以上版本,message事件已经被弃用,需要替换为messageCreate事件,同时初始化Client时需要传入对应的intents配置才能正常接收消息。
内容的提问来源于stack exchange,提问作者Calvin Tanuri
相关产品推荐
相关产品推荐

