Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit 61a727a

Browse files
authored
FIX: Sharing to Discord forum channels requires a thread_name (#251)
Discourse is able to share topics and replies to normal Discord channels, but it doesn't work with forum channels, which require an additional `thread_name` parameter. This change fixes the issue by checking for the specific error the Discord returns when trying to share to a forum channel, adding the `thread_name`, then trying again. This is intentionally a minimal fix, additional work would be required to make the sharing more closely fit the style of Discord forum channels.
1 parent 954009c commit 61a727a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/discourse_chat_integration/provider/discord/discord_provider.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def self.trigger_notification(post, channel, rule)
8888
message = generate_discord_message(post)
8989
response = send_message(webhook_url, message)
9090

91+
# If the message fails to send, it might be because it's a forum channel.
92+
if !response.kind_of?(Net::HTTPSuccess)
93+
error = JSON.parse(response.body)
94+
# Error code 220001 occurs when trying to post to a forum channel without a thread_name.
95+
if error["code"] == 220_001
96+
message[:thread_name] = message[:embeds][0][:title]
97+
response = send_message(webhook_url, message)
98+
end
99+
end
100+
91101
if !response.kind_of?(Net::HTTPSuccess)
92102
raise ::DiscourseChatIntegration::ProviderError.new(
93103
info: {

spec/lib/discourse_chat_integration/provider/discord/discord_provider_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,28 @@
4141
stub1 =
4242
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true").to_return(
4343
status: 400,
44+
body: '{"message": "This is an error!", "code": 400}',
4445
)
4546
expect(stub1).to have_been_requested.times(0)
4647
expect { described_class.trigger_notification(post, chan1, nil) }.to raise_exception(
4748
::DiscourseChatIntegration::ProviderError,
4849
)
4950
expect(stub1).to have_been_requested.once
5051
end
52+
53+
it "handles posting to forum channels" do
54+
stub1 =
55+
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true")
56+
.with { |request| !JSON.parse(request.body)["thread_name"].present? }
57+
.to_return(status: 400, body: '{"message": "This is an error!", "code": 220001}')
58+
stub2 =
59+
stub_request(:post, "https://discord.com/api/webhooks/1234/abcd?wait=true")
60+
.with { |request| JSON.parse(request.body)["thread_name"].present? }
61+
.to_return(status: 200)
62+
described_class.trigger_notification(post, chan1, nil)
63+
expect(stub1).to have_been_requested.once
64+
expect(stub2).to have_been_requested.once
65+
end
5166
end
5267

5368
describe ".get_channel_by_name" do

0 commit comments

Comments
 (0)