Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/moonstar-x/discord-downtime-notifier

A discord bot that notifies a server when a bot has gone offline. Useful for self-hosted bots.
https://github.com/moonstar-x/discord-downtime-notifier

discord discord-bot discord-downtime-notifier downtime notifier

Last synced: 3 months ago
JSON representation

A discord bot that notifies a server when a bot has gone offline. Useful for self-hosted bots.

Awesome Lists containing this project

README

        

[![discord](https://img.shields.io/discord/730998659008823296.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/mhj3Zsv)
[![ci-build-status](https://img.shields.io/github/workflow/status/moonstar-x/discord-downtime-notifier/CI?logo=github)](https://github.com/moonstar-x/discord-downtime-notifier)
[![open-issues-count](https://img.shields.io/github/issues-raw/moonstar-x/discord-downtime-notifier?logo=github)](https://github.com/moonstar-x/discord-downtime-notifier)
[![docker-image-size](https://img.shields.io/docker/image-size/moonstarx/discord-downtime-notifier?logo=docker)](https://hub.docker.com/repository/docker/moonstarx/discord-downtime-notifier)
[![docker-pulls](https://img.shields.io/docker/pulls/moonstarx/discord-downtime-notifier?logo=docker)](https://hub.docker.com/repository/docker/moonstarx/discord-downtime-notifier)

# Discord Downtime Notifier Bot

This bot can notify a server through a message when other bots go offline. It verifies when a bot has changed its presence status to `offline`. The main use for this is to let users know when a bot becomes unusable. It also lets users know once the bot has gone back online and for how long it's been offline for.

This is mostly useful for server owners with self-hosted bots that need a way of getting notified when any one of their bots go offline. Ideally, this bot should be hosted on a high uptime service such as [Heroku](https://www.heroku.com/).

## Pre-Requirements

You will need to have a bot account on Discord. For this, head over to the [developer portal](https://discord.com/developers/applications) and create an application. Inside the **Bot** tab, you need to enable the following **Privileged Gateway Intents**:

- `PRESENCE INTENT`
- `SERVER MEMBERS INTENT`

This is required because this bot depends on events like `presenceUpdate` and `guildMemberRemove` to function properly. You can enable these intents without the need to apply if your bot is in under 100 servers and/or is not verified. If your bot is verified, you will need to go through a whitelisting process where you'll need to justify the need for this information.

## Requirements

You can self-host this bot or deploy it on a service like [Heroku](https://www.heroku.com/) for example. If you do decide to self-host, you'll need to have the following dependencies:

* [git](https://git-scm.com/)
* [node.js](https://nodejs.org/en/) (Version 12.0 or higher.)
* [MongoDB](https://www.mongodb.com/)

## Installation

### Self-Hosting

In order to self-host this bot, you'll need to clone this repository.

```text
git clone https://github.com/moonstar-x/discord-downtime-notifier.git
```

Install the dependencies with:

```text
npm ci --only=prod
```

Or, if you want to also install the `devDependencies`:

```text
npm install
```

After you have [configured](#configuration) the bot, you can run it with:

```text
npm start
```

### Deploying to Heroku

To deploy to Heroku, you can click on the image below and login to your account.

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/moonstar-x/discord-downtime-notifier)

You can now go back to your app's *Overview*, make sure you disable the *web* dyno and enable the *bot* dyno. Your bot should now be up and running. Remember you can always check your bot's console if you access the *View Logs* in the *More* dropdown menu.

### Running on Docker

You can start a container with the bot's image by running:

```text
docker run -it -e DISCORD_TOKEN="YOUR DISCORD TOKEN" -e DISCORD_MONGO_URI="YOUR MONGO URI" moonstarx/discord-downtime-notifier:latest
```

Check [configuration](#configuration) to see which environment variables you can use.

The following volumes can be used:

- `/opt/app/config`: The config folder for the bot, here you can use the `settings.json` file to configure the bot if you don't want to use environment variables.

#### Docker Compose Example

You can inspire yourself from the following `docker-compose.yml` file:

```yaml
version: "3.9"

services:
db:
image: mongo:latest
volumes:
- ./data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
environment:
- MONGO_INITDB_ROOT_USERNAME=ADMIN_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=ADMIN_PASSWORD

bot:
image: moonstarx/discord-downtime-notifier:latest
depends_on:
- db
environment:
- DISCORD_TOKEN=YOUR_DISCORD_TOKEN
- DISCORD_PREFIX=d!
- DISCORD_MONGO_USERNAME=user
- DISCORD_MONGO_PASSWORD=password
- DISCORD_MONGO_DATABASE=database
- DISCORD_MONGO_HOST=db:27017
- DISCORD_OWNER_ID=123123123
- DISCORD_OWNER_REPORTING=false
- DISCORD_BROADCAST_REPORTING=true
- DISCORD_PRESENCE_REFRESH_INTERVAL=null
```

The `mongo-init.js` file in the `mongo` service is the DB initialization file. This runs only once when the container is first created (and no data is located in `/data/db`). This file creates the user and the database. You may inspire yourself from the following one which creates the user `user` and the database `database` which are shown in the `docker-compose.yml` file example.

```js
db = db.getSiblingDB('database');

db.createUser({
user: 'user',
pwd: 'password',
roles: [
{
role: 'readWrite',
db: 'database'
}
]
});
```

### Configuration

Inside the `config` folder, rename the file `settings.json.example` to `settings.json` and edit the file with your own Discord Token and other settings. If you don't have a Discord token yet, you can see a guide on how to create it [here](https://github.com/moonstar-x/discord-downtime-notifier/wiki).

Your file should look like this.

```json
{
"token": "YOUR_DISCORD_TOKEN",
"prefix": "$",
"mongo_username": "user",
"mongo_password": "pass",
"mongo_host": "localhost:27017",
"mongo_database": "mydb",
"mongo_uri": null,
"owner_id": "123123123",
"owner_reporting": false,
"broadcast_reporting": false,
"presence_refresh_interval": 900000
}
```

You may also configure these options with environment variables. The settings set with the environment variables will take higher precedence than the ones in the config JSON file.

This table contains all the configuration settings you may specify with both environment variables and the JSON config file.

| Environment Variable | JSON Property | Required | Type | Description |
|-----------------------------------|-----------------------------|-----------------------------|--------------------|-------------|
| DISCORD_TOKEN | `token` | Yes. | `string` | The bot's token.
| DISCORD_PREFIX | `prefix` | No. (Defaults to: `$`) | `string` | The bot's prefix. Used for the commands.
| DISCORD_MONGO_USERNAME | `mongo_username` | No. (Defaults to: `null`) | `string` or `null` | The username of the mongoDB database. If you set this to `null` (or omit this option) you should fill out the MONGO_URI setting.
| DISCORD_MONGO_PASSWORD | `mongo_password` | No. (Defaults to: `null`) | `string` or `null` | The password of the mongoDB database. If you set this to `null` (or omit this option) you should fill out the MONGO_URI setting.
| DISCORD_MONGO_HOST | `mongo_host`  | No. (Defaults to: `null`) | `string` or `null` | The host of the mongoDB database. This includes the IP and port. If you set this to `null` (or omit this option) you should fill out the MONGO_URI setting.
| DISCORD_MONGO_DATABASE | `mongo_database` | No. (Defaults to: `null`) | `string` or `null` | The database of the mongoDB database. If you set this to `null` (or omit this option) you should fill out the MONGO_URI setting.
| DISCORD_MONGO_URI | `mongo_uri` | No. (Defaults to: `null`) | `string` or `null` | The URI of the mongoDB database. You can set this to `null` (or omit this option) if you have added all the other mongo settings. This should have the shape: `mongodb://user:pass@host:port/database`.
| DISCORD_OWNER_ID | `owner_id` | No. (Defaults to: `null`) | `string` or `null` | The ID of the bot's owner.
| DISCORD_OWNER_REPORTING | `owner_reporting` | No. (Defaults to: `false`) | `boolean` | Whether the bot should send error reports to the owner via DM when a command errors.
| DISCORD_BROADCAST_REPORTING | `broadcast_reporting` | No. (Defaults to: `false`) | `boolean` | Whether the bot should send error reports to the guild owner when the status broadcast errors.
| DISCORD_PRESENCE_REFRESH_INTERVAL | `presence_refresh_interval` | No. (Defaults to: `900000`) | `number` or `null` | The time interval in ms in which the bot updates its presence. If set to `null` the presence auto update will be disabled.

> **Note on `Required`**: A required setting HAS to be in the JSON or environment variables.
>
> **Note on `Defaults`**: If a setting is missing from the JSON or environment variables, the default value takes place.
>
> **Note on the mongo settings**: If you specify `MONGO_URI` you don't need to specify `MONGO_USERNAME`, `MONGO_PASSWORD`, `MONGO_HOST` and `MONGO_DATABASE`.
> This setting is mostly for when you have a mongo instance that requires you to have control over the connection URI.

## Usage

Here's a list of all the commands for the bot.

| Command | Alias | Required Permissions | Description |
|-----------------------------|--------------------|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| d!add | d!a | `MANAGE_GUILD` | Add a new bot to the tracking list. You must mention a bot. It will not track regular users. |
| d!channel | d!c | `MANAGE_GUILD` | Update the channel where the broadcasting messages will be posted. The channel mentioned must be either a `text` channel or a `news` channel. If you don't mention any channel, the bot will reply with the currently set channel. |
| d!list | d!l | `MANAGE_GUILD` | List all the bots currently being tracked. |
| d!remove | d!r, d!delete, d!d | `MANAGE_GUILD` | Remove a bot from the tracking list. You must mention a bot that is currently being tracked. |
| d!help | d!h |  | Display a help message with all the available commands. |

> Replace `d!` with your own prefix (in case you're self-hosting the bot).

## Add this bot to your server

You can add this bot to your server by clicking in the image below:
[![Add this bot to your server](https://i.imgur.com/EJM2CM0.png)](https://discordapp.com/oauth2/authorize?client_id=514136165138563073&scope=bot&permissions=2048)

**The bot's prefix for this instance is: `d!`**

## Author

This bot was made by [moonstar-x](https://github.com/moonstar-x).