嵌入消息 gif 不起作用 |不和谐.js

时间:2021-03-30 20:08:56

标签: javascript node.js discord discord.js bots

机器人发送嵌入消息,但仅发送文本而不发送 gif。我在单独的 .json 文件中有 gif,所以当有人使用命令时会有不同的 gif。但由于某种原因它不起作用。也许是因为 .json 文件?我真的不知道,请帮帮我。 我的代码:

const Discord = require('discord.js');
const prefix = require('../config.json');
const patGif = require('../PATSGIFS.json');

module.exports = {
  name: "pat",
  description: "Pat someone UwU",
  aliases:["pat"],


execute: async (client, message, args) => {
    const gif = patGif[Math.floor(Math.random() * patGif.length)];

    if (!message.mentions.users.first())
    return message.reply("***please mention someone.***");
    
    const embed = new Discord.MessageEmbed()
    .setColor("#FF8DC4 ")
    .setTitle(`*Aww how cute, ${message.author.username} gave ${message.mentions.users.first().username} a pat!*`) 
    .setImage(gif);(String[patGif[gif]])
    message.channel.send({embed})
}} 

.json 文件的代码:

{
    "patgif": "https://i.pinimg.com/originals/15/d0/1e/15d01e231310bb6dabb3af0ae40fc209.gif"
}

(我知道,它只有一个 gif。)

当两个文件都在一个文件夹中时,我在控制台中得到的错误: 图片:https://i.stack.imgur.com/8rzXP.png |我无法从控制台复制它,所以这是一张图片。

这是消息的图片: 图片:https://i.stack.imgur.com/nKGG3.png

不胜感激,谢谢! :)

1 个答案:

答案 0 :(得分:0)

有几个错误。首先,您需要修复路径,它是 require('../PATSGIFS.json')

另一个错误是 JSON 文件包含一个对象,而不是一个数组。从您的代码中,您似乎想从数组中随机选择一个 gif,因此应该是这样的:

[
  "https://i.pinimg.com/originals/15/d0/1e/15d01e231310bb6dabb3af0ae40fc209.gif",
  "https://i.pinimg.com/originals/15/d0/1e/15d01e231310bb6dabb3af0ae40fc209.gif",
  "https://i.pinimg.com/originals/15/d0/1e/15d01e231310bb6dabb3af0ae40fc209.gif",
]

并且您应该按原样发送嵌入内容,而不是在带有 embed 键的对象内发送:

message.channel.send(embed)

完整代码:

const Discord = require('discord.js');
const prefix = require('../config.json');
const patGif = require('./PATSGIFS.json');

module.exports = {
  name: 'pat',
  description: 'Pat someone UwU',
  aliases: ['pat'],
  execute: async (client, message, args) => {
    const gif = patGif[Math.floor(Math.random() * patGif.length)];

    if (!message.mentions.users.first())
      return message.reply('***please mention someone.***');

    const embed = new Discord.MessageEmbed()
      .setColor('#FF8DC4')
      .setTitle(
        `*Aww how cute, ${message.author.username} gave ${
          message.mentions.users.first().username
        } a pat!*`,
      )
      .setImage(gif);

    message.channel.send(embed);
  },
}; 

enter image description here