Skip to content

Commit

Permalink
Disocrd channels improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
L-e-x-o-n committed Aug 7, 2024
1 parent b6afa28 commit 4595f5a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 184 deletions.
93 changes: 18 additions & 75 deletions lib/teiserver/bridge/bridge_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -218,31 +211,16 @@ 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}
end

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}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions lib/teiserver/bridge/chat_commands.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Teiserver.Bridge.ChatCommands do
@moduledoc false
alias Teiserver.Communication.DiscordChannelLib
alias Teiserver.{Account, CacheUser, Communication, Config, Logging}

Check warning on line 4 in lib/teiserver/bridge/chat_commands.ex

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Config

Check warning on line 4 in lib/teiserver/bridge/chat_commands.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unused alias Config

Check warning on line 4 in lib/teiserver/bridge/chat_commands.ex

View workflow job for this annotation

GitHub Actions / mix format

unused alias Config
alias Teiserver.Data.Types, as: T
alias Teiserver.Bridge.UnitNames
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
7 changes: 0 additions & 7 deletions lib/teiserver/bridge/commands/textcb_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

%{
Expand Down
23 changes: 10 additions & 13 deletions lib/teiserver/bridge/discord_bridge_bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ defmodule Teiserver.Bridge.DiscordBridgeBot do

response = CommandLib.handle_command(interaction, options_map)

Check warning on line 128 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / Build and test

variable "response" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 128 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

variable "response" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 128 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / mix format

variable "response" is unused (if the variable is not meant to be used, prefix it with an underscore)

# 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)

Check warning on line 132 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / Build and test

Teiserver.Bridge.TextcbCommand.execute/2 is undefined (module Teiserver.Bridge.TextcbCommand is not available or is yet to be defined)

Check warning on line 132 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

Teiserver.Bridge.TextcbCommand.execute/2 is undefined (module Teiserver.Bridge.TextcbCommand is not available or is yet to be defined)

Check warning on line 132 in lib/teiserver/bridge/discord_bridge_bot.ex

View workflow job for this annotation

GitHub Actions / mix format

Teiserver.Bridge.TextcbCommand.execute/2 is undefined (module Teiserver.Bridge.TextcbCommand is not available or is yet to be defined)

# _ ->
# nil
# end
_ ->
nil
end

if response do
Api.create_interaction_response(interaction, response)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/teiserver/bridge/message_commands.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Teiserver.Bridge.MessageCommands do
@moduledoc false
alias Teiserver.Communication
alias Credo.CLI.Command

Check warning on line 4 in lib/teiserver/bridge/message_commands.ex

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Command

Check warning on line 4 in lib/teiserver/bridge/message_commands.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

unused alias Command

Check warning on line 4 in lib/teiserver/bridge/message_commands.ex

View workflow job for this annotation

GitHub Actions / mix format

unused alias Command
alias Teiserver.{CacheUser, Account}
alias Teiserver.Account.AccoladeLib
alias Teiserver.Helper.NumberHelper
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion lib/teiserver/communication/libs/discord_channel_lib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ defmodule Teiserver.Communication.DiscordChannelLib do
"Server updates",
"Error updates",
"Github updates",
"Dev channel"
"Dev channel",
"Telemetry infologs",
"GDT discussion"
]
end

Expand Down
80 changes: 0 additions & 80 deletions lib/teiserver/libs/teiserver_configs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4595f5a

Please sign in to comment.