Skip to content

Commit 8086aa0

Browse files
committed
fix: restore discord bot replies
1 parent 8f94576 commit 8086aa0

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

elixir/threadr/lib/threadr/ingest/bot_qa.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ defmodule Threadr.Ingest.BotQA do
256256
end
257257

258258
defp send_discord_reply(api, channel_id, content) do
259-
call_adapter(api, :create_message, [channel_id, %{content: content}])
259+
call_adapter(api, :create_message, [parse_discord_channel_id(channel_id), %{content: content}])
260260
end
261261

262262
defp format_reply(%{platform: "irc", reply_prefix: prefix}, content) do
@@ -371,6 +371,17 @@ defmodule Threadr.Ingest.BotQA do
371371
Keyword.get(config, :discord_api, Nostrum.Api)
372372
end
373373

374+
defp parse_discord_channel_id(channel_id) when is_integer(channel_id), do: channel_id
375+
376+
defp parse_discord_channel_id(channel_id) when is_binary(channel_id) do
377+
case Integer.parse(channel_id) do
378+
{parsed, ""} -> parsed
379+
_ -> channel_id
380+
end
381+
end
382+
383+
defp parse_discord_channel_id(channel_id), do: channel_id
384+
374385
defp emit_question_detected(config, request) do
375386
Threadr.Ingest.emit_runtime_event(config, :question_detected, %{
376387
platform: request.platform,

elixir/threadr/test/support/ingest_test_support.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,10 @@ defmodule Threadr.TestDiscordApi do
110110
{:ok, %{id: "discord-reply-1"}}
111111
end
112112
end
113+
114+
defmodule Threadr.TestStrictDiscordApi do
115+
def create_message(channel_id, options, pid) when is_integer(channel_id) do
116+
send(pid, {:discord_api_create_message, channel_id, options})
117+
{:ok, %{id: "discord-reply-1"}}
118+
end
119+
end

elixir/threadr/test/threadr/ingest/discord/bot_qa_test.exs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule Threadr.Ingest.Discord.BotQATest do
6161
)
6262

6363
assert_receive {:published_envelope, _envelope}
64-
assert_receive {:discord_api_create_message, "12345", %{content: content}}
64+
assert_receive {:discord_api_create_message, 12345, %{content: content}}
6565
assert content =~ "<@1>"
6666
assert content =~ "what did Alice and Bob talk about last week?"
6767
end
@@ -99,6 +99,65 @@ defmodule Threadr.Ingest.Discord.BotQATest do
9999
refute_receive {:discord_api_create_message, _channel_id, _options}, 200
100100
end
101101

102+
test "casts Discord channel ids for the real API-style adapter" do
103+
tenant = create_tenant!("Discord Bot QA Strict API")
104+
actor = create_actor!(tenant.schema_name, "alice")
105+
channel = create_channel!(tenant.schema_name, "12345")
106+
107+
message =
108+
create_message!(
109+
tenant.schema_name,
110+
actor.id,
111+
channel.id,
112+
"Alice and Bob discussed endpoint isolation last week."
113+
)
114+
115+
create_embedding!(tenant.schema_name, message.id, [0.4, 0.5, 0.6])
116+
117+
Consumer.put_config(
118+
tenant_subject_name: tenant.subject_name,
119+
tenant_id: tenant.id,
120+
bot_id: "bot-123",
121+
channels: ["12345"],
122+
publisher: {Threadr.TestPublisher, self()},
123+
discord_api: {Threadr.TestStrictDiscordApi, self()},
124+
embedding_provider: Threadr.TestEmbeddingProvider,
125+
embedding_model: "test-embedding-model",
126+
generation_provider: Threadr.TestGenerationProvider,
127+
generation_model: "test-chat",
128+
discord: %{
129+
identity: %{
130+
user_id: "999",
131+
username: "threadr",
132+
global_name: "Threadr"
133+
}
134+
}
135+
)
136+
137+
:ok =
138+
Consumer.handle_event(
139+
{:MESSAGE_CREATE,
140+
%Message{
141+
id: 999_888_778,
142+
channel_id: 12_345,
143+
guild_id: 54_321,
144+
content: "<@999> hello",
145+
author: %User{
146+
id: 1,
147+
username: "alice",
148+
global_name: "Alice Display",
149+
bot: false
150+
},
151+
mentions: [%User{id: 999, username: "threadr", global_name: "Threadr"}]
152+
}, nil}
153+
)
154+
155+
assert_receive {:published_envelope, _envelope}
156+
assert_receive {:discord_api_create_message, 12345, %{content: content}}
157+
assert content =~ "<@1>"
158+
assert content =~ "hello"
159+
end
160+
102161
defp create_tenant!(prefix) do
103162
suffix = System.unique_integer([:positive])
104163

0 commit comments

Comments
 (0)