"Discord Bot": run one on your Discourse server & keep things in sync

Purpose of this plugin

This plugin does two things:

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

  2. Currently supports two commands which help sync membership of your Discord server with your Discourse instance.

    image
    these aren’t the two sync commands, just an illustration of bot interaction that is possible. the second command in the screenshot was deprecated during development :wink:

It is necessarily complex to set up, but great for fans of both systems who are keen to put the time in to understand how each system works, at least on a functional and conceptual level. You need to understand some of the fundamentals of how Discourse and Discord user management works to get the best out of this plugin.

State of this plugin

This is a complex plugin to set-up. That was pretty unavoidable.

However, it’s child’s-play to use once it’s all up and running.

Consider the code as currently in Beta. It’s been tested, even in Production, but likely to contain bugs. I recommend testing this first on a fresh Discord server before applying it to your “Production” main Discord server, unless you are just starting out.

Any bugs are much more likely to upset your Discord instance than your Discourse instance, because there are no changes made to Discourse. However, I’ve written it in a way that means most things are recoverable and you can merely repeat something to fix an issue usually. Discourse is used as a master for membership and group information and this data is used to update membership and Roles on your Discord server.

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 two required plugins (this one you are reading about and the Discord OAuth login plugin) (again, see below)
  1. 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’):

image

You will need to authorise the bot in the browser, see Discord Developer Portal

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

app.yml changes

plugins

You need two plugins for this. (The first is not originally mine but you need my forked changes presently until my PR is accepted and merged)

  1. GitHub - merefield/discourse-plugin-discord-auth: A Discourse plugin to login over Discord
  2. GitHub - merefield/discourse-discord-bot: A Discord bot that runs on your Discourse server to link the two servers.

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 pre-exist. You need to add the plugins to the plugin section directly beneath this.

Then at the prompt ./launcher rebuild app as usual.

This should work, np.

You may get some console and log errors indicating a failure of the bot on first build as you’ve not yet entered the bot’s credentials in the front end of Discourse’s settings yet. (more gracefully handling this is a todo, I’ll delete this bit when that gets done). It will do no harm though and Discourse itself will work normally.

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

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:

image

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

Once you’ve set these, go back to your root prompt in linux and type:

./launcher restart app

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

image

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.

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.:

image

!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.

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.

Limitations

The main goal of this plugin was to allow the user to create a bot on their Discord that would be powered by their Discourse server and be able to carry out some basic membership management functions. It also provides a foundation for any additional bot-like interactions between the two systems for the future. Part of the motivation for using bots is that they are just plain fun.

Apologies, only English language is presently supported and have yet to add hooks to support translation, partly because there’s quite a lot of language. Let me know if that’s a huge issue.

Bot commands are ad hoc by their very nature.

I have also been working on another plugin which avoids the use of bot commands and instead uses more vanilla REST calls from Discourse to manage things. The advantage of that approach is that you can create a batch process to sync things periodically without having to initiate the process every time. I’ll see if I have time to complete that given competing priorities …

Known Issues

The Robot is a bit chatty during ‘online’ Discourse upgrades as keeps getting reactivated during the upgrade process. This comes back to working out a good way to running the bot in a separate, managed process instead of a thread branched off the webserver. I believe this is all harmless though, and if the bots output is only to your admin channel on Discord, do you really care?

Thanks

There are several backers to thank for this plugin which took me a big while to get to this stage inc. @Wedgebert, @FoohonPie. Thanks to Jeff for his generous contribution. Thanks to @angus for all the encouragement and dealing with the financial support.

The plugin was inspired by the work @Watercolor_Games did at an earlier stage and relies on the discord OAuth plugin built by @featheredtoast

The plugin relies on the semi-official Discord-backed discordrb Ruby library and the fantastic work Discord team has done to make their system accessible. Thanks to @Falco for helping me with a dependency in an extremely responsive manner.

Additionally, of course, this would not be possible without the amazing plugin ecosystem of Discourse (woo!)

State of these instructions

They will be improved in time and I welcome feedback. There are areas that are bound to be unclear.

16 Likes

Hey I’m super interested in this.

Would the opposite be possible? I’d love a way to be able to create a Discourse setup for my Discord server. I’m not sure if there’s a Discord API event for roles being given but you could probably check when someone logs in with Discord or with CRON.

Yes, absolutely.

You could either build that as a bot command for ad hoc application, but with a repeat feature that reran the command after a period (nice but not very transparent and no way to individually manage these recurrences)

OR

As I alluded to above, have it scheduled as a job in Discourse as a sidekiq job. That might be a separate plugin (I actually started on the skeleton of that approach before moving to a bot only solution mainly because the ‘bot’ approach was kind of the Discord ‘thing’, fun and offered a unique approach and result), but if I can get Discourse to fire off bot commands then that’s moot (not yet had the chance to test that), and then we can write stuff once and have it work either way (nice!).

Both these solutions would rely on the same OAuth login though.

Whilst this is an excellent idea, it can’t be a priority for me at present because I have a lot of other client work at the moment I need to deliver, but if you wish to support such an extension, we can discuss offline on a PM and schedule it in.

1 Like

It might be interesting to play around with, but I have nowhere near the skill level required to volunteer to develop/maintain an addon like this. I might make a messy fork at some time but I certainly don’t want my name on anything official. :slight_smile:

1 Like

Absolutely get messy. It’s the only way to learn. :).