【问题标题】:how to get discord.js to pick a random image from file如何让 discord.js 从文件中选择随机图像
【发布时间】:2020-06-01 00:18:47
【问题描述】:

我在另一个泡菜中,我在过去一周意识到,由于链接已过期,我的图像没有加载,所以我想了解如何在代码中使用文件目录。

这是我尝试过的:

});
client.on('message', message => {
if (message.content.startsWith('L!hug')) {
var fs = require('fs');
var files = fs.readdirSync('C:\Users\nevbw\Desktop\games\FBIBot\images\hugs')
/* now files is an Array of the name of the files in the folder and you can pick a random name inside of that array */
let chosenFile = files[Math.floor(Math.random() * files.length)] 
    }
});

});
client.on('message', message => {
if (message.content.startsWith('L!hug')) {
const path = 'C:\Users\nevbw\Desktop\games\FBIBot\images\hugs';
const fs = require('fs');

fs.readdirSync(path).forEach(file => {
ranfile = Math.floor(Math.random() * file.length);
message.channel.sendFile(ranfile);
})
}
});

【问题讨论】:

标签: javascript discord discord.js


【解决方案1】:

乐于助人。

您以错误的方式使用 FS。这就是它的样子:D 还有一些关于它的文档 (https://nodejs.org/dist/latest-v13.x/docs/api/fs.html)。

-- 代码--

也只是一个提示!我看到您正在使用完整目录,这非常重要(例如,如果您更改用户名、驱动器 ID 等),因此在 fs 中,只要图像位于同一文件夹中,您就可以执行 ./(ImageName),或者如果它是在同一个文件夹中,但在另一个说 /FBIBot/Images 下,您可以执行 ./Images/(ImageName)。 ^^

--

错误是什么:(不幸的是,我无法测试它,但我 99% 肯定)。

当你本应使用fs.readfilesync(path).forEach(file => {时,你使用了fs.readdirSync(path).forEach(file => {

-- 第一个代码--

});

client.on('message', message => {
if (message.content.startsWith('L!hug')) {
var fs = require('fs');
var files = fs.readfileSync('C:\Users\nevbw\Desktop\games\FBIBot\images\hugs')
/* now files is an Array of the name of the files in the folder and you can pick a random name inside of that array */
let chosenFile = files[Math.floor(Math.random() * files.length)] 
    }
});

-- 第二个密码--

});
client.on('message', message => {
if (message.content.startsWith('L!hug')) {
var fs = require('fs');
var files = fs.readFileSync('C:\Users\nevbw\Desktop\games\FBIBot\images\hugs')
/* now files is an Array of the name of the files in the folder and you can pick a random name inside of that array */
let chosenFile = files[Math.floor(Math.random() * files.length)] 
    }
});

^^

【讨论】:

  • hi dr space 不幸的是,这些方法都不成功,这两个代码都出现了这个错误
  • 两个代码internal/fs/utils.js:227 throw err; ^Error: ENOENT: no such file or directory, open 'C:Users evbwDesktopgamesFBIBotimageshugs' at Object.openSync (fs.js:446:3) at Object.readFileSync (fs.js:348:35) at Client.<anonymous>
  • 啊,一切顺利!我说你可以做 ./Folder/ImageName 作为我可能错了的 FS 目录。如果脚本与您的图像位于同一文件夹中,请尝试执行 ./Imagename.让我知道它是怎么回事:D
  • 那还会做随机成像还是只提取一张图像?
  • 抱歉久等了!试一试。对不起任何凌乱的代码。我必须快速编写代码。 (是的,这包括最后一次没有的随机成像。这是从代码 2 中获取的。)github.com/MrDrSpace/TheCoolCodingCave 让我知道这是怎么回事!
【解决方案2】:

通过搜索和搜索发现但找到了修改它的答案,我希望人们在以后的参考中使用它! const num = (Math.floor(Math.random()* 5)+1).toString(); message.channel.send({files: [`./slap/slap${num}.gif`]})

【讨论】:

    【解决方案3】:

    使用 fs.readdirSync('./images/') 而不是 fs.readFileSync('./images/') 更容易,但是您必须在 VSC 内创建文件夹并将图像放入文件夹中,您也可以将图像拖放到解决方案中并使用:

    var files = fs.readdirSync(`./images/`).filter(file => file.endsWith('.png'))
    

    所以当它寻找图像时,它不会选择其他任何东西。希望它对某些人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 2021-10-05
      • 2019-12-13
      • 1970-01-01
      • 2012-09-03
      • 2012-06-13
      • 2013-03-27
      • 2023-03-12
      相关资源
      最近更新 更多