diff --git a/lib/teiserver/bridge/bridge_server.ex b/lib/teiserver/bridge/bridge_server.ex index 5b729dd72..492fec442 100644 --- a/lib/teiserver/bridge/bridge_server.ex +++ b/lib/teiserver/bridge/bridge_server.ex @@ -3,6 +3,8 @@ defmodule Teiserver.Bridge.BridgeServer do The server used to read events from Teiserver and then use the DiscordBridgeBot to send onwards """ use GenServer + alias Teiserver.Communication.DiscordChannelLib + alias Teiserver.Communication alias Teiserver.{Account, Room, CacheUser} alias Teiserver.Chat.WordLib alias Phoenix.PubSub @@ -43,16 +45,7 @@ defmodule Teiserver.Bridge.BridgeServer do @spec send_direct_message(T.user_id(), String.t()) :: :ok | nil def send_direct_message(userid, message) do - user = Account.get_user_by_id(userid) - - cond do - user.discord_dm_channel && user.discord_dm_channel_id == nil -> - nil - - true -> - channel_id = user.discord_dm_channel_id || user.discord_dm_channel - Api.create_message(channel_id, message) - end + Communication.send_discord_dm(userid, message) end @impl true @@ -218,19 +211,8 @@ defmodule Teiserver.Bridge.BridgeServer do def handle_info(%{channel: "teiserver_server", event: :started}, state) do if Config.get_site_config_cache("teiserver.Bridge from server") do - # Main - channel_id = Config.get_site_config_cache("teiserver.Discord channel #main") - - if channel_id do - Api.create_message(channel_id, "Teiserver startup for node #{Teiserver.node_name()}") - end - - # Server - channel_id = Config.get_site_config_cache("teiserver.Discord channel #server-updates") - - if channel_id do - Api.create_message(channel_id, "Teiserver startup for node #{Teiserver.node_name()}") - end + Communication.new_discord_message("Main chat", "Teiserver startup for node #{Teiserver.node_name()}") + Communication.new_discord_message("Server updates", "Teiserver startup for node #{Teiserver.node_name()}") end {:noreply, state} @@ -238,11 +220,7 @@ defmodule Teiserver.Bridge.BridgeServer do def handle_info(%{channel: "teiserver_server", event: :prep_stop}, state) do if Config.get_site_config_cache("teiserver.Bridge from server") do - channel_id = Config.get_site_config_cache("teiserver.Discord channel #server-updates") - - if channel_id do - Api.create_message(channel_id, "Teiserver shutdown for node #{Teiserver.node_name()}") - end + Communication.new_discord_message("Server updates", "Teiserver shutdown for node #{Teiserver.node_name()}") end {:noreply, state} @@ -322,62 +300,27 @@ defmodule Teiserver.Bridge.BridgeServer do end defp build_local_caches(state) do - channel_lookup = - [ - "teiserver.Discord channel #main", - "teiserver.Discord channel #newbies", - "teiserver.Discord channel #promote", - "teiserver.Discord channel #moderation-reports", - "teiserver.Discord channel #moderation-actions", - "teiserver.Discord channel #server-updates", - "teiserver.Discord channel #telemetry-infologs", - "teiserver.Discord forum #gdt-discussion", - "teiserver.Discord forum #gdt-voting" - ] - |> Enum.map(fn key -> - channel_id = Config.get_site_config_cache(key) - - if channel_id do - [_, room] = String.split(key, "#") - - {room, channel_id} - end - end) - |> Enum.reject(&(&1 == nil)) - |> Map.new() - room_lookup = [ - "teiserver.Discord channel #main", - "teiserver.Discord channel #newbies", - "teiserver.Discord channel #promote" + %{channel_id: DiscordChannelLib.get_discord_channel("Main chat"), room: "#main"}, + %{channel_id: DiscordChannelLib.get_discord_channel("New player chat"), room: "#newbies"}, + %{channel_id: DiscordChannelLib.get_discord_channel("Looking for players"), room: "#promote"} ] - |> Enum.map(fn key -> - channel_id = Config.get_site_config_cache(key) - - if channel_id do - [_, room] = String.split(key, "#") - - {channel_id, room} - end - end) - |> Enum.reject(&(&1 == nil)) - |> Map.new() + |> Enum.reject(fn %{channel_id: nil} -> true; _ -> false end) + |> Enum.map(fn %{channel_id: channel, room: r} -> %{channel_id: channel.channel_id, room: r} end) - Map.values(room_lookup) - |> Enum.each(fn room_name -> - Room.get_or_make_room(room_name, state.user.id) - Room.add_user_to_room(state.user.id, room_name) + room_lookup + |> Enum.each(fn channel_room -> + Room.get_or_make_room(channel_room.room, state.user.id) + Room.add_user_to_room(state.user.id, channel_room.room) - :ok = PubSub.unsubscribe(Teiserver.PubSub, "room:#{room_name}") - :ok = PubSub.subscribe(Teiserver.PubSub, "room:#{room_name}") + :ok = PubSub.unsubscribe(Teiserver.PubSub, "room:#{channel_room.room}") + :ok = PubSub.subscribe(Teiserver.PubSub, "room:#{channel_room.room}") end) Teiserver.store_put(:application_metadata_cache, :discord_room_lookup, room_lookup) - Teiserver.store_put(:application_metadata_cache, :discord_channel_lookup, channel_lookup) Map.merge(state, %{ - channel_lookup: channel_lookup, room_lookup: room_lookup }) end @@ -389,7 +332,7 @@ defmodule Teiserver.Bridge.BridgeServer do message |> convert_emoticons - Api.create_message(channel, "**#{author}**: #{new_message}") + Communication.new_discord_message(channel, "**#{author}**: #{new_message}") end defp convert_emoticons(message) do diff --git a/lib/teiserver/bridge/chat_commands.ex b/lib/teiserver/bridge/chat_commands.ex index 4f0f4defe..abc2a35ff 100644 --- a/lib/teiserver/bridge/chat_commands.ex +++ b/lib/teiserver/bridge/chat_commands.ex @@ -1,5 +1,6 @@ defmodule Teiserver.Bridge.ChatCommands do @moduledoc false + alias Teiserver.Communication.DiscordChannelLib alias Teiserver.{Account, CacheUser, Communication, Config, Logging} alias Teiserver.Data.Types, as: T alias Teiserver.Bridge.UnitNames @@ -35,18 +36,16 @@ defmodule Teiserver.Bridge.ChatCommands do end def handle_command({_user, _discord_id, message_id}, "gdt", _remaining, channel_id) do - gdt_discussion_channel_id = - Config.get_site_config_cache("teiserver.Discord channel #gdt-discussion") - + gdt_discussion_channel_id = DiscordChannelLib.get_discord_channel("GDT discussion") if gdt_discussion_channel_id do # Post message to channel - Api.create_message( - channel_id, + Communication.new_discord_message( + "GDT discussion", "Thank you for your suggestion, the game design team will be discussing it. Once they have finished discussing it they will vote on it and post an update to this thread." ) # Delete the message that was posted - Api.delete_message(channel_id, message_id) + Communication.delete_discord_message(channel_id, message_id) # channel_id = 1071140326644920353 {:ok, channel} = Api.get_channel(channel_id) @@ -200,7 +199,7 @@ defmodule Teiserver.Bridge.ChatCommands do end defp reply(channel, message) do - Api.create_message(channel, message) + Communication.new_discord_message(channel, message) :ignore end end diff --git a/lib/teiserver/bridge/commands/textcb_command.ex b/lib/teiserver/bridge/commands/textcb_command.ex index 8c55149b9..78f3d848e 100644 --- a/lib/teiserver/bridge/commands/textcb_command.ex +++ b/lib/teiserver/bridge/commands/textcb_command.ex @@ -57,13 +57,6 @@ defmodule Teiserver.Bridge.Commands.TextcbCommand do trigger: options_map["reference"] }) - main_id = Config.get_site_config_cache("teiserver.Discord channel #main") - - if interaction.channel_id == main_id do - bridge_user_id = BridgeServer.get_bridge_userid() - Room.send_message(bridge_user_id, "main", text_callback.response) - end - Communication.set_last_triggered_time(text_callback, interaction.channel_id) %{ diff --git a/lib/teiserver/bridge/discord_bridge_bot.ex b/lib/teiserver/bridge/discord_bridge_bot.ex index 46c0db25f..f9a28db7c 100644 --- a/lib/teiserver/bridge/discord_bridge_bot.ex +++ b/lib/teiserver/bridge/discord_bridge_bot.ex @@ -127,13 +127,13 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do response = CommandLib.handle_command(interaction, options_map) - # response = case data.name do - # "textcb" -> - # Teiserver.Bridge.TextcbCommand.execute(interaction, options_map) + response = case data.name do + "textcb" -> + Teiserver.Bridge.TextcbCommand.execute(interaction, options_map) - # _ -> - # nil - # end + _ -> + nil + end if response do Api.create_interaction_response(interaction, response) @@ -229,11 +229,8 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do @spec new_infolog(Teiserver.Telemetry.Infolog.t()) :: any def new_infolog(infolog) do - channel_id = Config.get_site_config_cache("teiserver.Discord channel #telemetry-infologs") - post_to_discord = cond do - channel_id == nil -> false infolog.metadata["shorterror"] == "Errorlog" -> false infolog.metadata["private"] == true -> false true -> true @@ -251,7 +248,7 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do ] |> Enum.join("\n") - Api.create_message(channel_id, message) + Communication.new_discord_message("Telemetry infologs", message) end end @@ -261,10 +258,10 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do channel = cond do report.type == "actions" -> - Config.get_site_config_cache("teiserver.Discord channel #overwatch-reports") + "Overwatch reports" true -> - Config.get_site_config_cache("teiserver.Discord channel #moderation-reports") + "Moderation reports" end if channel do @@ -298,7 +295,7 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do msg = "#{report.target.name} was reported by #{report.reporter.name} because #{report.type}/#{report.sub_type} #{match_icon} - #{report.extra_text} - #{url}#{outstanding_msg}" - Api.create_message(channel, "Moderation report: #{msg}") + Communication.new_discord_message("Moderation report:", msg) end end diff --git a/lib/teiserver/bridge/message_commands.ex b/lib/teiserver/bridge/message_commands.ex index d0734d28c..56ce48563 100644 --- a/lib/teiserver/bridge/message_commands.ex +++ b/lib/teiserver/bridge/message_commands.ex @@ -1,5 +1,7 @@ defmodule Teiserver.Bridge.MessageCommands do @moduledoc false + alias Teiserver.Communication + alias Credo.CLI.Command alias Teiserver.{CacheUser, Account} alias Teiserver.Account.AccoladeLib alias Teiserver.Helper.NumberHelper @@ -248,6 +250,6 @@ defmodule Teiserver.Bridge.MessageCommands do defp reply(channel, message) when is_list(message), do: reply(channel, Enum.join(message, "\n")) defp reply(channel, message) do - Api.create_message(channel, message) + Communication.new_discord_message(channel, message) end end diff --git a/lib/teiserver/communication/libs/discord_channel_lib.ex b/lib/teiserver/communication/libs/discord_channel_lib.ex index 2528c2e3a..674958350 100644 --- a/lib/teiserver/communication/libs/discord_channel_lib.ex +++ b/lib/teiserver/communication/libs/discord_channel_lib.ex @@ -19,7 +19,9 @@ defmodule Teiserver.Communication.DiscordChannelLib do "Server updates", "Error updates", "Github updates", - "Dev channel" + "Dev channel", + "Telemetry infologs", + "GDT discussion" ] end diff --git a/lib/teiserver/libs/teiserver_configs.ex b/lib/teiserver/libs/teiserver_configs.ex index 0060a0bba..b3d6d18bb 100644 --- a/lib/teiserver/libs/teiserver_configs.ex +++ b/lib/teiserver/libs/teiserver_configs.ex @@ -450,86 +450,6 @@ defmodule Teiserver.TeiserverConfigs do default: true }) - add_site_config_type(%{ - key: "teiserver.Discord channel #main", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to bridge with #main" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #newbies", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to bridge with #newbies" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #promote", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to bridge for promoting games" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #overwatch-reports", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to post overwatch specific reports" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #moderation-reports", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to post moderation-reports" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #moderation-actions", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to post moderation-actions" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #server-updates", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to post server updates" - }) - - add_site_config_type(%{ - key: "teiserver.Discord channel #telemetry-infologs", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the channel to post infologs" - }) - - add_site_config_type(%{ - key: "teiserver.Discord forum #gdt-discussion", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the forum for starting GDT discussions" - }) - - add_site_config_type(%{ - key: "teiserver.Discord forum #gdt-voting", - section: "Discord", - type: "integer", - permissions: ["Admin"], - description: "The discord ID for the forum for starting GDT voting" - }) - # User number channels add_site_config_type(%{ key: "teiserver.Discord counter clients",