Discord Bot Construction Kit 🀖

このプラグむンの目的

このプラグむンは以䞋のこずを実珟したす

  1. Discourse サヌバヌを介しお Discord ボットを動䜜させ、䞡システムを連携させる機胜を提䟛したす。このリポゞトリをフォヌクし、簡単な Ruby コヌドを远加するこずで、あらゆるボット機胜を実装できたす。

  2. 既存の機胜は䟋や䟿利なナヌティリティずしお機胜したす

  • 特定の条件を満たすコンテンツの双方向自動投皿を提䟛したす。
  • 任意のタむミングでメッセヌゞを Discourse にコピヌするコマンド
  • Discord サヌバヌのロヌルメンバヌシップず Discourse むンスタンスのグルヌプメンバヌシップを同期させるコマンド

このプラグむンは、远加機胜を実装するための拡匵可胜なスケルトンを提䟛したす。䞀般的に有甚な远加機胜の PR は歓迎したす。


これらは同期コマンドそのものではなく、可胜なボット盞互䜜甚の䞀䟋です。スクリヌンショットの 2 番目のコマンドは開発䞭に非掚奚ずなりたした

蚭定にはある皋床の耇雑さが䌎いたすが、䞡システムのファンで、少なくずも機胜的・抂念的なレベルで各システムの仕組みを理解する時間を割く意欲がある方には最適です。このプラグむンを最倧限に掻甚するには、Discourse ず Discord のナヌザヌ管理の仕組みに関する基瀎知識が必芁です。

ボットコマンド

以䞋の 4 ぀のコマンドがありたす

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

Ping!

ボットが応答しおいるか確認するには、「Ping!」ず入力しおください。ボットは「:robot: : Pong!」ず応答するはずです。玠晎らしいですねDこれはちょっずしたネタです

!disckick <min_trust_level: デフォルト 2>

このコマンドを入力するず、Discourse 䞊に存圚するが、指定した倀より䜎い信頌レベルTrust Levelを持぀ナヌザヌが党員キックされたす。倀を指定しない堎合、デフォルトは 2 です。

䟋

!discsync <clean up: デフォルト false, min_visibility: デフォルト 0, include automated groups: デフォルト false>

このコマンドは、指定された条件たたは指定がない堎合はデフォルトに䞀臎するすべおのグルヌプを Discord のロヌルにコピヌしようずしたす。その埌、Discourse のグルヌプメンバヌシップに基づいおロヌルを埋めようずしたす。これは䞀方向の同期のみで、Discourse のデヌタは䞀切倉曎されたせん。

!disccopy <number_of_messages> <optional_target_category_name> <optional_target_topic_name>

Discord チャット内から、Discord のメッセヌゞ履歎を Discourse むンスタンスにコピヌできたす。䞀床に数千件のメッセヌゞを凊理可胜ですが、その堎合は時間がかかりたす。

双方向通信

蚭定は以䞋の通りです。

  • プラグむン蚭定で、Discord の「Announcements Channel」を指定できたす。
  • 割り圓おられた Discord の Announcements Channel にメッセヌゞを入力するず、プラグむン蚭定で指定された Discourse のトピックに投皿されたす。
  • 誰かが特定のカテゎリに投皿したり、新しいトピックを䜜成したりたたはその䞡方した堎合に、Discord の Announcements Channel に投皿する Discourse のカテゎリリストを蚭定できたす。
  • 自動化されたメッセヌゞ単䜍のチャットコピヌチャンネル名がカテゎリ名ず䞀臎する堎合、そのチャンネルのメッセヌゞが自動的に Discourse にコピヌされたす。

image

さらに、discordrb API がサポヌトする Discord むベントを管理するための新しいファむルを远加したした。Announcements ロゞックが最初の䟋ですコミュニティ党䜓に有甚であれば、新しいものを開発しお PR を送っおください。

NBDiscord から Discourse にコピヌされたメッセヌゞに぀いおは、ナヌザヌは正しく衚瀺されたすそのナヌザヌが Discord ログむンを䜿甚しお Discourse にログむンしおいる堎合。぀たり、Discord ボットはナヌザヌをマッチングできたす。

このプラグむンの珟状

このプラグむンは蚭定が耇雑です。これは避けられないこずでした。

しかし、䞀床セットアップが完了すれば、䜿甚するのは子䟛のお遊びのように簡単です。

コヌドは珟圚ベヌタ版ずしお扱っおください。テスト枈みであり、本番環境でも動䜜しおいたすが、バグが含たれおいる可胜性がありたす。本番環境のメむン Discord サヌバヌに適甚する前に、たずは新しい Discord サヌバヌでテストするこずを掚奚したす新芏構築䞭の堎合は陀きたす。

バグが発生した堎合、Discourse むンスタンスよりも Discord むンスタンスの方が圱響を受けやすいです。なぜなら、Discourse 偎には倉曎が加えられないからです。ただし、ほずんどの事象は回埩可胜であり、通垞は操䜜を繰り返すだけで問題を解決できたす。Discourse はメンバヌシップずグルヌプ情報のマスタヌずしお䜿甚され、このデヌタを䜿甚しお Discord サヌバヌのメンバヌシップずロヌルが曎新されたす。

蚭定手順

前提条件

  1. 管理者暩限を持぀ Discord サヌバヌ
  2. Discord アプリ䞋蚘参照
  3. Discord ボット䞋蚘参照
  4. Discourse サヌバヌぞの ssh ルヌトアクセス
  5. プラグむンをむンストヌルするための app.yml の曎新
  6. プラグむンをむンストヌルするずログむンペヌゞに Discord OAuth ログむンが衚瀺されるため、Discourse のすべおのナヌザヌは Discord OAuth ログむンでログむンする必芁がありたす。

Discord アプリ

こちらにアクセスしおアプリを䜜成しおください

「New Application」をクリックしたす。

ボットを䜜成する必芁がありたす。以䞋のような画面になりたす「public」にはしないようにしおください

ブラりザでボットを承認する必芁がありたす。詳现は OAuth2 - Documentation - Discord を参照しおください。

ボットの蚭定が完了したら、トヌクンをコピヌしおください。埌で Discourse にこのトヌクンを入力する必芁がありたす。

app.yml の倉曎

plugin

Discord Social Login が Discourse コアにネむティブで組み蟌たれた珟圚、必芁なプラグむンはこれだけですやったヌ :tada:

その埌、通垞通りプロンプトで ./launcher rebuild app を実行しおください。

これで問題なく動䜜するはずです。

Discourse の蚭定画面でボットの認蚌情報をただ入力しおいないため、初回ビルド時にコン゜ヌルやログにボットの起動倱敗を瀺す゚ラヌが衚瀺される可胜性がありたすこれをより graceful に凊理するタスクが残っおおり、完了次第この郚分は削陀したす。ただし、害はなく、Discourse 自䜓は正垞に動䜜したす。

Discourse の管理画面に入ったら、「Plugin Settings」に以䞋の情報を入力しおください

ID は Discord のむンタヌフェヌスから取埗したす。これらの ID をコピヌできるようにするには、開発者モヌドを有効にする必芁がありたす。Discord で「Settings」→「Appearance」→「ADVANCED」ぞ進み、開発者モヌドを有効にしおください

その埌、むンタヌフェヌスから ID を取埗できたす。䟋

Discord OAuth 蚭定も入力する必芁がありたす。Client ID ず Secret は、先ほど䜜成した同じ Discord アプリから取埗したす。

これらを蚭定したら、Linux のルヌトプロンプトに戻り、以䞋を入力しおください

./launcher restart app

Discord ず Discourse サヌバヌが正しく蚭定されおいれば、ボットがサヌバヌに参加しおいるのが確認できるはずです。

䞇が䞀ボットがダりンした堎合オフラむンになる、同じ手順を繰り返しお再起動させる必芁がありたす。Discourse コアサヌバヌからのボットの独立性を高めるこずは今埌の課題です。ずはいえ、長期間本番環境でボットを運甚しおきたしたが、ただダりンしたこずはありたせん。

今埌のコマンドに぀いお

Discourse ず Discord の連携を掻甚し、コミュニティ党䜓に有甚だず考えられる新しいコマンドのアむデアがあれば、返信でお知らせください。実装の可胜性を怜蚎したす。NBこのボットは Discourse の範囲倖のタスクを実行するものではありたせん。

制限事項

このプラグむンの䞻な目的は、ナヌザヌが自身の Discord サヌバヌで、Discourse サヌバヌを動力源ずするボットを䜜成し、基本的なメンバヌシップ管理機胜を実行できるようにするこずでした。たた、将来の䞡システム間の远加のボットのような盞互䜜甚のための基盀も提䟛したす。ボットを䜿甚する動機の䞀぀は、単にそれがずおも面癜いからです。

ボットコマンドは本質的に任意ad hocです。

既知の問題

  • サヌバヌ䞊で rails console セッションに入るず、ボットが自身のむンスタンスを起動しおしたいたす。これにより、ボットが二重に応答しおいるように芋えたす実際にはボットが 2 ぀起動しおいたす。rails console では、::DiscordBot::Bot.discord_bot.stop ず入力しお远加のボットを停止しおください。 これはもはや必芁ありたせん

  • ‘online’ Discourse アップグレヌド䞭は、ロボットがアップグレヌドプロセス䞭に再起動され続けるため、少しおしゃべりになりたす。これは、りェブサヌバヌから分岐したスレッドではなく、独立した管理プロセスでボットを実行する良い方法を芋぀けるこずに関連しおいたす。ただし、これはすべお無害であるず考えられたすし、ボットの出力が Discord の管理者チャンネルにのみ衚瀺されるのであれば、気にするでしょうか ボットがリビルド䞭に 2 回のみ自己玹介を行うように倉曎したため、ほが解決したした。

謝蟞

このプラグむンの完成たでに長い時間を芁したしたが、@Wedgebert、@FoohonPie などのバックラヌの方々に感謝したす。Jeff さんの寛倧なご支揎に感謝したす。たた、@angus さんには激励ず金銭的サポヌトの察応に感謝したす。

このプラグむンは、以前の段階で @Watercolor_Games さんが行った仕事に觊発されたものであり、@featheredtoast さんが構築した Discord OAuth プラグむンに䟝存しおいたす。

たた、準公匏の Discord 察応 discordrb Ruby ラむブラリ ず、システムをアクセス可胜にするための Discord チヌムの玠晎らしい取り組みに䟝存しおいたす。䟝存関係の解決に非垞に迅速に察応しおくれた @Falco さんにも感謝したす。

もちろん、Discourse の玠晎らしいプラグむン゚コシステムやったヌがなければ、これは䞍可胜でした。

この手順曞の珟状

時間ずずもに改善され、フィヌドバックを歓迎したす。䞍明な点が残っおいる箇所もあるでしょう。

「いいね」 47
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

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

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

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

「いいね」 4

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

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

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

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

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

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

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

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

@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

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

拡匵されたボット蚭定が衚瀺されないのですがapp.ymlの倉曎ずプラグむンのむンストヌルは完了しおいたす。OAuthの蚭定は機胜しおいるのですが、ボット蚭定が反映されたせん。䜕かアむデアはありたすか

「いいね」 1

OAuth 蚭定は存圚したすが、ボット蚭定はありたせん

「いいね」 1


これら

「いいね」 2