Skip to content

Commit

Permalink
Merge pull request #103 from MozillaReality/error-handling
Browse files Browse the repository at this point in the history
Add additional error handling and bot join/leave message filtering
  • Loading branch information
robertlong authored Sep 15, 2020
2 parents 7730895 + fb2baf5 commit 4f9c8f2
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ VERBOSE=false

# If set, Hubs rooms created by the bot with no scene specified will use this scene/GLTF/GLB/bundle URL as the scene.
DEFAULT_SCENE_URL=https://asset-bundles-prod.reticulum.io/rooms/atrium/Atrium.bundle.json

# Sentry error logging.
SENTRY_DSN=""
120 changes: 105 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# hubs-discord-bot (Beta)

### [Go here to add the hosted bot to your server!][invite-page]
### [Go here to add the hosted discord bot to your server!][invite-page]

### [Video introduction](https://www.youtube.com/watch?v=5HtRJolThZ8)
### [Discord Bot Video introduction](https://www.youtube.com/watch?v=5HtRJolThZ8)

**Note: self-hosting the bot and pointing it at production Hubs servers is currently broken. If you want to run the bot as-is, you'll need to also run your own Hubs server. We're trying to fix this.**

A Discord bot that interacts with [Mozilla Hubs](https://hubs.mozilla.com). Mostly bridges information (chat, media links, joins/leaves), lets you see who is currently in Hubs from Discord and sets Hubs permissions and abilities based on Discord roles. Check out the bot in action on our own [Hubs community Discord][hubs-discord]!

* [What it does](#what-it-does)
* [Room/channel permissions linkage](#room-channel-permissions-linkage)
* [Room/channel bridging](#room-channel-bridging)
* [Running the bot](#great-i-want-to-run-this-on-my-discord-server)
* [Permissions](#permissions)
* [Hacking on it](#hacking-on-it)
Discord
- [What it does](#what-it-does)
- [Room/channel permissions linkage](#room-channel-permissions-linkage)
- [Room/channel bridging](#room-channel-bridging)
- [Running the bot](#great-i-want-to-run-this-on-my-discord-server)
- [Permissions](#permissions)
- [Hacking on it](#hacking-on-it)

## What it does

Expand All @@ -36,7 +37,7 @@ Independently of being permission-linked, the bot will detect any Hubs rooms in
- A notification will appear in the Discord channel when someone joins or leaves the Hubs room, or if administrative stuff happens in the Hubs room.
- Text chat and images will be bridged from the Discord channel into the Hubs room.
- Text chat and photos will be bridged from the Hubs room into the Discord channel.
- Links to media (images, videos, models) which are *pinned* in the Hubs room will be bridged to Discord.
- Links to media (images, videos, models) which are _pinned_ in the Hubs room will be bridged to Discord.

Note that you need to set up a webhook for the bot to use in the Discord channel, or it won't be able to post chat from Hubs.

Expand All @@ -50,16 +51,24 @@ Once the bot is running on your server:

1. Give the bot [appropriate permissions](#permissions) on the channels you want it to run in.

2. Create a webhook named "Hubs" in the channels you want it to run in. It will use this webhook to bridge chat and
send Hubs status updates.
2. Create a webhook named "Hubs" in the channels you want it to run in. It will use this webhook to bridge chat and send Hubs status updates.

3. Try out the bot! Type `!hubs` in a channel the bot is in to see all of the ways you can control the bot. Put your favorite Hubs room into a channel topic to start bridging, or use the `!hubs create` command to create a new room.

### Permissions

The bot requires several permissions in order to work:

- "Send messages," "Read messages," and "Embed links" are necessary in order to bridge between the Hubs room that is linked to a channel and the messages that are sent within the channel on Discord.
General Permissions
- Manage Webhooks
- Manage Channels - Grant locally per channel not in Developer Portal
Text Permissions
- Send Messages
- Manage Messages
- Embed Links
- Read Message History

- "Send messages" and "Embed links" are necessary in order to bridge between the Hubs room that is linked to a channel and the messages that are sent within the channel on Discord.
- "Manage webhooks" is necessary in order for the bot to find and use a webhook for bridging chat.
- "Manage channels" is necessary in order for the bot to set the channel topic and bridge chat. **Note:** We do not ask for this permission globally when you add the bot to your server, instead we recommend you grant this permission to the bot in specific groups or channels.
- "Manage messages" and "read message history" are necessary in order for the bot to pin notification messages. Like "manage channels", you should probably grant these for specific groups and channels.
Expand All @@ -80,14 +89,95 @@ To simply run the bot process:

4. [Create a Discord bot on the Discord website.][discord-docs]

5. Create an `.env` file with your bot's API token. Include `RETICULUM_HOST={your server}` and `HUBS_HOSTS={your server}` to point it at your local backend. You can see the different configuration bits you can override in [`.env.defaults`](./.env.defaults). You can also pass these values as environment variables when you run `npm start`.
5. Add redirect URI in the OAuth page and select the bot permissions
- Redirect URI: `https://hubs.local:4000/api/v1/oauth/discord`

6. Create an `.env` file with your bot's API token. Include `RETICULUM_HOST={your server}` and `HUBS_HOSTS={your server}` to point it at your local backend. `RETICULUM_HOST={your server}` should point to 'hubs.local:4000'. You can see the different configuration bits you can override in [`.env.defaults`](./.env.defaults). You can also pass these values as environment variables when you run `npm start`/`npm run local`.

6. Run `npm start` to start the server, connect to Discord and Reticulum, and operate indefinitely.
7. Inside your local reticulum instance in reticulum/config/dev.exs change the configuration for `Ret.DiscordClient` to point to your bot's: `client_id`, `client_secret`, and `bot_token` found inside your discord bot.

7. [Follow the instructions above](#usage) to set up and use the bot on your Discord guild.
8. Run `npm run local` to start the server, connect to Discord and Reticulum, and operate indefinitely.

9. [Follow the instructions above](#usage) to set up and use the bot on your Discord guild.

[npm]: https://nodejs.org/en/
[discord-docs]: https://discordapp.com/developers/docs/intro
[invite-page]: https://hubs.mozilla.com/discord
[hubs-discord]: https://discord.gg/wHmY4nd
[bot-invite]: mailto:[email protected]

## Deploying to hubs.mozilla.com

The Hubs Discord Bot doesn't have a Jenkins job to build it yet. SO we need to build it manually.

### Prerequisites
You'll need the [Habitat CLI](https://www.habitat.sh/docs/install-habitat/) installed locally.

You'll also need access to the Habitat Builder Token. Ask someone for help with that.


### Import the Habitat Builder Keys

Ask someone about getting the private key.

You'll download it and then feed it into Habitat using:

```bash
hab origin key import path/to/mozillareality.sig.key
```

Then for the public key run:

```bash
hab origin key download mozillareality
```

### Building the Habitat Package
In the project directory run:

```bash
HAB_ORIGIN=mozillareality hab pkg build .
```

If everything builds successfully you should see a `/results` folder in the project directory. Take note of the `mozillareality-hubs-discord-bot-0.0.1-<version>-x86_64-linux.hart` file.

We now need to upload that file to the habitat.sh repository.

Run the following command in the project directory:

```
HAB_AUTH_TOKEN="<habitat builder token>" hab pkg upload ./results/mozillareality-hubs-discord-bot-0.0.1-<version>-x86_64-linux.hart
```

You should see a success message. Your uploaded package should be visible at: https://bldr.habitat.sh/#/pkgs/mozillareality/hubs-discord-bot/latest

### Promoting the Habitat Package

This step will promote the package to be live on hubs.mozilla.com

Run this command to promote the package:

```
HAB_AUTH_TOKEN="<habitat builder token>" hab pkg promote mozillareality/hubs-discord-bot/0.0.1/<version> stable
```

To verify the install you can ssh into the box and tail journalctl. To do so run the following command in the `hubs-ops` directory.

```
./bin/ssh.sh discord prod
```

Once logged into the box run `journalctl -f` to tail the logs.

You'll see a bunch of logs saying:

```
Connected to Hubs room
```

Some errors that are caused by users revoking access to the hubs bot or deleting their guild. These are normal.

And finally:
```
Scan finished
```
1 change: 1 addition & 0 deletions habitat/hooks/run
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export TIMEZONE="{{ cfg.general.timezone }}"
export STATSD_HOST="{{ cfg.general.statsd_host }}"
export STATSD_PREFIX="{{ cfg.general.statsd_prefix }}"
export DEFAULT_SCENE_URL="{{ cfg.general.default_scene_url }}"
export SENTRY_DSN="{{ cfg.general.sentry_dsn }}"

cd {{pkg.svc_path}}

Expand Down
4 changes: 2 additions & 2 deletions habitat/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pkg_license=('MPL2')
pkg_description="Discord bot for Hubs by Mozilla"

pkg_deps=(
core/coreutils
core/node/12.9.0
core/coreutils/8.30/20190115012313
core/node/12.9.0/20190827002349
)

pkg_build_deps=(
Expand Down
108 changes: 103 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"local": "NODE_TLS_REJECT_UNAUTHORIZED=0 npm start",
"lint": "eslint src",
"test": "tape tests/*.js"
},
Expand All @@ -18,6 +19,7 @@
"node": ">=8.0.0"
},
"dependencies": {
"@sentry/node": "^5.23.0",
"bufferutil": "^4.0.1",
"discord.js": "^11.5.1",
"dotenv": "^8.2.0",
Expand Down
Loading

0 comments on commit 4f9c8f2

Please sign in to comment.