Repository navigation
Discord chat: a moderators' log, and timeouts that follow forum restrictions #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,18 @@ class MemberSync | |
| # the run up to the next one. | ||
| MAX_WAIT = 10 | ||
|
|
||
| # Discord's longest timeout is 28 days; a longer forum suspension is | ||
| # covered by renewing it as it runs down. | ||
| MAX_TIMEOUT = 28.days - 1.hour | ||
|
|
||
| # A timeout already this close to the one wanted is left alone, so a | ||
| # long suspension isn't rewritten on every run. | ||
| TIMEOUT_SLACK = 1.day | ||
|
|
||
| # Remembers which timeouts the forum set, so a moderator's own timeout | ||
| # in the server is never lifted by the sync. | ||
| STORE = "discord_chat" | ||
|
|
||
| def initialize(api: Api.new, sleeper: ->(seconds) { sleep(seconds) }) | ||
| @api = api | ||
| @sleeper = sleeper | ||
|
|
@@ -45,11 +57,15 @@ def sync_member(member, user, owner_id:) | |
| want = allowed && tests.any? { |wanted| wanted.call(user) } | ||
| if want && !has.include?(role_id) | ||
| call(:put, "/guilds/#{guild_id}/members/#{discord_id}/roles/#{role_id}") | ||
| log_access(discord_id, user, granted: true) if role_id == member_role_id | ||
| elsif !want && has.include?(role_id) | ||
| call(:delete, "/guilds/#{guild_id}/members/#{discord_id}/roles/#{role_id}") | ||
| log_access(discord_id, user, granted: false) if role_id == member_role_id | ||
| end | ||
| end | ||
|
|
||
| sync_timeout(member, user) if discord_id != owner_id && SiteSetting.discord_chat_sync_timeouts | ||
|
|
||
| # Discord doesn't let anyone rename the server's owner. | ||
| return if discord_id == owner_id | ||
| return unless allowed && SiteSetting.discord_chat_sync_nicknames | ||
|
|
@@ -61,8 +77,76 @@ def sync_member(member, user, owner_id:) | |
| Rails.logger.warn("#{DiscourseDiscordChat::LOG_TAG} member #{discord_id}: #{e.message}") | ||
| end | ||
|
|
||
| # A forum suspension or silence times the member out in the server for | ||
| # as long as it lasts (up to Discord's 28 days, renewed as it runs). | ||
| # Only a timeout the forum set is ever lifted. | ||
| def sync_timeout(member, user) | ||
| discord_id = member.dig("user", "id") | ||
| wanted = timeout_until(user) | ||
| current = Time.zone.parse(member["communication_disabled_until"].to_s) | ||
| current = nil if current && current <= Time.zone.now | ||
| ours = PluginStore.get(STORE, timeout_key(discord_id)).present? | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When timeout syncing is enabled, every non-bot guild member reaches this AGENTS.md reference: AGENTS.md:L24-L24 Useful? React with 👍 / 👎. |
||
|
|
||
| if wanted | ||
| return if current && (current - wanted).abs < TIMEOUT_SLACK | ||
| return if current && !ours && current > wanted | ||
| call( | ||
| :patch, | ||
| "/guilds/#{guild_id}/members/#{discord_id}", | ||
| { communication_disabled_until: wanted.iso8601 }, | ||
| ) | ||
| PluginStore.set(STORE, timeout_key(discord_id), true) | ||
| unless ours | ||
| ModLog.post( | ||
| "⏸️ <@#{discord_id}> (#{user.username}) timed out until #{wanted.to_date}: restricted on the forum.", | ||
| api: @api, | ||
| ) | ||
| end | ||
| elsif ours | ||
| if current | ||
| call( | ||
| :patch, | ||
| "/guilds/#{guild_id}/members/#{discord_id}", | ||
| { communication_disabled_until: nil }, | ||
| ) | ||
| ModLog.post( | ||
| "▶️ <@#{discord_id}> timeout lifted: no longer restricted on the forum.", | ||
| api: @api, | ||
| ) | ||
| end | ||
| PluginStore.remove(STORE, timeout_key(discord_id)) | ||
| end | ||
| rescue Api::RateLimited | ||
| raise | ||
| rescue Api::Error => e | ||
| Rails.logger.warn("#{DiscourseDiscordChat::LOG_TAG} timeout #{discord_id}: #{e.message}") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def timeout_until(user) | ||
| return if user.nil? | ||
| now = Time.zone.now | ||
| ends = [user.suspended_till, user.silenced_till].compact.select { |time| time > now }.max | ||
| [ends, now + MAX_TIMEOUT].min if ends | ||
| end | ||
|
|
||
| def timeout_key(discord_id) | ||
| "timeout_#{discord_id}" | ||
| end | ||
|
|
||
| def member_role_id | ||
| SiteSetting.discord_chat_member_role_id.to_s.strip | ||
| end | ||
|
|
||
| def log_access(discord_id, user, granted:) | ||
| if granted | ||
| ModLog.post("✅ <@#{discord_id}> got in as forum user #{user.username}.", api: @api) | ||
| else | ||
| ModLog.post("⛔ <@#{discord_id}> lost access to the chat.", api: @api) | ||
| end | ||
| end | ||
|
|
||
| # A launch can link many people at once; a short rate limit is waited | ||
| # out instead of ending the run. | ||
| def call(*args, **kwargs) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseDiscordChat | ||
| # One line per thing the forum did in the server, in the moderators' | ||
| # log channel. A line that can't be posted is dropped: the log is a | ||
| # convenience and never holds up the work it describes. | ||
| module ModLog | ||
| def self.post(text, api: Api.new) | ||
| channel = SiteSetting.discord_chat_mod_log_channel_id.to_s.strip | ||
| return if channel.blank? || !DiscourseDiscordChat.configured? | ||
| api.bot( | ||
| :post, | ||
| "/channels/#{channel}/messages", | ||
| { content: text.to_s.truncate(2000), allowed_mentions: { parse: [] } }, | ||
| ) | ||
| nil | ||
| rescue Api::Error => e | ||
| Rails.logger.warn("#{DiscourseDiscordChat::LOG_TAG} mod log: #{e.message}") | ||
|
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When posting the optional log line encounters a network failure such as Useful? React with 👍 / 👎. |
||
| nil | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an admin disables this setting after the forum has set a timeout, this guard prevents the stored
oursmarker from ever being cleaned up. If the forum restriction is then lifted, a Discord moderator applies a new manual timeout, and syncing is re-enabled, the stale marker makes theelsif oursbranch clear the moderator's timeout, contradicting the guarantee that manually applied Discord timeouts are left alone; remove markers when disabling the setting or store enough state to verify that the current timeout is still the forum-created one.Useful? React with 👍 / 👎.