You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.js开发kiss指令时如何调整嵌入消息内GIF图片的尺寸

问题原因

你添加的?size=4096是Discord自有CDN的专用查询参数,对Tenor等第三方站点的外链GIF不生效,所以没有调整效果。


解决方法

方法1:调整Tenor GIF原始尺寸(最简单)

Tenor的GIF链接本身有内置的尺寸标识,结构为https://c.tenor.com/[GIF唯一ID]/[尺寸标识]/[GIF名称].gif,其中尺寸标识对应的尺寸如下:

  • AAAAS:小尺寸,宽高≤200px
  • AAAAM:中尺寸,宽高≤500px
  • 移除该尺寸标识即可获取原始最大尺寸的GIF

你可以根据自己的需求修改所有GIF链接里的尺寸标识,比如要拿最大版的GIF,把你的gifs数组统一调整为:

const gifs = [
    "https://c.tenor.com/F02Ep3b2jJg/cute-kawai.gif",
    "https://c.tenor.com/BjwmxFVGKm0/toloveru-unexpected.gif",
    "https://c.tenor.com/0mdCwkmGD1o/kiss-love.gif",
    "https://c.tenor.com/Hcvab1NgNdk/kiss-anime.gif",
    "https://c.tenor.com/VTvkMN6P648/anime-kiss.gif",
    "https://c.tenor.com/SqpFZQfcyEg/anime-kiss.gif",
    "https://c.tenor.com/OPUTh1nbf7Y/anime-kiss.gif",
    "https://c.tenor.com/3wE3JNW0fsw/anime-kiss-love.gif",
    "https://c.tenor.com/36NbjGxwOT8/anime-kissing.gif"
]

调整后直接把链接传给setImage即可,不需要加额外参数。


方法2:自定义控制GIF显示尺寸(完全可控)

如果需要严格控制GIF的宽高,不受原始资源限制,可以使用sharp库对下载的GIF进行缩放后,作为附件发送再嵌入到消息中:

  1. 先安装依赖:npm install sharp axios
  2. 修改执行逻辑:

注意:需要把execute方法改为异步方法,即async execute(client,msg,args),否则await语法会报错。

const axios = require("axios")
const sharp = require("sharp")
const { MessageEmbed, MessageAttachment } = require("discord.js")

// ......你的原有逻辑......
else{
    const gifs = [/* 你的GIF链接数组 */]
    const gifUrl = gifs[Math.floor(Math.random() * gifs.length)]
    // 下载GIF并调整为宽度400px,高度自适应
    const res = await axios.get(gifUrl, { responseType: 'arraybuffer' })
    const resizedGif = await sharp(res.data, { animated: true })
        .resize({ width: 400 })
        .toBuffer()
    const attachment = new MessageAttachment(resizedGif, 'kiss.gif')
    
    const embed = new MessageEmbed()
        .setTitle(`${author.username} kisses ${user.username}! How cute 🥰`)
        .setImage('attachment://kiss.gif')
        .setColor('BLUE')
    msg.reply({ embeds: [embed], files: [attachment] })
}

内容的提问来源于stack exchange,提问作者dimondflash57

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 19:45:05