Eat, Play, Nap and Code

食とあそびと昼寝とプログラミング学習

【discordrb】BotをDiscordスレッド内から呼び出せるようにする

f:id:eatplaynap329:20220211172655p:plain
スレッド内でBotが呼び出せた!

はじめに

こんにちは。去年の8月にこんなDiscord Botを作りました。

eatplaynap329.hatenablog.jp

この時点でDiscordにスレッド機能というものはあったんですが、このBotはスレッド内呼び出しに対応させていませんでした。 スレッド機能が出来たばかり(2021年7月29日に使えるようになったみたい)ということもあり、そもそもスレッドを使ったことがなく、スレッド内で呼べるBotである必要がなかったので対応させるという発想がありませんでした。

ですが、最近になってFBCサーバー内でスレッドを多用する機会が増えて、スレッド内でも自分の指名Botを呼べたら便利だな〜と思ったので調べて対応させました。現時点(2022年2月11日)ではこれが最適解なのかなーと思います。もっといいやり方があったら教えて下さい。

discordrbでの対応方法はあんまり見つからなそうなので、数少ない日本語話者のdiscordrb仲間に届いたらいいですね😄

もくじ

対応方法

要約

  • Gemfileのgem 'discordrb'gem 'discordrb', github: 'shardlab/discordrb', branch: 'threads'に書き変えてスレッド対応済みのブランチを取り込む
  • Privileged Gateway IntentsのPRESENCE INTENTとSERVER MEMBERS INTENT両方を有効にする

詳細

とりあえずDiscord Developer Portalのスレッドのページを読んでみます。

Threads are only available in API v9. Bots that do not update to API v9 will not receive most gateway events for threads, or things that happen in threads (such as Message Create).

だそうです。API v9にアップデートしてないBotはスレッド内での情報を受け取れないようです。

実際、自分のBotをローカルで動かしながら通常のテキストチャンネルとスレッド内でそれぞれでコメントをしてみたところ、通常のテキストチャンネルに書いたコメントはログに出力されましたが、スレッド内のコメントについては何も出力されませんでした。

なので、discordrbのissue一覧を眺めて、「thread」とか「v9」みたいな話をしてないか探してみました。そしたら1件それっぽいのが見つかりました。

github.com

このissueの会話によると、現在(といっても2021年4月時点)はスレッド対応用のブランチを取り込むことでスレッドのイベントが受け取れるようになると話しているようでした。

さっそくGemfileを書き換えて、bundleして実行したところ以下のようなエラーに見舞われて起動終了してしまいました。

[DEBUG : main @ 2022-02-11 16:53:44.839] WS thread created! Now waiting for confirmation that everything worked
[DEBUG : websocket @ 2022-02-11 16:53:44.839] Connecting
[DEBUG : websocket @ 2022-02-11 16:53:44.943] Gateway URL: wss://gateway.discord.gg/?encoding=json&v=9
[DEBUG : websocket @ 2022-02-11 16:53:45.010] Obtained socket
[IN : websocket @ 2022-02-11 16:53:45.218] {"t"=>nil, "s"=>nil, "op"=>10, "d"=>{"heartbeat_interval"=>41250, "_trace"=>["[\"gateway-prd-main-5fkx\",{\"micros\":0.0}]"]}}
[DEBUG : websocket @ 2022-02-11 16:53:45.218] Hello!
[DEBUG : websocket @ 2022-02-11 16:53:45.218] Trace: ["[\"gateway-prd-main-5fkx\",{\"micros\":0.0}]"]
[DEBUG : websocket @ 2022-02-11 16:53:45.218] Session: nil
[OUT : websocket @ 2022-02-11 16:53:45.219] {"op":2,"d":{"token":"Bot REDACTED_TOKEN","properties":{"$os":"x86_64-darwin20","$browser":"discordrb","$device":"discordrb","$referrer":"","$referring_domain":""},"compress":false,"large_threshold":100,"intents":32767}}
[OUT : websocket @ 2022-02-11 16:53:45.536] 
[DEBUG : websocket @ 2022-02-11 16:53:45.537] Raised a Discordrb::Events::DisconnectEvent
[ERROR : websocket @ 2022-02-11 16:53:45.537] The websocket connection has closed: (no information)
[DEBUG : websocket @ 2022-02-11 16:53:45.537] Raised a Discordrb::Events::DisconnectEvent
[ERROR : websocket @ 2022-02-11 16:53:45.537] Websocket close frame received!
[ERROR : websocket @ 2022-02-11 16:53:45.537] Code: 4014
[ERROR : websocket @ 2022-02-11 16:53:45.537] Message: Disallowed intent(s).
[ERROR : websocket @ 2022-02-11 16:53:45.537] You attempted to identify with privileged intents that your bot is not authorized to use
Please enable the privileged intents on the bot page of your application on the discord developer page.
Read more here https://discord.com/developers/docs/topics/gateway#privileged-intents
[WARN : websocket @ 2022-02-11 16:53:45.537] The WS loop exited! Not sure if this is a good thing
[DEBUG : main @ 2022-02-11 16:53:45.845] Reconnection flag was unset. Exiting run.
[DEBUG : main @ 2022-02-11 16:53:45.846] Oh wait! Not exiting yet as run was run synchronously.

ちゃんと読むと、とても丁寧にエラーの原因と対処法を教えてくれていました。

You attempted to identify with privileged intents that your bot is not authorized to use Please enable the privileged intents on the bot page of your application on the discord developer page.

私のBotは以前まではSERVER MEMBERS INTENTしか有効にしていないまま動いていましたが、APIv9だとPRESENCE INTENTの方も有効にしないとダメみたいです。なので有効にしました。

Discord_Developer_Portal_—_My_Applications.png

それからマージしてデプロイしたらスレッド内でもBotの呼び出しができるようになりました😄

Image from Gyazo

github.com

おわりに

実は去年の11月からBotが毎朝Herokuで手動デプロイしないと動かない状態になってたんですが、この状況が一旦解決した?かもしれなくて動くようになったので新機能に取り組む元気が出てきました。

↑の件はまだ何で動かなくなったのか・何で今動くのかがよくわからないので公にしづらいですがなにか分かったらまた記事にするかもしれないです!

とりあえずBotを使ってくれる皆様と、いろいろアドバイスくださった皆様に感謝です!

FBC卒業生で友達のふーがさんが主催するりんどく.rbでも使ってくれてます😭

fuga-ch85.hatenablog.com