Discord.js v14 – Hug slash command api not connecting correctly to command

huangapple go评论52阅读模式
英文:

Discord.js v14 - Hug slash command api not connecting correctly to command

问题

Here is the translated code portion with the comments removed:

const { EmbedBuilder, SlashCommandBuilder, AttachmentBuilder } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("hug")
    .setDescription("Hug someone.")
    .addUserOption((option) =>
        option.setName("user").setDescription("Select a user")
              .setRequired(true)
    ),

  async execute(client, interaction) {
    const { member, options, user } = interaction;

    let target = options.getUser("user") || user;
    let author = interaction.member;

    let canvas = `https://some-random-api.com/animu/hug`;
  
    let embed = new EmbedBuilder()
      .setImage(canvas)
      .setDescription(`${author} has hugged ${target}!`)
      .setColor('#2B2D31')
      .setTimestamp()
      .setFooter({ text: `Requested by ${member.user.tag}`, iconURL: member.displayAvatarURL() });
  
    await interaction.reply({ embeds: [embed] });
  },
};

If you have any specific questions or need further assistance with this code, please let me know.

英文:

I have a hug slash command that i'm trying to implement using a link from the some-random-api website. Only problem is i'm not sure on how to make it so that the random generated hug gif loaded up is linked to the command.

How can i fix this?

Here is the code i'm using currently:

const { EmbedBuilder, SlashCommandBuilder, AttachmentBuilder } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("hug")
    .setDescription("Hug someone.")
    .addUserOption((option) =>
        option.setName("user").setDescription("Select a user")
              .setRequired(true)
    ),

  async execute(client, interaction) {
    const { member, options, user } = interaction;

    let target = options.getUser("user") || user;
    let author = interaction.member;

    let canvas = `https://some-random-api.com/animu/hug`;
  
    let embed = new EmbedBuilder()
      .setImage(canvas)
      .setDescription(`${author} has hugged ${target}!`)
      .setColor('#2B2D31')
      .setTimestamp()
      .setFooter({ text: `Requested by ${member.user.tag}`, iconURL: member.displayAvatarURL() });
  
    await interaction.reply({ embeds:  });
  },
};

I copy the link i receive from https://some-random-api.com/animu/hug onto the canvas section of my code however it only generates the same gif it printed out and i copied. If i use the link from the actual endpoint, no gif shows up.

答案1

得分: 0

你不能直接使用端点链接来获取gif,你需要执行一个实际的请求,你需要从查询中获取JSON格式,当你访问端点时,你实际上可以看到带有键“link”的JSON。

尝试使用以下代码:

let response = await fetch("https://some-random-api.com/animu/hug");
let dataJSON = await response.json();
let canvas = dataJSON.link;

请注意,你需要安装node-fetch(npm install node-fetch)。

英文:

You can't directly use the endpoint link to fetch the gif, you need to do an actual request, you need to retrieve a JSON format from the query, when you go to the endpoint you can actually see the JSON with the key "link".

try with this :

let response = await fetch("https://some-random-api.com/animu/hug");
let dataJSON = await response.json();
let canvas = dataJSON.link;

Note that you will need node-fetch (npm install node-fetch).

huangapple
  • 本文由 发表于 2023年5月24日 17:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定