Skip to content

Commit

Permalink
do nothing if discord client not found
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Jun 26, 2024
1 parent 8d251fc commit f28437f
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions core/lib/canary/clients/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,28 @@ defmodule Canary.Clients.Discord do
defp respond(%Channel{id: thread_id, parent_id: channel_id, guild_id: guild_id}, user_msg) do
source_ids = find_source_ids(guild_id, channel_id)

if length(source_ids) == 0 do
send(thread_id, user_msg, @no_source_message)
else
query = strip(user_msg.content)

{:ok, pid} = Canary.Sessions.find_or_start_session(thread_id)
GenServer.call(pid, {:submit, :website, %{query: query, source_ids: source_ids}})
Api.start_typing(thread_id)

receive do
{:complete, %{content: content}} -> send(thread_id, user_msg, content)
{:progress, _} -> Api.start_typing(thread_id)
_ -> :ignore
after
@timeout ->
send(thread_id, user_msg, @failed_message)
end
cond do
source_ids == nil ->
:ignore

length(source_ids) == 0 ->
send(thread_id, user_msg, @no_source_message)

true ->
query = strip(user_msg.content)

{:ok, pid} = Canary.Sessions.find_or_start_session(thread_id)
GenServer.call(pid, {:submit, :website, %{query: query, source_ids: source_ids}})
Api.start_typing(thread_id)

receive do
{:complete, %{content: content}} -> send(thread_id, user_msg, content)
{:progress, _} -> Api.start_typing(thread_id)
_ -> :ignore
after
@timeout ->
send(thread_id, user_msg, @failed_message)
end
end
end

Expand Down Expand Up @@ -94,7 +99,7 @@ defmodule Canary.Clients.Discord do
})
|> Ash.read_one!()

if client == nil, do: [], else: client.sources |> Enum.map(& &1.id)
if client == nil, do: nil, else: client.sources |> Enum.map(& &1.id)
end

defp strip(s), do: s |> String.replace(~r/<@!?\d+>/, "") |> String.trim()
Expand Down

0 comments on commit f28437f

Please sign in to comment.