Discord Bot Bausatz đŸ€–

Zweck dieses Plugins

Dieses Plugin ĂŒbernimmt mehrere Aufgaben:

  1. Es ermöglicht dir, einen Discord-Bot ĂŒber deinen Discourse-Server zu steuern, um verschiedene Aktionen zur VerknĂŒpfung der beiden Systeme durchzufĂŒhren. Fork diesen Repository und erweitere ihn mit einfachem Ruby, um diverse Bot-Funktionen zu erstellen.

  2. Bestehende Funktionen dienen als Beispiele und nĂŒtzliche Hilfsprogramme:

  • Automatisierte Zwei-Wege-Veröffentlichung von Inhalten, die bestimmte Kriterien erfĂŒllen.
  • Befehl zum manuellen Kopieren von Nachrichten nach Discourse.
  • Befehl zum Synchronisieren der Gruppenmitgliedschaft mit der Rollenmitgliedschaft deines Discord-Servers und deiner Discourse-Instanz.

Das Plugin stellt ein erweiterbares GrundgerĂŒst bereit, auf dem du weitere Funktionen aufbauen kannst. Pull Requests fĂŒr allgemein nĂŒtzliche Erweiterungen sind willkommen.


dies sind nicht die beiden Sync-Befehle, sondern nur eine Illustration der möglichen Bot-Interaktion. Der zweite Befehl im Screenshot wurde wÀhrend der Entwicklung veraltet :wink:

Die Einrichtung ist zwangslĂ€ufig komplex, aber ideal fĂŒr Fans beider Systeme, die bereit sind, Zeit in das VerstĂ€ndnis der Funktionsweise jedes Systems zu investieren – zumindest auf funktionaler und konzeptioneller Ebene. Um das Beste aus diesem Plugin herauszuholen, musst du einige Grundlagen der Benutzerverwaltung in Discourse und Discord verstehen.

Bot-Befehle

Es gibt vier Befehle:

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

Ping!

Um zu prĂŒfen, ob der Bot antwortet, tippe einfach „Ping!" ein. Der Bot sollte dann mit „:robot: : Pong!" antworten – großartig, oder? :smiley: (das ist ĂŒbrigens ein kleiner Meme!)

!disckick <min_trust_level: Standard 2>

Mit diesem Befehl werden alle Benutzer gekickt, die auf Discourse existieren, aber ein Vertrauenslevel unter dem angegebenen Wert haben. Wenn kein Wert angegeben wird, betrÀgt der Standardwert 2.

Beispiel:

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

Dieser Befehl versucht, alle Gruppen in Discord-Rollen zu kopieren, die den angegebenen Kriterien (oder den Standardwerten, falls keine angegeben wurden) entsprechen. Anschließend werden die Rollen entsprechend der Gruppenmitgliedschaft in Discourse aufgefĂŒllt. Es handelt sich hierbei um eine einseitige Synchronisation. Keine Discourse-Daten werden dabei verĂ€ndert.

!disccopy <number_of_messages> <optional_target_category_name> <optional_target_topic_name>

Ermöglicht das Kopieren des Discord-Nachrichtenverlaufs in deine Discourse-Instanz direkt aus dem Discord-Chat heraus. Dies kann Tausende von Nachrichten auf einmal verarbeiten (fĂŒr dieses Volumen wird jedoch Zeit benötigt).

Zwei-Wege-Kommunikation

Hier sind die Einstellungen:

  • Du kannst einen AnkĂŒndigungs-Kanal auf Discord in den Plugin-Einstellungen festlegen.
  • Du kannst nun in den zugewiesenen Discord-AnkĂŒndigungs-Kanal schreiben, und deine Nachricht wird im zugewiesenen Discourse-Thema in den Plugin-Einstellungen veröffentlicht.
  • Du kannst eine Liste von Kategorien in Discourse einrichten, um Nachrichten an den AnkĂŒndigungs-Kanal auf deinem Discord zu senden, wenn dort jemand postet oder ein neues Thema erstellt (entweder oder beides).
  • Automatisierte Kopie von Chat-Nachrichten pro Kanal in eine gleichnamige Kategorie auf Discourse: Wenn der Kanalname mit dem Kategoriennamen ĂŒbereinstimmt, wird die Nachricht in Discourse kopiert.

image

ZusĂ€tzlich habe ich eine neue Datei hinzugefĂŒgt, um Discord-Ereignisse zu verwalten, die von der discordrb-API unterstĂŒtzt werden, wobei die AnkĂŒndigungslogik das erste Beispiel ist (du kannst gerne neue entwickeln und Pull Requests einreichen, wenn sie fĂŒr die Gemeinschaft nĂŒtzlich sind).

Hinweis: FĂŒr jede aus Discord nach Discourse kopierte Nachricht wird der Benutzer korrekt angezeigt, wenn dieser Benutzer sich ĂŒber die Discord-Anmeldung bei Discourse eingeloggt hat (so kann der Discord-Bot die Benutzer zuordnen).

Status dieses Plugins

Dies ist ein komplexes Plugin zur Einrichtung. Das war leider unvermeidbar.

Sobald es jedoch eingerichtet und in Betrieb ist, ist die Bedienung ein Kinderspiel.

Betrachte den Code derzeit als Beta. Er wurde getestet, sogar in der Produktion, enthĂ€lt aber wahrscheinlich noch Fehler. Ich empfehle, dies zunĂ€chst auf einem frischen Discord-Server zu testen, bevor du es auf deinen „Produktions"-Haupt-Discord-Server anwendest, es sei denn, du fĂ€ngst gerade erst an.

Fehler sind viel wahrscheinlicher, um deine Discord-Instanz zu stören als deine Discourse-Instanz, da keine Änderungen an Discourse vorgenommen werden. Allerdings habe ich es so geschrieben, dass die meisten Dinge wiederherstellbar sind und du ein Problem meist einfach durch Wiederholen der Aktion beheben kannst. Discourse dient als Master fĂŒr Mitgliedschafts- und Gruppeninformationen, und diese Daten werden verwendet, um die Mitgliedschaft und Rollen auf deinem Discord-Server zu aktualisieren.

Einrichtungshinweise

Voraussetzungen

  1. Ein Discord-Server, auf dem du Administrator bist.
  2. Eine Discord-App (siehe unten).
  3. Ein Discord-Bot (siehe unten).
  4. SSH-Root-Zugriff auf deinen Discourse-Server.
  5. Änderungen an app.yml zur Installation des Plugins.
  6. Alle Benutzer deines Discourse mĂŒssen sich mit der Discord-OAuth-Anmeldung anmelden, die auf deiner Anmeldeseite erscheint, sobald das Plugin installiert ist.

Discord-App

Gehe hierhin und erstelle eine App:

Klicke auf „New Application".

Du musst einen Bot erstellen, der ungefĂ€hr so aussieht (lass ihn nicht „öffentlich"):

Du musst den Bot im Browser autorisieren, siehe OAuth2 - Documentation - Discord

Sobald der Bot eingerichtet ist, kopiere das Token. Du musst dies spÀter in Discourse eingeben.

Änderungen an app.yml

Plugin

Du benötigst jetzt nur noch ein Plugin, da die Discord-Social-Login-Funktion nun nativer Bestandteil des Discourse-Kerns ist (Juhu :tada: )

Dann fĂŒhre am Prompt wie ĂŒblich ./launcher rebuild app aus.

Das sollte funktionieren, kein Problem.

Möglicherweise erhÀltst du einige Console- und Log-Fehler, die auf einen Fehler des Bots beim ersten Build hinweisen, da du die Zugangsdaten des Bots noch nicht in der BenutzeroberflÀche der Discourse-Einstellungen eingegeben hast. (Die elegantere Handhabung dessen steht noch an, ich werde diesen Teil löschen, sobald das erledigt ist). Es tut jedoch keinen Schaden, und Discourse selbst funktioniert normal.

Gehe in deinem Discourse-Admin-Bereich zu den Plugin-Einstellungen und fĂŒlle diese aus:

Die IDs erhĂ€ltst du ĂŒber die Discord-OberflĂ€che. Du musst den Entwicklermodus aktivieren, um diese IDs kopieren zu können. Gehe in Discord zu deinen Einstellungen → Erscheinungsbild → ERWEITERT und aktiviere den Entwicklermodus:

Du kannst dann IDs aus der OberflÀche abrufen, z. B.:

Du musst auch die Discord-OAuth-Einstellungen ausfĂŒllen. Die Client-ID und das Geheimnis stammen aus derselben Discord-App.

Sobald du diese Einstellungen vorgenommen hast, gehe zurĂŒck zu deinem Root-Prompt in Linux und tippe ein:

./launcher restart app

Wenn du deine Discord- und Discourse-Server korrekt eingerichtet hast, solltest du sehen, wie der Bot dem Server beitritt.

Falls der Bot jemals abstĂŒrzt (er geht offline), musst du dasselbe erneut tun, um ihn wiederherzustellen. Eine verbesserte UnabhĂ€ngigkeit des Bots vom Discourse-Kernserver steht noch an. Dennoch habe ich einen Bot lĂ€ngere Zeit in der Produktion betrieben, und er ist noch nie abgestĂŒrzt.

ZukĂŒnftige Befehle?

Wenn du eine Idee fĂŒr einen weiteren Befehl hast, der die Verbindung zwischen Discourse und Discord nutzt und deiner Meinung nach fĂŒr die breitere Gemeinschaft nĂŒtzlich wĂ€re, lass es mich in den Antworten wissen, und wir können prĂŒfen, ob eine Implementierung möglich ist. Hinweis: Dieser Bot soll keine Aufgaben außerhalb des Discourse-Umfangs ausfĂŒhren.

EinschrÀnkungen

Das Hauptziel dieses Plugins war es, dem Benutzer die Möglichkeit zu geben, einen Bot auf seinem Discord zu erstellen, der von seinem Discourse-Server gesteuert wird und grundlegende Funktionen zur Mitgliedschaftsverwaltung ausfĂŒhren kann. Es bietet auch eine Grundlage fĂŒr weitere botĂ€hnliche Interaktionen zwischen den beiden Systemen fĂŒr die Zukunft. Ein Teil der Motivation fĂŒr die Verwendung von Bots ist, dass sie einfach Spaß machen.

Bot-Befehle sind von Natur aus manuell/ad hoc.

Bekannte Probleme

  • Der Bot startet eine eigene Instanz, wenn du eine Rails-Console-Sitzung auf dem Server beginnst. Dies fĂŒhrt dazu, dass der Bot doppelt zu antworten scheint (tatsĂ€chlich gibt es dann zwei Bots). Stoppe im Rails-Console einfach den zusĂ€tzlichen Bot, indem du eingibst: ::DiscordBot::Bot.discord_bot.stop Dies sollte nun nicht mehr notwendig sein!

  • Der Roboter ist wĂ€hrend „online" Discourse-Upgrades etwas gesprĂ€chig, da er wĂ€hrend des Upgrade-Prozesses stĂ€ndig neu aktiviert wird. Dies fĂŒhrt zurĂŒck zu der Frage, wie man den Bot in einem separaten, verwalteten Prozess statt in einem vom Webserver abgezweigten Thread ausfĂŒhrt. Ich glaube jedoch, dass dies alles harmlos ist, und wenn die Ausgabe des Bots nur in deinen Admin-Kanal auf Discord geht, macht es dir wirklich etwas aus? Dies sollte jetzt so gut wie gelöst sein, wobei der Bot sich wĂ€hrend eines Neubuilds nur zweimal anmeldet.

Dank

Es gibt mehrere UnterstĂŒtzer, die fĂŒr dieses Plugin zu danken sind, das mich eine Weile bis zu diesem Stadium gebracht hat, darunter @Wedgebert, @FoohonPie. Danke an Jeff fĂŒr seinen großzĂŒgigen Beitrag. Danke an @angus fĂŒr die ganze Ermutigung und die BewĂ€ltigung der finanziellen UnterstĂŒtzung.

Das Plugin wurde von der Arbeit inspiriert, die @Watercolor_Games in einer frĂŒheren Phase geleistet hat, und basiert auf dem von @featheredtoast entwickelten Discord-OAuth-Plugin.

Das Plugin basiert auf der halb-offiziellen, von Discord unterstĂŒtzten discordrb Ruby-Bibliothek und der fantastischen Arbeit, die das Discord-Team geleistet hat, um ihr System zugĂ€nglich zu machen. Danke an @Falco, der mir in Ă€ußerst reaktionsschneller Weise bei einer AbhĂ€ngigkeit geholfen hat.

Außerdem wĂ€re dies natĂŒrlich ohne das erstaunliche Plugin-Ökosystem von Discourse nicht möglich (Juhu!).

Status dieser Anleitung

Sie werden im Laufe der Zeit verbessert, und ich freue mich ĂŒber Feedback. Es gibt Bereiche, die zweifellos unklar sein werden.

47 „GefĂ€llt mir“
Discord<-> Discourse Selective Group Sync
Discord login and obtain discord roles in discourse
Discord and Discourse - Better Together | Blog
Convert Existing Plugin to do Discord to Discourse role/group sync
Slack Bot Construction Kit :robot:
2019: The Year in Review
Creat a user automatically when granted a Discord Role?
Discord Sync: sync a Discourse forum with a Discord server
Creating bot on discourse
Using "custom" ruby gems
Discourse Trust Levels for Discord
Discord is taking aim at Discourse. How does Discourse remain unique and stand out from the crowd?
Discord is taking aim at Discourse. How does Discourse remain unique and stand out from the crowd?
Copy + Pasting text from Discord into Discourse mangles paragraph breaks
Discord Role Badge Sync
Discord Bot: Topics not Posts
How do I go about making a very customized theme?
API feature request for additonal info about external accounts
Can anyone help me to add chat option of discord on forum?
How to Integrate/Sync Discord User MEE6 Points with Discourse Leaderboard?
LF Developer, Theme Creator, and Discord/Discourse Hero
Discord Sync: sync a Discourse forum with a Discord server
[PAID] Discourse/Discord integration needed (roles/user groups)
Can anyone help me to add chat option of discord on forum?
Discord and Discourse - Better Together | Blog
How to easily make Discourse bots?
Discourse to Discord bot possible?
Migrating from Discord to Discourse
Convert Existing Plugin to do Discord to Discourse role/group sync
Is migration from other chat apps supported?
Partially enable login option
Introducing Discourse Chat (BETA)
Which instant messenger do you use with Discourse?
Request widgetbot.io
Discord Sync: sync a Discourse forum with a Discord server

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.

2 „GefĂ€llt mir“

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.

4 „GefĂ€llt mir“

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:

3 „GefĂ€llt mir“

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

4 „GefĂ€llt mir“

Minor bug fix deployed:

Got this during rebuild:

Bot is still offline. Triple checked everything and it’s all set up correctly. Not sure how to proceed now
 Maybe I need to open a port on my server instance?

2 „GefĂ€llt mir“

Thanks for testing this out and so soon after the update. Let me try to reproduce and I will revert. I did not change anything wrt to the port config.

2 „GefĂ€llt mir“

No issues rebuilding here, Bot comes up fine 
 have you changed anything in your server config in between?

Your 400 Bad Request sounds like your server sent a corrupt or bad request to the Discord server and this was the response. This suggests it received it fine and was not blocked.

You can get this for:

  • malformed requests
  • lack of authorisation
  • exceeding rate limits

Unfortunately the error message doesn’t appear to help us determine which of these was the particular issue.

Presumably your Discourse is up? Check all the plugin settings are populated correctly.

Just to rule out a temporary gremlin run ./launcher restart app when you get chance 
 this will take your site down for a few seconds only (sorry about that!).

I see it runs the bot in the after_initialize block, so it will prevent migrations (and rebuilds) if the bot is misconfigured, or if Discord is down.

Maybe try to handle this exception and just log it?

5 „GefĂ€llt mir“

It runs on a separate thread? So I expect that not to be an issue?

However this callback seems to run several times in a rebuild, so the main issue will be that a misconfigured bot could actually breach rate limits if authorisation fails too many times in quick succession. If that happens the account, in worse case, could be blocked for some time.

In any case, I’ll do a quick patch to do that with the current implementation to lessen the impact. Thanks for the suggestion.

UPDATE: @falco, this is done, e.g.:

image

3 „GefĂ€llt mir“

First I want to say thank you for making this, once I can get it working its going to be a huge help for us managing our discourse and discord for a volunteer community.
I am running into a 400 error when trying to run this bot. Prior to this plugin being added we have been successfully using the official discord auth plugin and webhooks via the chat integration plugin. I’ve validated that those are both still working appropriately.

I created the bot within the same discord app I’d previously created authorized the bot within the discord server, it shows up in the member list as offline. Your documentation didn’t say what permission value to apply, but given most bots I’ve encountered for discord asked for it I went ahead and set it up with a permissions value of 8 for Administrator.

Within the discord server I then copied the ID of the Role it created, which was named after my app, and the id of the channel for admin text.I added the app Role to that channel with full rights, just to be sure, then added the IDs to discourse and restarted.

Unfortunately no matter what I try I’m getting a 400, I even tried a rebuild just to be sure.

3 „GefĂ€llt mir“

Thank you, kind words. It was also enabled by the generosity of funders.

Yes, that’s great. It should have Admin.

This is the second report of this issue.

If you intentially remove the last char of the token in settings (remember what this is) do you get a 401 instead?

Let’s move this to PM because it might get messy :wink: (we can always post the solution here).

4 „GefĂ€llt mir“

Just to post an update in case anyone else sees this, we’re actively looking into the issue but haven’t yet narrowed down the cause. I’d say if anyone else is running into this issue please say so would help to have others testing.

1 „GefĂ€llt mir“

This is now fixed. It was a weird one to track down.

Thanks to @ransim for raising and working with me to get to the bottom of it.

Huge thanks to the #ruby_discordrb gang on Discord API for their patient and instant help!

@neemiasvf

5 „GefĂ€llt mir“

@merefield no problem,Your plugin is great.But I have encountered some problems now.
such this:

Discourse Sync:  Starting.  Please be patient, I'm rate limited to respect Discord services.
Discourse Sync:  Checking if there are any eligible groups for sync ...
Discourse Sync: 1 eligible group(s) were found
Discourse Sync:  Preparing list of users who also have a registered account on Discord ...
Discourse Sync:  Preparing list of groups that users who have a registered account on Discord belong to on Discourse ...
Discourse Sync: 0 eligible group(s) were found with Discord users
Discourse Sync:  No users were found in elibigle groups for sync using provided or default criteria!

my commond is : !discsync 4

and commond changed: !discsync false 5 false

Discourse Sync:  Starting.  Please be patient, I'm rate limited to respect Discord services.
Discourse Sync:  Checking if there are any eligible groups for sync ...
Discourse Sync: 10 eligible group(s) were found
Discourse Sync:  Preparing list of users who also have a registered account on Discord ...
Discourse Sync:  Preparing list of groups that users who have a registered account on Discord belong to on Discourse ...
1 „GefĂ€llt mir“

Hey @p0nda, sorry for slow reply.

If you include any parameters, you need to include all of them. It might be interpreting things strangely.

3 „GefĂ€llt mir“

Unable to get the extended bot settings to show up? I have the changes to the app.yml and plugins installed. I have the OAuth setup working, but not the bot settings. Any ideas?

1 „GefĂ€llt mir“

The OAuth setting are there but no bot settings

1 „GefĂ€llt mir“

image
These

2 „GefĂ€llt mir“