首页 > 解决方案 > 您如何让机器人在嵌入中发布随机 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);
    },
};

标签: javascriptnode.jsdiscorddiscord.js

解决方案


取决于是什么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

这是直接来源。


推荐阅读