0

So I'm new to making bots and coding and stuff and this might be a newbie question but anyway, I don't have a hosting server for my bot so i was thinking that maybe when the bot turned off it would send a message in a channel But I have no clue how to do that or if its possible. I was able to get it to happen when the bot turns on but I don't know how to have it occur when the bot turns off.

I don't even know where to start. What I have so far is when I type node . in the terminal the bot turns on, tells the console log and then tells the server. But I don't know how to do it for the bot turning off.

bot.on('ready', () =>{
bot.channels.get("488916182406004747").send("The cloud has lifted");

})

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You could make it happen only if the bot shuts down for a given reason. Like, for example, if you issue it a shutdown command. But if it happened for another reason, like a crash, or your power going out, it wouldn't send the message. – MegaEmailman Jun 06 '19 at 06:17
  • It depends how the bot is launched You may be able to catch a crash on the system side and send a request to a webhook url you created so the webhook will send a message in the channel But it depends on how / why the bot goes offline. If it's because of a command, as @MegaEmailman said, you can send a message. You can also kill it the hard way and handle it with (look for [this question](https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits)) – JackRed Jun 06 '19 at 08:07

1 Answers1

2

Shutdown Command

As mentioned in the comments, you could create a simple command to shut down your client, which would send the desired message.

Example:

// Async context required to use keyword 'await'

try {
  await message.channel.send('Goodbye');    // Send the message

  await message.delete();                   // Delete the command message
  await client.user.setStatus('invisible'); // Mark the client as offline (instead of waiting)
  await client.destroy();                   // Remove the client instance
} catch(err) {
  console.error(err);                       // Return any errors from rejected promises
}

Webhook

If you wanted the message to be sent when your Node process exits in general for any reason (i.e. client logs out, computer is turned off, CTRL + C, etc.), you could create a Webhook and use it in the process's beforeExit event.

To create the Webhook, navigate to the options of the channel you want the messages to be sent in. Go to Webhooks, click Create Webhook, customize the options, copy the Webhook URL, and then click Save. In the URL, the ID is the first group of digits only after /webhooks/, and the token follows the next /.

Example:

const { id, token } = require('./webhook.json'); // json file with Webhook's ID and token 
// const Discord = require('discord.js');

// const client = new Discord.Client();
const webhook = new Discord.WebhookClient(id, token);

process.once('beforeExit', () => {
  webhook.send(`${client.user} has gone offline.`)
    .catch(console.error);
});

Bonus: You could also use the Webhook in the ready event to send the 'ready' message.

client.once('ready', () => {
  webhook.send(`${client.user} is now online.`)
    .catch(console.error);
});

EDIT:

The beforeExit event is not emitted when the Node.js process exits unexpectedly, like when you close the console window. Therefore the only scenarios where you'd find the Webhook option helpful is when your client crashes or you use CTRL/CMD + C in the console window.

My suggestion would be to stick to a shutdown command. To keep your bot running without proper hosting, you could try a process manager or creating a Windows Service.

slothiful
  • 5,548
  • 3
  • 12
  • 36
  • I'm so sorry but I couldn't get it to work I was able to get the 'ready' message to work, but not the 'beforeExit' – Kearston Burnett Jun 06 '19 at 20:09
  • Do you mean with the Webhook? Are there any errors? What happens? – slothiful Jun 06 '19 at 20:11
  • Well there isn't any errors. It just doesnt send anything. This is what it looks like for me const { id, webtoken } = require('./webhook.json'); const webhook = new Discord.WebhookClient(id, webtoken); process.once('beforeExit', () => { webhook.send(${bot.user} has gone offline.) .catch(console.error); }); – Kearston Burnett Jun 06 '19 at 20:15
  • My apologies; after some of my own testing, `beforeExit` isn't emitted when forcefully stopping the Node.js process. Your message will still be sent when the bot crashes, but not if you close the command prompt window. My suggestion is to stick to the shutdown command. – slothiful Jun 06 '19 at 20:23