javascript - 您如何让机器人在嵌入中发布随机 gif?
问题描述
我看过并没有真正得到有效的答案。我正在从仅发布附件(无链接)转变为尝试将其放入嵌入中。我使用 discord.js.org 帮助我想出了下面的代码,但是当我使用该命令时,它最终只是一个完全空白的嵌入,只是一个小方块。在 gif 不会加载的地方甚至不够大。这个广场很小。我不确定它为什么这样做。
const Discord = require('discord.js');
const prefix = require('../config.json');
const angryGif = require('../AngryWolves.json');
const colors = require('../colors.json');
module.exports = {
name: "angry",
description: "Posts a random GIF of an angry wolf.",
usage: `${prefix}angry`,
execute(message, args) {
const gif = new Discord.MessageAttachment(angryGif[Math.floor(Math.random() * angryGif.length)]);
const embed = new Discord.MessageEmbed()
.setColor(colors.blue)
.setImage(String[angryGif[gif]])
message.channel.send(embed);
},
};
解决方案
取决于是什么angryGifs
,它是字符串 url 的数组吗?对象列表?
假设angryGifs
是 gif 的字符串 url 列表:
第一的:
const gif = new Discord.MessageAttachment(angryGif[Math.floor(Math.random() * angryGif.length)])
这将制作 gif 的消息附件,它不会在嵌入中
所以改为:
const gif = angryGif[Math.floor(Math.random() * angryGif.length)];
第二:.setImage(String[angryGif[gif]])
在这里,您从字符串中获取属性名称为 的属性angryGif[gif]
,angryGif[gif]
这将导致 undefined 如此重要:
String[undefined]
你的 gif 应该已经是一个字符串,所以你只需要:
embed.setImage(gif);
此外,如果您的链接指的是某些 imgur 链接或 giphy 链接,则它们可能没有链接到实际的 gif 源。
例如:https ://media.giphy.com/media/l396KvvE78gsGhr8c/giphy.gif
当您访问此页面时,它会显示 gif 以及其他一些文本,例如“查看更多狗 gif”,这表明它不是直接来源,要获取直接来源,您可以右键单击 gif 并按复制 url
这将导致此链接:https ://i.giphy.com/media/l396KvvE78gsGhr8c/giphy.webp
这是直接来源。
推荐阅读
- c# - VSTS 构建:角色不断回收,因为它们找不到有效的 DLL
- java - 如何将以下代码转换为lamba?
- android - 如何在putExtra中添加多个字符串
- sql-server - T-SQL 处理无结果
- c# - UserDialog 进度条不显示
- php - 重写查询字符串以允许 wordpress 中单个自定义帖子类型的多个 URL
- c++ - 带有 std::quoted 的 Visual Studio 中的错误
- r - RandomForest没有计算OOB错误
- ruby-on-rails - 2 分钟 Rails/AngularJS 后 SSO/SAML 会话超时
- html - 填充整页 div,每边减去填充