首页 > 解决方案 > Discord bot 无法从计算机读取文件,即使它存在

问题描述

我正在我的不和谐机器人上创建一个命令,该命令下载图像,使用 python 对其进行修改,然后将图像作为消息返回。我一切正常,但似乎程序找不到我的文件?

这是代码的摘要版本:

async function downloadAndCreate(url,name,mode)
        {
            const response = await fetch(url);
            const buffer = await response.buffer();
            await fs.writeFile(`./commands/external/${name}`, buffer, () =>
                console.log("Downloaded Image")); 

// Downloads Image from discord url

                var pythonApplication = spawn("python",["./commands/external/create.py",name,mode]);
                pythonApplication.stdout.on('data', function(data) { 
                    fileName = data.toString()
                    return fileName
            });
// Opens python and returns edited file name. Python creates new image in same folder
            
        }

fileName = await downloadAndCreate(messageInfo[0]["url"], image, mode);
setTimeout(function () {
     message.channel.send({ files: [`Y:/WINDOWS/DiscordBot/commands/external/${fileName}`] })}, 2000); 

// Timeout to make sure image is created before sending message

运行代码后,python 使用编辑后的图像创建一个新文件,但我也收到此错误并且没有返回消息:

(node:16472) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'Y:\WINDOWS\DiscordBot\commands\external\{insert image name}'

如果我导航到该路径,我可以看到图像存在并且应用了编辑。为什么 node js 无法读取这个文件,即使它存在于指定的文件夹中?

标签: javascriptnode.jsdiscorddiscord.js

解决方案


推荐阅读