javascript - 为什么这会发送相同的图像?
问题描述
我正在使用 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")
}
}
解决方案
事实证明,每次调用 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>
推荐阅读
- python - 使用 conda 安装包
- ruby-on-rails - Rails 远程表单并重定向到子域
- ruby-on-rails - 在红宝石中运行“铁路服务器”后,我得到了这个错误
- django - 如果它是外键,如何使用过滤器过滤掉数据?
- c# - 我没有使用闭包,但它没有编译为静态方法
- python - 打印时代码给出“输出太大”
- c - 无法从 C 中的文件中读取和打印所有信息
- haskell - 案例表达式中的解析错误 - haskell
- openapi-generator - 用于列表构造的 openapi-generator 的“cpp-restbed-server”生成器的编译错误
- mysql - Insert records in to join table where missing Mysql