首页 > 解决方案 > 如何将文本和图像传递给数组

问题描述

所以我有机器人从数组中响应随机消息。现在我希望它同时响应文本和图像。我试过这个:

let msgs = [{text:'Hi guys',  pic: new Discord.MessageAttachment('https://i.ytimg.com/vi/JilHZ_DdBYg/maxresdefault.jpg')}, "xd", "hello", "hi my friend"]

let random = Math.floor(Math.random() * msgs.length);

   let random_msg = msgs[random];
     message.channel.send(random_msg)

错误是:

UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message

标签: javascriptdiscord

解决方案


DiscordAPI 消息 sdk 似乎只能发送非空字符串。而不是使用 Math.random() 进行测试,您应该遍历消息数组并尝试发送每条消息,此时您会发现零索引对象会引发错误。您将需要进一步调查 discord 文档以了解如何发送带字幕的图像。


推荐阅读