首页 > 解决方案 > 为什么这会发送相同的图像?

问题描述

我正在使用 Discord 命令从 Unsplash 的随机图像生成器中获取图像。现在我从https://source.unsplash.com/random中提取了图像。如您所见,每次您访问该 URL 时,它都会拉取一个随机的 Unsplash 图像。但是每当我运行这个简单的 Discord 命令时,它都会发送相同的图像。可能是 Discord 缓存还是我在这里做错了什么?

const { MessageEmbed } = require("discord.js");

module.exports = {
    name: "image",
    aliases: ["random", "img"],
    category: "fun",
    description: "Sends an epic image from unsplash",
    run: async (client, message, args) => {

        const embed = new MessageEmbed()
            .setColor("RANDOM")
            .setImage('https://source.unsplash.com/random/')
            .setTitle(`From Unsplash`)
            .setURL(`https://unsplash.com/`);

        message.channel.send(embed);
        console.log("image run")
    }
}

标签: javascriptnode.jsdiscord.js

解决方案


事实证明,每次调用 api 时,您都需要稍微更改 URL

我的解决方法是提供查询参数..

// Get a hook function
const {useState} = React;

const Example = ({title}) => {
  const [count, setCount] = useState(0);
  const [image, setImage] = useState('https://source.unsplash.com/random');
  
  const getImage = () => {
    
    ///setImage('')
      setTimeout(() => {
      setImage('https://source.unsplash.com/random?count'+ Date.now())
      }, 1000)
    
  }
  
  return (
    <div>
      <button onClick={() => getImage()}>
        Click to change the image
      </button>
      <img src={image} width='400px' height='400px' />
    </div>
  );
};

// Render it
ReactDOM.render(
  <Example title="Example using Hooks:" />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>


推荐阅读