Skip to content

ブートキャンプを退会した時にDiscordからも退出させる#6099

Closed
maeda-m wants to merge 2 commits intomainfrom
feature/kick-from-discord-when-bootcamp-retired
Closed

ブートキャンプを退会した時にDiscordからも退出させる#6099
maeda-m wants to merge 2 commits intomainfrom
feature/kick-from-discord-when-bootcamp-retired

Conversation

@maeda-m
Copy link
Copy Markdown
Contributor

@maeda-m maeda-m commented Jan 24, 2023

Issue

概要

ブートキャンプ退会時に Discord アカウント情報があれば Web API を利用して自動で Discord サーバーから退出させる機能を実装しました。

ただし、次の懸念点があります。

  1. ブートキャンプの登録情報にある Discord アカウント情報は非認証の自己申告されたものであること
  2. ブートキャンプの登録情報は /current_user/edit から変更できるが Discord アカウント情報の入力は必須ではないこと
  3. 休会時にも同様に Discord サーバーから退出させる必要があるのでは?

上記の懸念点への対応としては次の通りとしました。

  • 懸念点1への対応
    • 自動で退出させる Discord アカウントのロールを現役生のみに限定することで、
      管理者・メンター・顧問・研修生などのロールが付与されている Discord アカウントの退出を回避します
    • Discord アカウント情報が存在しないものであったり、上記のロールが付与されているものの場合は、
      Discord サーバーから退出させずに、試みたことをエラーログとして記録します
  • 懸念点2への対応
    • Discord アカウント情報があった場合のみ Discord サーバーからさせます
  • 懸念点3への対応
    • この議題の範囲外として判断しました(次回ミーティングで共有します)

変更確認方法

  1. feature/kick-from-discord-when-bootcamp-retired をローカルに取り込みます
  2. bundle exec rails db:drop を実行します
  3. bin/setup を実行します
  4. bin/rails log:clear を実行します
  5. 後述の「注意・伝達事項」にある追加する2の環境変数を設定します
  6. bin/rails s でサーバーを立ち上げます
  7. kimura でログインします
  8. Discord Bot を参加させた Discord サーバーに確認用のアカウント(Bot 以外)を参加させます
    • Discord にて @maeda-m に招待リンクを共有いただければ参加します👍
  9. 登録情報変更( http://localhost:3000/current_user/edit )から項目「Discord アカウント」に 8 で参加させた確認用のアカウント(#付き)を入力し、ボタン「更新する」をクリックします
  10. 退会手続き( http://localhost:3000/retirement/new )にアクセスし、項目「全体の満足度を教えてください」に任意の値を選択し、ボタン「退会する」をクリックします
  11. 確認ダイアログのOKをクリックすると、「退会処理が完了しました」と表示されます
  12. 8 で参加させた確認用のアカウントが Discord サーバーから退出されたことを確認します
    • Discord の画面右に表示されるメンバーリストや
      管理者であれば サーバー設定 > ユーザー管理 のメンバーから確認できます
  13. log/development.log[Discord API] <Discord アカウント(#付き)が入ります> を退出させました。 と出力されていることを確認します

Screenshot

画面上の変更はありません。

注意・伝達事項

Discord Bot を参加させる Discord サーバーのIDを取得する

Note

確認用の Discord サーバーを作成する場合は #5454 ようにサーバーを新規作成してください(webhookURLの取得は不要です)。

アイコンを右クリックして、コンテキストメニュー「IDをコピー」をクリックして控えます。

discord_005

Discord Bot のトークン情報を取得する

https://discord.com/developers/applications にアクセスして右上のボタン「New Application」からアプリケーションを登録し、ボットを使えるように設定します。

  1. 次の項目を入力・チェックしてアプリケーションを作成します
    • NAME: Bootcamp
    • By clicking Create, you agree to the Discord Developer Terms of Service and Developer Policy(利用規約や開発者ポリシーへの同意): チェックを入れる
  2. 左のメニューからBotをクリックし、ボタン「Add Bot」からボットを追加します
  3. Bot のトークン情報の更新と各種設定をします
    • ボタン「Reset Token」をクリックし、コピーして控えます
    • Authorization Flow > PUBLIC BOT のトグルボタンを OFF にします
    • Privileged Gateway Intents > SERVER MEMBERS INTENT のトグルボタンを ON にします
    • ボタン「Save Changes」をクリックします

discord_003

  1. OAuth2 > URL Generator をクリックし、Discord サーバーへ参加させるためのリンクを発行します
    • SCOPES
      • bot: チェックを入れる
    • BOT PERMISSIONS
      • Kick Members: チェックを入れる
    • GENERATED URL
      • ボタン「Copy」をクリックし、コピーして控えます

discord_004

  1. 4でコピーしたリンクを開いて Bot を Discord サーバーへ参加させます

Warning

前述の Privileged Gateway Intents > SERVER MEMBERS INTENT を OFF のまま参加した場合は、前項の URL Generator からやり直してください。

ボタン「認証」をクリックして画面の指示に従えばOKです。

Bot を 参加させる Discord サーバーを選びます メンバーをキックにチェックが入っていることを確認します
discord_006 discord_007

追加する2の環境変数を設定する

前述の手順で発行された各キーを環境変数を設定する必要があります。

  1. DISCORD_GUILD_ID
    • 前述の「Discord Bot を参加させる Discord サーバーのIDを取得する」で控えた値です
  2. DISCORD_BOT_TOKEN
    • 前述の「Discord Bot のトークン情報を取得する」の3.で控えた値です

開発環境での例として次のコマンドをターミナルで実行します:

export DISCORD_GUILD_ID=nnnnnnnnnnnnnnnnnnn
export DISCORD_BOT_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

参考情報

@maeda-m maeda-m self-assigned this Jan 24, 2023
@maeda-m maeda-m force-pushed the feature/kick-from-discord-when-bootcamp-retired branch from 86b14e0 to 41ed47e Compare January 25, 2023 01:55
end

def initialize(attributes = {})
user = attributes[:user]
Copy link
Copy Markdown
Contributor Author

@maeda-m maeda-m Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 attributes には次の内容が渡されます。

  {
    "avatar": null,
    "communication_disabled_until": null,
    "deaf": false,
    "flags": 1,
    "joined_at": "2023-01-25T01:22:03.389250+00:00",
    "mute": false,
    "nick": null,
    "pending": false,
    "premium_since": null,
    "roles": [
      "ロールのIDが入ります",
      "ロールのIDが入ります"
    ],
    "user": {
      "avatar": "xxxxxxxxxxxxx",
      "avatar_decoration": null,
      "bot": false,
      "discriminator": "NNNN",
      "id": "Discord アカウントのIDが入ります",
      "public_flags": 0,
      "username": "maeda-m"
    }
  }


result = JSON.parse(response.body, symbolize_names: true)
discord_members = result.map { |attrs| DiscordMember.new(attrs) }
discord_members.find { |member| account_name == member.account_name }
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 find_by の戻り値は Discord のアカウント名(#付き)が完全に一致するものに限定しています。
これは Search Guild Members の仕様としてユーザー名だけでなくニックネームも検索対象になっているためです。

discord_account = user.discord_account
return if discord_account.blank?

discord_member = DiscordMember.find_by(account_name: discord_account)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Discord サーバーから退出させるには Discord アカウントの ID が必要になるため、
Discord アカウント名を Web API で検索し、 Discord アカウントの ID を求めています。

return
end

if discord_member.destroy
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Discord サーバーから退出させる処理は、メンバーのリソースを削除する Web API で実行しています。

Comment on lines +63 to +64
return false if @bot
return false unless @role_ids.empty?
Copy link
Copy Markdown
Contributor Author

@maeda-m maeda-m Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 プルリクエストの概要で記載した懸念点1への対応に加えて、ボットの退出も回避しています。
これはボットは現役生ではない判断としているためです。

notify_to_admins
notify_to_mentors
logout
kick_from_discord(user)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Newspaper 経由でもいいのかなと思いつつも、愚直に処理を実行しています。

@komagata
Copy link
Copy Markdown
Member

@maeda-m (CC: @machida)

すみません、こちら、下記に書いたように問題があるのでまずは調査ぐらいでお願いしたかったのですが、割り当ててしまって作業させてしまって申し訳ありません。

#1996

@maeda-m
Copy link
Copy Markdown
Contributor Author

maeda-m commented Jan 25, 2023

📝 ふりかえり・計画ミーティング2023年01月25日 より
#1996 は調査までの議題となるため完了とした。併せてこのプルリクエストもクローズする(ブランチは残す)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants