您如何让漫游器在嵌入中发布随机gif?

时间:2020-06-25 14:32:32

标签: javascript node.js discord discord.js

我已经看过了,但并没有真正得到有效的答案。我正从仅发布附件(无链接)到尝试将其放入嵌入中。我使用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);
    },
};

1 个答案:

答案 0 :(得分:2)

取决于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]会导致未定义的本质:

String[undefined]

您的gif应该已经是一个字符串,所以您所需要的只是:

embed.setImage(gif);

此外,如果您的链接引用的是某些imgur链接或giphy链接,则很可能它们未链接到实际的gif源。

例如:https://media.giphy.com/media/l396KvvE78gsGhr8c/giphy.gif

当您访问此页面时,它会在gif旁边显示一些gif文字,例如“查看更多狗gifs”,这表明它不是直接来源,要获取直接来源,您可以右键单击gif并按复制网址< / p>

这将导致此链接:https://i.giphy.com/media/l396KvvE78gsGhr8c/giphy.webp

这是直接来源。