如何修复Discord机器人的“Declaration or statement expected”报错?
Discord机器人报错「Declaration or statement expected」/「Unexpected Token」解决求助
我是新手开发者,用Node.js v16.20.2开发Discord机器人,目前卡在「Declaration or statement expected」或「Unexpected Token」报错上,报错位置指向代码里的.play(queueConstruct.songs[0], message, client)语句。之前试过添加一段流媒体处理代码,结果问题更严重;移除.play前的点后,又出现其他未知报错,求帮忙解决。
相关代码
const { play } = require("../include/play"); const { Client, Collection, MessageEmbed } = require("discord.js"); const { attentionembed } = require("../util/attentionembed"); const {approveemoji,denyemoji,PREFIX,} = require(`../config.json`); const ytsr = require("youtube-sr"); async execute(message, args, client) { if (!message.guild) return; const { channel } = message.member.voice; const serverQueue = message.client.queue.get(message.guild.id); if (!channel) return attentionembed(message, "Please join a Voice Channel first"); if (serverQueue && channel !== message.guild.me.voice.channel) return attentionembed(message, `You must be in the same Voice Channel as me`); if (!args.length) return attentionembed(message, `Usage: ${message.client.prefix}play <YouTube URL or Video Name>`); //react with approve emoji message.react(approveemoji).catch(console.error); const permissions = channel.permissionsFor(message.client.user); if (!permissions.has("CONNECT")) return attentionembed(message, "I need permissions to join your channel!"); if (!permissions.has("SPEAK")) return attentionembed(message, "I need permissions to speak in your channel"); //...// try { .play(queueConstruct.songs[0], message, client), catch (error) { //if an error comes log console.error(error); //delete the Queue message.client.queue.delete(message.guild.id); //leave the channel await channel.leave(); //sent an error message return attentionembed(message, `Could not join the channel: ${error}`); } };
曾尝试添加的代码
let stream = null; if (song.url.includes("youtube.com")) { stream = await ytdl(song.url); stream.on('error', function(er) { if (er) { if (queue) { queue.songs.shift(); play(queue.songs[0]); return sendError(`An unexpected error has occurred.\nPossible type \`${er}\``, message.channel) } } }); } queue.connection.on("disconnect", () => message.client.queue.delete(message.guild.id)); const dispatcher = queue.connection .play(ytdl(song.url, {quality: 'highestaudio', highWaterMark: 1 << 25 ,type: "opus"})) .on("finish", () => { const shiffed = queue.songs.shift(); if (queue.loop === true) { queue.songs.push(shiffed); }; play(queue.songs[0]) })
问题分析与解决方法
核心语法错误:
你通过const { play } = require("../include/play");解构导入了play函数,它是一个独立函数,不是某个对象的方法,所以不能用.play()调用,直接写play(queueConstruct.songs[0], message, client)即可。try/catch语法错误:
你的代码里try块结尾多了个逗号,正确语法是try { 执行代码 } catch (error) { 错误处理 },需要把逗号删掉。修复后的关键代码片段:
try { play(queueConstruct.songs[0], message, client); } catch (error) { //if an error comes log console.error(error); //delete the Queue message.client.queue.delete(message.guild.id); //leave the channel await channel.leave(); //sent an error message return attentionembed(message, `Could not join the channel: ${error}`); }
- 额外注意点:
- 尝试添加的流媒体代码里,要确保已导入
ytdl-core(const ytdl = require('ytdl-core');),否则会报错找不到ytdl。 - 检查
sendError函数是否已定义或导入,queue变量是否在当前作用域内有效。
- 尝试添加的流媒体代码里,要确保已导入
内容的提问来源于stack exchange,提问作者Kikiyama
相关产品推荐
相关产品推荐

