javascript - 如何将文本和图像传递给数组
问题描述
所以我有机器人从数组中响应随机消息。现在我希望它同时响应文本和图像。我试过这个:
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
解决方案
DiscordAPI 消息 sdk 似乎只能发送非空字符串。而不是使用 Math.random() 进行测试,您应该遍历消息数组并尝试发送每条消息,此时您会发现零索引对象会引发错误。您将需要进一步调查 discord 文档以了解如何发送带字幕的图像。
推荐阅读
- c# - 第二次迁移时出现 Entity Framework Core 编译器错误
- c# - 初始化全局存储库或表单?
- python - 将 SMTP 服务器回复打印到文件/变量上
- c++ - 使用 CppUTest 问题进行 Yaml-cpp 配置解析器测试“在 '(' 标记之前的预期类型说明符”
- typescript - 使用模块 es6 编译库项目,但仍然支持使用模块 none 的项目
- javascript - Vue 数据绑定在功能组件中不起作用
- php - WordPress:posts_per_page 在存档页面上不起作用
- javascript - 为什么这个 JS 解构赋值方法适用于反向单链表?
- angular - 使用 angular/spring-boot 通过 firebase 生成验证邮件
- ruby-on-rails - 使用 jwt 设计 - 可恢复的 reset_password_by_token