Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disocrd channels improvements #269

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 34 additions & 73 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,26 @@ 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")
Communication.new_discord_message(
"Main chat",
"Teiserver startup for node #{Teiserver.node_name()}"
)

if channel_id do
Api.create_message(channel_id, "Teiserver startup for node #{Teiserver.node_name()}")
end
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 +310,35 @@ 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
|> 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)
|> Enum.reject(&(&1 == nil))
|> Map.new()

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 +350,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
14 changes: 7 additions & 7 deletions lib/teiserver/bridge/chat_commands.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Teiserver.Bridge.ChatCommands do
@moduledoc false
alias Teiserver.{Account, CacheUser, Communication, Config, Logging}
alias Teiserver.Communication.DiscordChannelLib
alias Teiserver.{Account, CacheUser, Communication, Logging}
alias Teiserver.Data.Types, as: T
alias Teiserver.Bridge.UnitNames
alias Nostrum.Api
Expand Down Expand Up @@ -35,18 +36,17 @@ 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 +200,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
10 changes: 1 addition & 9 deletions lib/teiserver/bridge/commands/textcb_command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Teiserver.Bridge.Commands.TextcbCommand do
@moduledoc """
Calls the bot and tells it to post one of the text-callbacks
"""
alias Teiserver.Bridge.{BridgeServer}
alias Teiserver.{Communication, Room, Logging, Config}
alias Teiserver.{Communication, Logging}

@behaviour Teiserver.Bridge.BridgeCommandBehaviour

Expand Down Expand Up @@ -57,13 +56,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
21 changes: 9 additions & 12 deletions lib/teiserver/bridge/discord_bridge_bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
alias Teiserver.{Room, Moderation, Communication}
alias Teiserver.Bridge.{BridgeServer, MessageCommands, ChatCommands, CommandLib}
alias Teiserver.{Config}
alias Nostrum.Api

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

View workflow job for this annotation

GitHub Actions / Dialyzer

unused alias Api

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

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Api

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

View workflow job for this annotation

GitHub Actions / mix format

unused alias Api
require Logger

@emoticon_map %{
Expand Down Expand Up @@ -125,7 +125,7 @@
{name, value}
end)

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 / 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 / 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 / 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" ->
Expand All @@ -135,11 +135,11 @@
# nil
# end

if response do
Api.create_interaction_response(interaction, response)
else
:ignore
end
# if response do
# Api.create_interaction_response(interaction, response)
# else
# :ignore
# end
end

def handle_event({:READY, ready_data, _ws}) do
Expand Down Expand Up @@ -229,11 +229,8 @@

@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 @@
]
|> Enum.join("\n")

Api.create_message(channel_id, message)
Communication.new_discord_message("Telemetry infologs", message)
end
end

Expand All @@ -261,10 +258,10 @@
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 @@
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,10 +1,12 @@
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 / Dialyzer

unused alias 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 / mix format

unused alias Command
alias Teiserver.{CacheUser, Account}
alias Teiserver.Account.AccoladeLib
alias Teiserver.Helper.NumberHelper
alias Teiserver.Bridge.UnitNames
alias Nostrum.Api

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

View workflow job for this annotation

GitHub Actions / Dialyzer

unused alias Api

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

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Api

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

View workflow job for this annotation

GitHub Actions / mix format

unused alias Api
require Logger

@unauth ~w(discord)
Expand Down Expand Up @@ -248,6 +250,6 @@
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"
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that got me confused for a while trying to figure out where these things lived (or not actually).


# User number channels
add_site_config_type(%{
key: "teiserver.Discord counter clients",
Expand Down
Loading