How to send a midi file as a Message Attachment - node.js

I made my bot generate a random midi file, I just need to know how to send it onto discord
var write = new MidiWriter.Writer([track]);
message.channel.send(new Discord.Attachment(write.dataUri()))
.catch(console.error);
The error it gives me is:
{ [Error: ENOENT: no such file or directory, stat 'C:\Users\bossay\Desktop\Botz\CropBot\data:audio\midi;base64,TVRoZAAAAAYAAAABAIBNVHJrAAAASgCQA0CEAIADQACQAEAAgABAAJACQIQAgAJAAJADQACAA0AAkANAhACAA0AAkABAggCAAEAAkABAhACAAEAAkAJAggCAAkAA\y8A']
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path:
'C:\\Users\\bossay\\Desktop\\Botz\\CropBot\\data:audio\\midi;base64,TVRoZAAAAAYAAAABAIBNVHJrAAAASgCQA0CEAIADQACQAEAAgABAAJACQIQAgAJAAJADQACAA0AAkANAhACAA0AAkABAggCAAEAAkABAhACAAEAAkAJAggCAAkAA\\y8A' }

I am going to assume you are using the midi-writer-js npm module here.
The Discord.Attachment does not accept a data uri, you want to pass a buffer instead.
(Also to not send a file.jpg you need to set a file name)
To get such a buffer you can pass the Uint8Array retrieved via Writer#buildFile to Buffer.from.
Putting all of it together could look like this.
var write = new MidiWriter.Writer([track]);
var buffer = Buffer.from(write.buildFile())
message.channel.send(new Discord.Attachment(buffer, 'file.midi'))
.catch(console.error);

Related

How to read the contents of a file onto the console using Nodejs

I am going back to learning js after many years off and all i want to do is read the contents of a file onto the console using Nodejs. I found the sample code. Nice and simple. I have spent over an hour trying to figure out why it will not find the file. This is sample right off the documentation and i made it exactly like the example to debug it. The absolute only difference is the name joe is replaced with my user folder.
const fs = require('fs')
fs.readFile('/Users/gendi/test.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
console.log(data)
})
It runs fine except it will not find test.text. no matter what. I receive the following error and no matter how i format the file path. Nothing.
C:\Users\gendi>node readfile.js
[Error: ENOENT: no such file or directory, open 'C:\Users\gendi\test.txt'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\gendi\\pcsSnipe\\test.txt'
}
You can also only pass in the file path as 'test.txt' and the exact same results come up. on the first part of the error msg the path looks formatted correctly but on the last line of the error msg it is not? Its been years.. so i know i am missing something really simple. I assure that file is there!! Thank you in advance and forgive my ineptness.
The fs module requires an exact path to the file you'd like to read. A simple fix to this would be to add __dirname which will return the directory path along with the path of your file.
// Define modules
const fs = require('fs');
const path = require('path');
// Read the file
fs.readFile(path.join(__dirname, '/Users/gendi/test.txt'), 'utf8' , (err, data) => {
if (err) // If FS returned an error
return console.error(err); // Log the error and return
console.log(data); // If the reading was successful, log the data
});
It works if you remove the file extention, '.txt' . Idk why it make a differnce. maybe the "." is throwing it off but it doesn't matter in this respect. Thank you

fs.readFileSync throwing errno 4058

When I try to access the json file iike this:
...
var configurationFile = fileSystem.readFileSync(
"../configurationFile.json"
);
var apiSecretKey = JSON.parse(configurationFile.toString()).secret;
var url = JSON.parse(configurationFile.toString()).db_url;
var dbName = JSON.parse(configurationFile.toString()).db_name;
...
i get this error
Error: ENOENT: no such file or directory, open '../configurationFile.json'
at Object.<anonymous> (C:\Users\Samuel\Documents\GitHub\project-1\Back-End\API\Services\loginservice.js:7:36)
...
at Object.<anonymous> (C:\Users\Samuel\Documents\GitHub\project-1\Back-End\index.js:11:26) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '../configurationFile.json'
this is the dir structure
API
...Services
......loginservice.js
...configurationFile.js
index.js
How do i get around this besides this wrapping the config in a module. Is this a windows specific issue?

Error writing a file using 'fs' in Node

I'm trying to write to a file using the following function:
function writeFile (data, callback) {
var fs = require('fs');
var now = new Date();
fs.writeFile(now.toISOString() + ".json", data, function(err) {
if (err) {
return console.log(err);
} else {
console.log(true);
}
});
}
but im getting an error like this:
{ Error: ENOENT: no such file or directory, open 'C:\Users\Ruslan\WebstormProjects\communication-system\client\6\28\2017_19:47:55.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Me\\WebstormProjects\\blah-blah\\client\\6\\28\\2017_19:47:55.json' }
I'm trying to create a file every time I run the program, but that doesn't seem to work very well because it says file does not exist. Is there anything im doing wrong? BTW, im running this on windows
EDIT: It was indeed wrong file name that was bugging the saving process
When you call fs.writeFile() you have to pass it a filename/path:
Where the parent directory in the path already exists.
Where the path/filename contains only characters that are legal for your OS.
It appears you are likely failing both of these unless you've pre-created the directory: C:\Users\Ruslan\WebstormProjects\communication-system\client\6\28. And, if this is running on Windows, then you also can't use : in a filename.
Assume you actually want the path to be C:\Users\Ruslan\WebstormProjects\communication-system\client and what the filename to be based on your now.toISOString(), the usual work-around is to replace path separators and other invalid filename characters with safe characters to you convert your now.toISOString() to something that is always a safe filename. In this case, you could do this:
// replace forward and back slashes and colons with an underscore
// to make sure this is a legal OS filename
let filename = now.toISOString().replace(/[\/\\:]/g, "_") + ".json";
fs.writeFile(filename, ....)

How to read from a file where the filepath uses backward slashes?

In node.js, if I try to read from a file with backward slashes in the link (using fs module), I get this
Error: EISDIR: illegal operation on a directory, open 'C:\main\temp\config
1\folder\plugin\jquery-3.1.1.min.js'
at Error (native)
errno: -4068,
code: 'EISDIR',
syscall: 'open',
path: 'C:\\main\\temp\\config1\\folder\\plugin\\jquery-3.1.1.min.js' }
node.js code:
fs.readFile('C:\main\temp\config1\folder\plugin\jquery-3.1.1.min.js', function (err, data) {
});
Does anyone know how to fix it?
Thanks
Windows paths are supported by nodejs. You need to escape the backslashes:
fs.readFile('C:\\main\\temp\\config1\\folder\\plugin\\jquery-3.1.1.min.js', function (err, data) {
});

NodeJS: readdir() returns "undefined" instead of the list of files?

I'm trying to check how many files does have a directory using NodeJS's File System.
var fs =require('fs');
function listaArchivos(directorio){
fs.readdir(directorio, function(err, archivos){
if(!err) {
console.log(archivos);
} else {console.log(err)}
})
}
var directorio = 'home/Rosamunda/Desktop/coderhouse/fs/';
listaArchivos(directorio);
I receive this error:
{ [Error: ENOENT, readdir 'home/Rosamunda/Desktop/coderhouse/fs/']
errno: 34,
code: 'ENOENT',
path: 'home/Rosamunda/Desktop/coderhouse/fs/' }
I've tried to search for that ENOENT error, and what I do understand is that the error appears when the path is incorrect, but the path does exist. If I try to print archivos, it returns "undefined".
ENOENT means the path doesn't exist. It looks like you may be missing the / at the beginning of the path (to make it an absolute path).

Resources