'Const' 未定义。 (discord.js)

'Const' Is not defined. (discord.js)

我正在制作一个音乐机器人,我想在它 returns 一条消息时添加一个嵌入,但我收到一条错误消息,指出 const 未定义。

这是我的代码:

const Discord = require('discord.js');
const bot = new Discord.Client({
    partials: ['MESSAGE', 'CHANNEL', 'REACTION']

});
const config = require('./settings.json');
const { loadCommands } = require('./utils/loadCommands');
const DisTube = require('distube');


bot.distube = new DisTube(bot, { searchSongs: false, emitNewSongOnly: true });
bot.distube
    .on("playSong", (message, queue, song) => message.channel.send(
    const exampleEmbed = new Discord.MessageEmbed()   
        .setColor('#0099ff')
    .setTitle('Playing Now:')
    .setDescription('`${song.name}`')
    .setTimestamp()
    .setFooter('Requested by: ${song.user}');        


    .on("addSong", (message, queue, song) => message.channel.send(
        .setColor('#0099ff')
    .setTitle('Added to Queue:')
    .setDescription('\`${song.name}\`')
    .setTimestamp()
    .setFooter('${song.formattedDuration} - Requested by ${song.user}');         

require('./utils/loadEvents')(bot);

bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();

loadCommands(bot);

bot.login(config.token);

然后在我 运行 它之后,我得到这个错误:

const exampleEmbed = new Discord.MessageEmbed()
    ^^^^^

SyntaxError: Unexpected token 'const'
←[90m    at wrapSafe (node:internal/modules/cjs/loader:1024:16)←[39m
←[90m    at Module._compile (node:internal/modules/cjs/loader:1072:27)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:973:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:813:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)←[39m

看起来 channel.send() 接受字符串或嵌入作为参数。您应该在发送前在外层 lambda 中定义消息:

// abbreviated code to relevant snippet
bot.distube = new DisTube(bot, { searchSongs: false, emitNewSongOnly: true });
bot.distube
   .on("playSong", (message, queue, song) => {
       const exampleEmbed = new Discord.MessageEmbed()
           ...
       message.channel.send(exampleEmbed);
   });

我从未使用过 discord js,但通过快速查看文档,我猜你应该先声明你的变量然后发送它

bot.distube
    .on("playSong", (message, queue, song) => {
    const exampleEmbed = new Discord.MessageEmbed()
           .setColor('#0099ff')
         .setTitle('Playing Now:')
         .setDescription('`${song.name}`')
         .setTimestamp()
         .setFooter(`Requested by: ${song.user}`);  

     message.channel.send(exampleEmbed);
}

还有2个问题。

  1. 注意 .setFooter('Requested by: ${song.user}') 模板字符串应该带有反引号 '`'。见下文
.setFooter(`Requested by: ${song.user}`)
  1. 你的事件侦听器( playSong 和 addSong )和你的 send 方法似乎没有关闭 )

在这里阅读更多 -> embed discord