diff options
author | Elizabeth (Lizzy) Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-22 23:32:55 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-22 23:32:55 -0600 |
commit | f5773f606a738e092845597f2af77e9602b06552 (patch) | |
tree | f5611aa4fef4c5d972f18451e2127762ffd54bd7 /lib/chessh/discord | |
parent | fb1825e964ee5ebb8427c56eaa34e9437c56c5ab (diff) | |
download | chessh-f5773f606a738e092845597f2af77e9602b06552.tar.gz chessh-f5773f606a738e092845597f2af77e9602b06552.zip |
README changes, create thread on join, update package-lock (#21)
Diffstat (limited to 'lib/chessh/discord')
-rw-r--r-- | lib/chessh/discord/notifier.ex | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/lib/chessh/discord/notifier.ex b/lib/chessh/discord/notifier.ex index 7751041..93d27f1 100644 --- a/lib/chessh/discord/notifier.ex +++ b/lib/chessh/discord/notifier.ex @@ -54,11 +54,30 @@ defmodule Chessh.DiscordNotifier do {:noreply, state} end + defp send_notification({:player_joined, game_id}) do + case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do + %Game{ + status: :continue, + dark_player: %Player{discord_id: dark_player_discord_id}, + light_player: %Player{discord_id: light_player_discord_id} + } = game -> + game = maybe_put_new_thread_on_game(game) + + post_discord( + game.discord_thread_id, + %{ + content: + "Everyone (<@#{dark_player_discord_id}> as the dark pieces, <@#{light_player_discord_id}> as light) has joined! Play chess!" + } + ) + + _ -> + nil + end + end + defp send_notification({:move_reminder, game_id}) do - [min_delta_t, remind_move_channel_id] = - Application.get_env(:chessh, DiscordNotifications) - |> Keyword.take([:game_move_notif_delay_ms, :remind_move_channel_id]) - |> Keyword.values() + min_delta_t = Application.get_env(:chessh, DiscordNotifications)[:game_move_notif_delay_ms] case Repo.get(Game, game_id) |> Repo.preload([:dark_player, :light_player]) do %Game{ @@ -68,23 +87,10 @@ defmodule Chessh.DiscordNotifier do last_move: last_move, updated_at: last_updated, moves: move_count, - status: :continue, - discord_thread_id: discord_thread_id + status: :continue } = game -> delta_t = NaiveDateTime.diff(NaiveDateTime.utc_now(), last_updated, :millisecond) - - game = - if is_nil(discord_thread_id) do - {:ok, game} = - Game.changeset(game, %{ - discord_thread_id: make_private_discord_thread_id(remind_move_channel_id, game) - }) - |> Repo.update() - - game - else - game - end + game = maybe_put_new_thread_on_game(game) if delta_t >= min_delta_t do post_discord( @@ -233,4 +239,21 @@ defmodule Chessh.DiscordNotifier do bot_token = Application.get_env(:chessh, DiscordNotifications)[:discord_bot_token] {'Authorization', 'Bot #{bot_token}'} end + + defp maybe_put_new_thread_on_game(%Game{discord_thread_id: discord_thread_id} = game) do + remind_move_channel_id = + Application.get_env(:chessh, DiscordNotifications)[:remind_move_channel_id] + + if is_nil(discord_thread_id) do + {:ok, game} = + Game.changeset(game, %{ + discord_thread_id: make_private_discord_thread_id(remind_move_channel_id, game) + }) + |> Repo.update() + + game + else + game + end + end end |