Search code examples
javascriptnode.jsdiscorddiscord.js

I want to make my bot status be shown 24/7


I want to make my bot status be shown 24/7

Here is my code:

client.once('ready', () => {
    console.log('pdm bot is online'); 
        setInterval(() => {
          targetGuild = client.guilds.cache.get('My guild id')
          client.user.setPresence({ 
            activities: [{ name: `${targetGuild.memberCount} Users👥`, type: 'WATCHING' }], 
            status: 'online'
            });
        }, 1000 * 60 * 5);
    
    });

My problem is when I close command prompt the code do not run anymore and my but activity will be empty.


Solution

  • To make your bot run 24/7 you have to keep the node process alive, when you close the terminal the execution of that process is terminated and your bot goes off. You can do it in a couple of ways:

    • Buy a vps online(that stays online 24/7 of course) and start the process there
    • Host it on services like heroku
    • If you have a pc in your house and you want to use it as a server(meaning that you never turn it off) you can use it(not the best idea).

    These are the main ways of hosting a bot, and last but not least for each of these(except for pre-made services of course) you will probably need a process manager, the most famous one is pm2, it basically allows you to run code in background(so you don't need to keep the terminal open) but more important than that it allows you to generate several processes and in case scale your application.

    I haven't said all the things you need to start hosting a bot, but i think this will be something to start from. I guess you now have the basics