Repository navigation
Discord chat: "Report to moderators" on any message in the server #446
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Jobs | ||
| # A message reported in the Discord server: a line in the moderators' log | ||
| # there, and a message to the forum's moderators group, where moderators | ||
| # already work. The reported text is untrusted, so it goes in as a code | ||
| # block on the forum (no mentions, links or formatting) and pings nobody | ||
| # in the server. | ||
| class DiscordChatReport < ::Jobs::Base | ||
| def execute(args) | ||
| return if !DiscourseDiscordChat.configured? || !DiscourseDiscordChat.reports_enabled? | ||
|
|
||
| guild = SiteSetting.discord_chat_guild_id.to_s.strip | ||
| link = "https://discord.com/channels/#{guild}/#{args[:channel_id]}/#{args[:message_id]}" | ||
| content = args[:content].to_s | ||
|
|
||
| DiscourseDiscordChat::ModLog.post( | ||
| "🚩 <@#{args[:reporter_id]}> reported a message by <@#{args[:author_id]}> in <##{args[:channel_id]}>: #{link}\n" + | ||
| content.lines.first(10).map { |line| "> #{line.chomp}" }.join("\n"), | ||
| ) | ||
|
|
||
| moderators = Group.find_by(id: Group::AUTO_GROUPS[:moderators]) | ||
| return if moderators.nil? | ||
|
|
||
| PostCreator.create!( | ||
| Discourse.system_user, | ||
| title: "Server report: a message by #{plain(args[:author_name])}", | ||
| raw: forum_body(args, link, content), | ||
| archetype: Archetype.private_message, | ||
| target_group_names: [moderators.name], | ||
| skip_validations: true, | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def forum_body(args, link, content) | ||
| fence = "~~~~" | ||
| <<~MD | ||
| A message in the JTech server was reported. | ||
|
|
||
| - **Reported by:** #{who(args[:reporter_id], args[:reporter_name])} | ||
| - **Written by:** #{who(args[:author_id], args[:author_name])} | ||
| - **Message:** #{link} | ||
|
|
||
| #{fence}text | ||
| #{content.gsub(/~{3,}/, "~~")} | ||
| #{fence} | ||
| MD | ||
| end | ||
|
|
||
| # The server name, plus the forum account when it's linked, as a profile | ||
| # link rather than an @mention so nobody is notified. | ||
| def who(discord_id, discord_name) | ||
| user = | ||
| UserAssociatedAccount.find_by(provider_name: "discord", provider_uid: discord_id.to_s)&.user | ||
| name = plain(discord_name) | ||
| user ? "#{name} (forum: [#{user.username}](#{user.full_url}))" : "#{name} (not linked)" | ||
| end | ||
|
|
||
| def plain(text) | ||
| text.to_s.gsub(/[^\w.\- ]/, "").strip.truncate(40).presence || "unknown" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") | ||
| nil | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When a report originates in a Discord channel that some forum moderators cannot view, such as an admin-only or private project channel, this unconditionally copies the raw message into a PM for the entire forum moderators group. The only authorization check is the guild ID; neither the interaction handler nor this job restricts source channels or recipients, so reporting widens access to restricted Discord content. Avoid copying the content unless every recipient is authorized for that channel, or constrain reports to an explicit safe channel set.
AGENTS.md reference: AGENTS.md:L9-L9
Useful? React with 👍 / 👎.