【问题标题】:Random image generator not working for discord bot随机图像生成器不适用于不和谐机器人
【发布时间】:2018-01-04 23:53:58
【问题描述】:

我已经摆弄了它,但仍然无法让它工作。我也尝试过使用单一图像并且它没有问题。这是我的代码:

exports.run = (client, message, args) => {
    var rands = ["./catboy/cb1.jpg","./catboy/cb2.jpg","./catboy/cb3.jpg","./catboy/cb4.jpg"];
    var rand = [Math.floor(Math.random()*(rands.length))];
    message.channel.send("rawr", rand);
}

【问题讨论】:

  • 你能解释一下到底是什么问题吗?什么不起作用?
  • 它说的是我输入的信息,但它没有发送图片。

标签: javascript image random discord


【解决方案1】:

用途:

var rand = Math.floor(Math.random() * rands.length);

比发送图片使用

message.channel.send("rawr", rands[rand]);

使用更好的变量命名的示例:

var images = ["./catboy/cb1.jpg","./catboy/cb2.jpg","./catboy/cb3.jpg","./catboy/cb4.jpg"];
var rand = Math.floor(Math.random() * images.length);
var randomImage = images[rand];

console.log( randomImage ) // The one to send
// message.channel.send("rawr", randomImage);

【讨论】:

  • 对我来说似乎没有任何改变这可能与我的文件有关吗?
  • @Andrew 也许你的路径是错误的./ 我不知道。该示例现在按预期工作 - 您需要检查控制台中的错误。
  • 在控制台中显示“./catboy/cb3.jpg”,但不和谐中没有弹出图片
  • @Andrew 你取消了// message.channel.send("rawr", randomImage); 的注释吗?将这 5 行放在您的 exports.run 中并取消注释最后一行。
  • 这样吗? exports.run = (client, message, args) => { var images = ["./catboy/cb1.jpg","./catboy/cb2.jpg","./catboy/cb3.jpg","./catboy/cb4.jpg"]; var rand = Math.floor(Math.random() * images.length); var randomImage = images[rand]; console.log( randomImage ) message.channel.send("rawr", randomImage); }
猜你喜欢
  • 1970-01-01
  • 2023-02-14
  • 2019-06-08
  • 2021-03-11
  • 1970-01-01
  • 2018-03-20
  • 2021-06-27
  • 2021-04-26
  • 1970-01-01
相关资源
最近更新 更多