Commenting auto download message media files script

This commit is contained in:
Nur Muhammad
2022-07-01 09:41:02 +08:00
parent 1f29738d0f
commit e1eb461cbb

60
app.js
View File

@@ -70,42 +70,44 @@ client.on('message', msg => {
});
}
// NOTE!
// UNCOMMENT THE SCRIPT BELOW IF YOU WANT TO SAVE THE MESSAGE MEDIA FILES
// Downloading media
if (msg.hasMedia) {
msg.downloadMedia().then(media => {
// To better understanding
// Please look at the console what data we get
console.log(media);
// if (msg.hasMedia) {
// msg.downloadMedia().then(media => {
// // To better understanding
// // Please look at the console what data we get
// console.log(media);
if (media) {
// The folder to store: change as you want!
// Create if not exists
const mediaPath = './downloaded-media/';
// if (media) {
// // The folder to store: change as you want!
// // Create if not exists
// const mediaPath = './downloaded-media/';
if (!fs.existsSync(mediaPath)) {
fs.mkdirSync(mediaPath);
}
// if (!fs.existsSync(mediaPath)) {
// fs.mkdirSync(mediaPath);
// }
// Get the file extension by mime-type
const extension = mime.extension(media.mimetype);
// // Get the file extension by mime-type
// const extension = mime.extension(media.mimetype);
// Filename: change as you want!
// I will use the time for this example
// Why not use media.filename? Because the value is not certain exists
const filename = new Date().getTime();
// // Filename: change as you want!
// // I will use the time for this example
// // Why not use media.filename? Because the value is not certain exists
// const filename = new Date().getTime();
const fullFilename = mediaPath + filename + '.' + extension;
// const fullFilename = mediaPath + filename + '.' + extension;
// Save to file
try {
fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
console.log('File downloaded successfully!', fullFilename);
} catch (err) {
console.log('Failed to save the file:', err);
}
}
});
}
// // Save to file
// try {
// fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
// console.log('File downloaded successfully!', fullFilename);
// } catch (err) {
// console.log('Failed to save the file:', err);
// }
// }
// });
// }
});
client.initialize();