Discourse Discord Bot 🤖

This is a discord bot that lets you sync your Discord server membership with your Discourse users.

image

Purpose

This plugin does two things:

  1. Helps you sync membership of your Discord server with your Discourse instance.

  2. Fundamentally, allows you to power a Discord bot using your Discourse server to do stuff to link the two systems.

Bot Commands

There are three commands:

  1. Ping!
  2. !disckick
  3. !discsync

Ping!

To check if the bot is responding, just type “Ping!” and the bot should respond with “:robot: : Pong!” - marvellous, eh? :smiley: (this is a bit of a meme btw!)

!disckick <min_trust_level: default 2>

Typing this will kick any user that exists on your Discourse but at a Trust Level below your supplied value. The default if you don’t supply a value is 2.

e.g.:

!discsync <clean up: default false, min_visibility: default 0, include automated groups: default false>

This command will attempt to copy over all groups into Roles on Discord which match provided criteria (or defaults if not provided). It will then attempt to populate the roles as per the Group membership on Discourse. It’s a one way sync only. No Discourse data is ever changed.

Setup Instructions

Prerequisites

  1. a Discord server on which you are Admin
  2. a Discord App (see below)
  3. a Discord Bot (see below)
  4. ssh root access to your Discourse server
  5. updates to app.yml to
    • install libsodium, a dependency (see below)
    • install the plugin
  6. All users of your Discourse will need to login using Discord OAuth login that will appear on your login page once the plugin is installed.

Discord App

Go here and create an app:

Hit “New Application”

You will need to create a Bot and it will look something like this (leave it not ‘public’):

You will need to authorise the bot in the browser, see https://discordapp.com/developers/docs/topics/oauth2#bot-authorization-flow

Once the bot is set up, copy the Token. You will need to enter that into Discourse later.

app.yml changes

plugin

Add this plugin to your app.yml in the same way you normally install a plugin.

libsodium

This library is required for the discordrb gem, installed by plugin.rb, to work.

hooks:
    before_code:
       - exec:
              cmd:
                - apt-get update
                - apt-get install -y libsodium-dev

This goes just above where you specify your plugins. The hooks statement should already exist. You need to add the plugins to the plugin section directly beneath this.

Site Settings

Once in your Discourse admin area, go to Plugin Settings and fill these settings:

image

You will get the IDs from Discord interface. You will need to activate Developer Mode to allow you to copy these IDs. In Discord, go to your Settings - > Appearance - > ADVANCED and enable Developer Mode:

You can then get ID’s on from the interface, e.g.:

image

You will also need to fill in the Discord OAuth Settings. The Client ID and Secret are from your very same Discord App.

image

Rebuild to connect

Once you’ve set up your app.yml and your site settings, go back to your root prompt in linux and type:

./launcher rebuild app

If you’ve set up your Discord and Discourse servers correctly, you should see the bot join the server.

If ever the bot falls over (it will go offline) you will need to do the same thing again to bring it back. Improved independence of the bot from the Discourse core server is a to do. That said, I’ve run a bot in Production for a long while and it’s yet to fall over.

Future commands?

If you have an idea for another command that leverages the link between Discourse and Discord that you believe would be useful to the wider community let me know in the replies and we can look at the potential to implement that. NB this bot is not intended to perform tasks outside the scope of Discourse.