Discord机器人发送随机Embed报错求助(JS/Node.js)
Discord机器人随机Embed发送实现记录
我要做一个Discord机器人,触发指定命令(比如!embedzay)时发送随机Embed。目前已经搞定单条Embed发送和随机文本发送,但折腾随机Embed时踩了不少坑,遇到过[object Object]、无法发送空消息的错误,甚至搞崩过机器人。我是JavaScript新手,用JS和Node.js开发,下面是我的尝试过程和最终能用的代码:
可用代码示例
随机文本发送(!rtest命令)
//rtest - random test + if (command === 'rtest'){ const options = [ "message1", "message2", "message3", ] const random = options[Math.floor(Math.random() * options.length)] message.channel.send(`${random}`) }
单条Embed发送(!embedz命令)
//embed thingy - send and embed + if (command === 'embedz'){ message.channel.send({ embeds: [new EmbedBuilder() .setTitle('Some title') .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')], }); }
失败的随机Embed尝试及问题
尝试一:出现[object Object]错误
把Embed对象和普通字符串混在数组里,用模板字符串包裹发送时,对象被转成了默认的[object Object]字符串。
if (command === 'rtest'){ const options = [ {embeds: new EmbedBuilder().setTitle('rtfhgfh')}, "message2", "message3", ] const random = options[Math.floor(Math.random() * options.length)] message.channel.send(`${random}`) }
尝试二:出现「无法发送空消息」错误
直接把Embed对象传给send()方法,Discord.js要求发送Embed必须放在embeds数组参数里,单独传对象不符合API要求。
// earlier embed attempt - cannot send empty message if (command === 'embedzay'){ const embed3 = new EmbedBuilder() .setTitle('rtfhgfh') .setDescription('yhhfggf') const embed4 = new EmbedBuilder() .setTitle('haha') .setDescription('bunny') var embedArr = [embed3, embed4]; let randomEmbed = embedArr[Math.floor(Math.random() * embedArr.length)]; message.channel.send(randomEmbed); }
尝试三:再次出现[object Object]错误
和尝试一同理,用模板字符串包裹Embed对象,导致对象被转成字符串输出。
if (command === 'embedzy'){ const embed1 = new EmbedBuilder() .setTitle('Some title') .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif') const embed2 = new EmbedBuilder() .setTitle('Some titleuhhhhhh') .setImage('https://i.imgur.com/64l7KFc.png'); const embeds = [embed1, embed2] const random = embeds[Math.floor(Math.random() * embeds.length)] message.channel.send(`${random}`) }
尝试四:仅输出JSON字符串,无法显示Embed
把Embed对象转成JSON字符串发送,Discord只会显示纯文本,不会解析成Embed组件。
if (command === 'embedzye'){ const embed1 = new EmbedBuilder() .setTitle('Some title') .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif') const embed2 = new EmbedBuilder() .setTitle('Some titleuhhhhhh') .setImage('https://i.imgur.com/64l7KFc.png'); const embeds = [embed1, embed2] const random = embeds[Math.floor(Math.random() * embeds.length)] message.channel.send(`${JSON.stringify(random)}`) }
最终可行的随机Embed代码(!embedzyel命令)
核心是把随机选中的Embed对象放在embeds数组里,作为send()方法的参数传递,符合Discord.js的API要求。
if (command === 'embedzyel'){ const embed1 = new EmbedBuilder() .setTitle('Some title') .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif') const embed2 = new EmbedBuilder() .setTitle('Some titleuhhhhhh') .setImage('https://i.imgur.com/64l7KFc.png'); const embeds = [embed1, embed2] const random = embeds[Math.floor(Math.random() * embeds.length)] message.channel.send({embeds: [random],}); }
内容的提问来源于stack exchange,提问作者user17777004
相关产品推荐
相关产品推荐

