为什么会发送相同的图像?

时间:2020-03-25 04:12:34

标签: javascript node.js discord.js

我正在处理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")
    }
}

1 个答案:

答案 0 :(得分:2)

事实证明,每次调用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>