Creating a sleep mode for Discord.js bot

I’m working on a Discord bot and want to add a sleep mode feature. When active, the bot should stay online but not respond to messages. Here’s what I’ve tried:

const config = JSON.parse(fs.readFileSync('config.json'));

if (config.sleepMode) {
  botClient.user.setStatus('idle');
  botClient.user.setActivity('Zzz... Sleep mode');
  console.log('Bot is sleeping');
  return;
} else {
  botClient.user.setStatus('online');
  botClient.user.setActivity(null);
  console.log('Bot is awake');
}

I put this code in the botClient.once('ready', () => {}) event, but I’m getting an error saying it can’t read the ‘setStatus’ property of null.

How can I make the bot ignore all messages and show as idle when sleep mode is on? Any help would be great!

I’ve implemented a similar sleep mode feature in my Discord bot, and I can share what worked for me. Instead of using the ‘ready’ event, I found it more reliable to create a separate command to toggle sleep mode. Here’s a simplified version:

let sleepMode = false;

client.on('messageCreate', message => {
  if (message.content === '!sleep') {
    sleepMode = !sleepMode;
    if (sleepMode) {
      client.user.setStatus('idle');
      client.user.setActivity('Sleeping');
      message.channel.send('Sleep mode activated');
    } else {
      client.user.setStatus('online');
      client.user.setActivity(null);
      message.channel.send('Sleep mode deactivated');
    }
  }

  if (sleepMode && !message.content.startsWith('!sleep')) return;

  // Rest of your message handling code
});

This approach avoids file I/O operations on every message and gives you more control over when to toggle sleep mode. You can expand on this basic idea to fit your specific needs.

Your approach is on the right track, but the error suggests the client isn’t fully initialized when you’re trying to set the status. Try moving that code into a separate function that you call after the bot is fully ready.

Here’s a suggestion:

botClient.once('ready', () => {
  console.log('Bot is ready');
  updateSleepMode();
});

function updateSleepMode() {
  const config = JSON.parse(fs.readFileSync('config.json'));
  if (config.sleepMode) {
    botClient.user.setStatus('idle');
    botClient.user.setActivity('Zzz... Sleep mode');
    console.log('Bot is sleeping');
  } else {
    botClient.user.setStatus('online');
    botClient.user.setActivity(null);
    console.log('Bot is awake');
  }
}

To ignore messages when sleep mode is on, you can add a check in your message event handler:

botClient.on('messageCreate', (message) => {
  if (JSON.parse(fs.readFileSync('config.json')).sleepMode) return;
  // Your regular message handling code here
});

This should give you a working sleep mode. Remember to handle potential file read errors in a production environment.

yo, i had similar issue b4. try using a global var for sleep mode instead of reading config file every time. like this:

let isSleeping = false;

client.on(‘messageCreate’, msg => {
if (isSleeping && !msg.content.startsWith(‘!wake’)) return;
// rest of ur code
});

then make commands to toggle it. works gr8 for me!