I’m a beginner to discord.js, currently I’m trying to develop a bot that will listen to a folder and send any files that are being added to the specific discord server’s specific channel. I’m getting this error when a new file is being added/removed from the folder, I don’t get what’s wrong.
error:
const attachment = new Discord.MessageAttachment(`${folderPath}/${filename}`);
^
TypeError: Cannot read properties of undefined (reading 'MessageAttachment')
at FSWatcher.<anonymous> (C:xampphtdocsxdddbot.js:24:44)
at FSWatcher.emit (node:events:519:28)
at FSWatcher._handle.onchange (node:internal/fs/watchers:215:12)
code:
const { Client, IntentsBitField, Discord, MessageAttachment, } = require('discord.js');
const fs = require('fs');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]
});
const serverId = 'nuhuh';
const channelId = 'nuhuh';
const folderPath = 'uploadedVideos';
client.on('ready', () => {
console.log('The bot is ready');
// Set up file system watcher
fs.watch(folderPath, (eventType, filename) => {
if (eventType === 'rename' && filename) {
// Send the file to Discord
const attachment = new Discord.MessageAttachment(`${folderPath}/${filename}`);
const channel = client.guilds.cache.get(serverId).channels.cache.get(channelId);
if (channel) {
channel.send(attachment)
.then(() => console.log('File sent to Discord'))
.catch(console.error);
} else {
console.error('Channel not found.');
}
}
});
});
client.login("nuhuh");
please help!
I added messageattachment to const { Client, IntentsBitField, Discord, MessageAttachment, } = require(‘discord.js’); but it’s no help
pipipupuuu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.