Skip to content

Commit b018a0e

Browse files
committed
FEATURE: support associated accounts: show correct user for message if user has account in Discourse
1 parent dcaca86 commit b018a0e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

config/locales/server.en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ en:
55
discord_bot_admin_channel_id: "The id of your admin text channel on Discord"
66
discord_bot_admin_role_id: "The id of your admin role on Discord for which commands are permitted"
77
discord_bot_announcement_channel_id: "The id of your Discord announcement channel"
8-
discord_bot_discourse_announcement_topic_id: "The id of your Discourse announcement Topic"
9-
discord_bot_auto_channel_sync: "Enable chat sync on identically named channels/Categories"
8+
discord_bot_discourse_announcement_topic_id: "The id of your Discourse Announcement Topic (if discord auto channel sync is enabled and matching Category found this is suppressed)"
9+
discord_bot_auto_channel_sync: "Enable chat sync on identically named channels/Categories (takes precedence over Discourse Announcement Topic)"
1010
discord_bot_message_copy_topic_size_limit: "Message history copy: number of Discord messages per Discourse Topic"
1111
discord_bot_post_announcement_categories: "Announce new Posts in here within the announcements channel"
1212
discord_bot_topic_announcement_categories: "Announce new Topics in here within the announcements channel"

lib/bot_commands.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,31 @@ def self.manage_discord_commands(bot)
2828
end
2929
end
3030
system_user = User.find_by(id: -1)
31+
3132
total_copied_messages = 0
32-
# .sort_by(|m| -m.id)
33+
3334
past_messages.reverse.in_groups_of(SiteSetting.discord_bot_message_copy_topic_size_limit.to_i).each_with_index do |message_batch, index|
3435
current_topic_id = nil
3536
message_batch.each_with_index do |pm, topic_index|
3637
next if pm.nil?
3738
raw = pm.to_s
39+
40+
associated_user = UserAssociatedAccount.find_by(provider_uid: pm.author.id)
41+
unless associated_user.nil?
42+
posting_user = User.find_by(id: associated_user.user_id)
43+
else
44+
posting_user = system_user
45+
end
46+
3847
if topic_index == 0 && destination_topic.nil?
39-
new_post = PostCreator.create!(system_user, title: I18n.t("discord_bot.commands.disccopy.discourse_topic_title", channel: event.channel.name) + (past_messages.count <= SiteSetting.discord_bot_message_copy_topic_size_limit ? "" : " #{index + 1}") , raw: raw, category: destination_category.id, skip_validations: true)
48+
new_post = PostCreator.create!(posting_user, title: I18n.t("discord_bot.commands.disccopy.discourse_topic_title", channel: event.channel.name) + (past_messages.count <= SiteSetting.discord_bot_message_copy_topic_size_limit ? "" : " #{index + 1}") , raw: raw, category: destination_category.id, skip_validations: true)
4049
total_copied_messages += 1
4150
current_topic_id = new_post.topic.id
4251
elsif !destination_topic.nil? || !current_topic_id.nil?
4352
if current_topic_id.nil?
4453
current_topic_id = destination_topic.id
4554
end
46-
new_post = PostCreator.create!(system_user, raw: raw, topic_id: current_topic_id, skip_validations: true)
55+
new_post = PostCreator.create!(posting_user, raw: raw, topic_id: current_topic_id, skip_validations: true)
4756
total_copied_messages += 1
4857
else
4958
event.respond I18n.t("discord_bot.commands.disccopy.error.unable_to_determine_topic_id")

lib/discord_events_handlers.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ module ::DiscordBot::DiscordEventsHandlers
33
module TransmitAnnouncement
44
extend Discordrb::EventContainer
55
message do |event|
6-
# Copy the message to the assigned Discourse announcement Topic if assigned in plugin settings
7-
discourse_announcement_topic = Topic.find_by(id: SiteSetting.discord_bot_discourse_announcement_topic_id)
8-
unless discourse_announcement_topic.nil?
9-
system_user = User.find_by(id: -1)
10-
raw = event.message.to_s
11-
new_post = PostCreator.new(system_user, raw: raw, topic_id: discourse_announcement_topic.id)
12-
new_post.create!
6+
7+
system_user = User.find_by(id: -1)
8+
9+
associated_user = UserAssociatedAccount.find_by(provider_uid: event.message.author.id)
10+
unless associated_user.nil?
11+
posting_user = User.find_by(id: associated_user.user_id)
12+
else
13+
posting_user = system_user
1314
end
15+
1416
if SiteSetting.discord_bot_auto_channel_sync
1517
matching_category = Category.find_by(name: event.message.channel.name)
1618
unless matching_category.nil?
1719
raw = event.message.to_s
18-
system_user = User.find_by(id: -1)
1920
if !(target_topic = Topic.find_by(title: I18n.t("discord_bot.discord_events.auto_message_copy.default_topic_title", channel_name: matching_category.name))).nil?
20-
new_post = PostCreator.create!(system_user, raw: raw, topic_id: target_topic.id, skip_validations: true)
21+
new_post = PostCreator.create!(posting_user, raw: raw, topic_id: target_topic.id, skip_validations: true)
2122
else
22-
new_post = PostCreator.create!(system_user, title: I18n.t("discord_bot.discord_events.auto_message_copy.default_topic_title", channel_name: matching_category.name), raw: raw, category: matching_category.id, skip_validations: true)
23+
new_post = PostCreator.create!(posting_user, title: I18n.t("discord_bot.discord_events.auto_message_copy.default_topic_title", channel_name: matching_category.name), raw: raw, category: matching_category.id, skip_validations: true)
24+
end
25+
else
26+
# Copy the message to the assigned Discourse announcement Topic if assigned in plugin settings
27+
discourse_announcement_topic = Topic.find_by(id: SiteSetting.discord_bot_discourse_announcement_topic_id)
28+
unless discourse_announcement_topic.nil?
29+
raw = event.message.to_s
30+
new_post = PostCreator..create!(posting_user, raw: raw, topic_id: discourse_announcement_topic.id)
2331
end
2432
end
2533
end

0 commit comments

Comments
 (0)