Discord Bot send message without command

9.6k Views Asked by At

I have some code that runs in the background, 24/7, in JavaScript of course. I'd like to set a discord.js bot to output a message if something (like an errror) happens on the code that is constantly running.

How do you send a message to any channel without listening for a messagge? All the tutorials create commands that rely on COMMAND to make the bot "reply" to you. I want the bot to be the one sending the first message, and reading back my response and acting accordingly.

I'm currently using the discord.js node library (https://discord.js.org/).

Is this possible?

1

There are 1 best solutions below

13
Ori Drori On BEST ANSWER

Get the list of channels from the channel cache by name, and add an event listener for the error event to window. When an error is detected, send a message to the channel:

var client = new Discord.Client();
var channel = client.channels.get('name', nameOfChannel); // or client.channels.get("the channel id")

window.addEventListener('onError', function(e) {
    channel.send(e.error.message) // or client.sendMessage(channel, e.error.message) in older versions;
});