Discord bot file reading issue - can't get text file integration working

I’m working on a Discord bot that should read from a text file but I keep running into problems. The bot needs to access a local .txt file and process the data but something isn’t working right.

Here’s what I have so far:

const { Client, GatewayIntentBits, Partials } = require('discord.js');
const fs = require('fs');
const bot = new Client({ intents: [GatewayIntentBits.Guilds], partials: [Partials.Channel] });

const authToken = 'hidden_for_security';

bot.on('ready', () => {
  console.log(`Bot is online as ${bot.user.tag}!`);
});

bot.on('messageCreate', async msg => {
  if (msg.content.startsWith('!alert')) {
    try {
      const fileContent = fs.readFileSync('D:\\Projects\\BotData\\alerts.txt', 'utf8');
      const alertList = JSON.parse(fileContent);
      const params = msg.content.split(' ');
      const mentionedUser = params[1];
      const alertMessage = params.slice(2).join(' ');

      const newAlert = {
        username: mentionedUser,
        message: alertMessage
      };

      alertList.push(newAlert);

      fs.writeFileSync('D:\\Projects\\BotData\\alerts.txt', JSON.stringify(alertList, null, 2));

      msg.channel.send(`Alert sent to ${mentionedUser}: ${alertMessage}`);
    } catch (error) {
      console.error(error);
      msg.channel.send('File reading failed.');
    }
  }
});

bot.login(authToken);

I want it to respond in the #general-chat channel. This is just for testing purposes. Not sure if the file format is correct either.

Your file path or initial file state is the problem. Your code expects alerts.txt to have valid JSON, but if it’s empty or doesn’t exist, JSON.parse will crash. Had this exact issue with my notification bot. You need to handle missing or empty files. Wrap your file read in try-catch and start with an empty array if the file’s not there. That absolute path might break things too - use path.join(__dirname, ‘alerts.txt’) instead. The MessageContent intent issue is real, but file initialization is probably what’s killing you. Just create the file manually with inside it to test first.

Had the same problem last month with my bot. Your code looks fine, but check a few things. First, does your alerts.txt file actually exist? Make sure it has valid JSON - even just an empty array [] works. If it’s empty or has broken JSON, JSON.parse will crash. Also verify that file path is correct on your system. I noticed you’re only listening for Guild intents but trying to read message content - you might need MessageContent intent depending on your Discord.js version. Try logging the actual error instead of just console.error(error) so you can see what’s breaking. File format should work fine as long as the JSON is valid.

you’re missing error handling. I had similar issues - usally it’s permissions. Check if node has read/write access to that D: drive location. try fs.existsSync() before reading the file. those hardcoded paths are messy, use relative paths instead. JSON structure looks fine but log what’s in the file before parsing it.