【问题标题】:discord.js random images displaying incorrectlydiscord.js 随机图像显示不正确
【发布时间】:2019-03-11 13:28:21
【问题描述】:

我想从 url 中获取随机图像,但是当我不和谐地调用命令时,它会一遍又一遍地提供相同的图像。但是当我重新启动机器人并再次运行它时,图像发生了变化,但仍然一遍又一遍地播放不同的图像,没有任何变化?我将非常感谢任何帮助

else if (message.content.startsWith(prefix + "randimg") && message.author != client.user) {
  message.channel.send({embed: {
      color: 0x0000FF,
      author: {
          name: client.user.username,
          icon_url: client.user.avatarURL
      },
      title: "Random Image Test",
      image: {
          url: "https://source.unsplash.com/random",
      },
      description: "Random Image Generation"
  },
  currentTimestamp: new Date(),
  footer: {
    icon_url: client.user.avatarURL,
    text: "2018 @ Canarado"
  }
})

我想也许是因为它一遍又一遍地从同一个随机 img url 中抓取它们,但我不知道如何去改变它。谢谢你,我希望有人能提供帮助。

【问题讨论】:

  • 您的语法没有问题。也许有一种不同的方式来使用 unsplash url。
  • 是的,我只是不知道。是否有一些 javascript 方法可以每隔 x 秒将 url 更改为新的随机 unsplash url? @弗罗斯特
  • 如果您之前在变量中声明了该 message.channel.send 调用的所有内容,这可能会有所帮助(并在调用中传递变量)吗?
  • 不幸的是,这没用@chackerian
  • 也许这种类型的东西可能会帮助你,不确定。它找到链接的重定向 url。 redirectdetective.com 这是代码:redirectdetective.com/ajax.js

标签: javascript node.js discord discord.js


【解决方案1】:

缓存有可能是根本原因吗?您是否尝试在您的网址中添加随机参数?

image: {
      url: "https://source.unsplash.com/random" + "?rnd=" + Math.random(),
  },

【讨论】:

  • 是的!只是没有正确格式化网址,但您确实帮助解决了这个问题,所以谢谢!
【解决方案2】:

所以经过细致的研究,问题确实是缓存,但其他可用的答案(虽然它很好并且帮助我思考)没有正确格式化链接。我发现这是随机 unsplash 链接的正确格式。

          image: {
          url: "https://source.unsplash.com/random?sig=" + Math.random(),
      },

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2020-08-10
    • 2020-12-25
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    相关资源
    最近更新 更多