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

discord.js开发问题:调用!cat指令返回的嵌入消息猫咪图片不刷新

问题原因
  • 你使用的猫咪图片源URL为固定值,Discord会对相同URL的媒体资源做全局CDN缓存,只要URL不变,无论你多少次重建Embed对象,拉取的都是缓存好的同一张图片,和Embed是否在回调内生成没有关联。
修复方案

给图片URL追加随机无意义的查询参数绕过Discord缓存即可。你使用的猫咪图片接口会忽略未知的查询参数,仍然返回随机猫咪图,同时带不同参数的URL会被Discord识别为不同资源,不会复用历史缓存。这里用当前时间戳作为随机参数即可保证每次请求的URL唯一。

修正后代码
const Discord = require('discord.js');
const client = new Discord.Client();

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {
    console.log(message.content);
    if (message.content === "!cat") {
        // 追加随机时间戳参数绕过缓存
        const randomParam = `?t=${Date.now()}`
        const catEmbed = new Discord.MessageEmbed()
        .setTitle("MEOW!")
        .setImage(`http://theoldreader.com/kittens/600/400${randomParam}`)
        .setThumbnail(`http://theoldreader.com/kittens/600/400${randomParam}`)
        message.channel.send(catEmbed)
    }
});

client.login('NOT GONNA TELL MY TOKEN');

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 23:33:02