HomingPigeon/lib/formatter.ex

144 lines
3.3 KiB
Elixir
Raw Normal View History

2020-07-16 23:11:18 -04:00
defmodule Discordirc.Formatter do
@moduledoc """
Transforms messages to/from discord from/to irc
"""
2020-07-17 02:35:27 -04:00
alias Nostrum.Api, as: DiscordAPI
2022-11-07 10:42:08 -05:00
alias Discordirc.DiscordInfo
2022-11-07 10:43:39 -05:00
@discordcdn "https://cdn.discordapp.com/"
2020-07-18 04:22:57 -04:00
def from_irc(nick, msg, ctcp \\ false) do
2020-07-16 23:11:18 -04:00
# strip or replace IRC formatting.
fmsg =
msg
|> :re.replace("\x02(.*?)\x02", "**\\g1**", [:global])
|> :re.replace("\x02(.*)", "**\\g1**")
|> :re.replace("\x01|\x03[0123456789]*(,[0123456789]*)?", "", [:global])
case ctcp do
true ->
case fmsg do
2020-09-04 05:45:26 -04:00
x when is_binary(x) -> "_#{x}_"
x when is_list(x) -> "_#{List.to_string(x)}_"
2020-07-16 23:11:18 -04:00
end
false ->
case fmsg do
2020-09-04 05:45:26 -04:00
x when is_binary(x) -> "#{x}"
x when is_list(x) -> "#{List.to_string(x)}"
2020-07-16 23:11:18 -04:00
end
end
end
2022-11-07 10:42:08 -05:00
def get_id_info([match, type, id], guild) do
i = String.to_integer(id)
2022-11-07 10:43:39 -05:00
2022-11-07 10:42:08 -05:00
case type do
"#" ->
2022-11-07 10:43:39 -05:00
{match, "#" <> DiscordInfo.get_channel_name_by_id(i)}
"@" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
2022-11-07 10:42:08 -05:00
"@!" ->
2022-11-07 10:43:39 -05:00
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
2022-11-07 10:42:08 -05:00
"@&" ->
2022-11-07 10:43:39 -05:00
{match, "@" <> DiscordInfo.get_role_name_by_id(guild, i)}
2020-07-18 04:22:57 -04:00
end
end
2022-11-07 10:42:08 -05:00
def do_replace(str, [head | tail]) do
2022-11-07 10:43:39 -05:00
{fst, snd} = head
do_replace(String.replace(str, fst, snd, global: true), tail)
2020-07-17 02:35:27 -04:00
end
2022-11-07 10:43:39 -05:00
2022-11-07 10:42:08 -05:00
def do_replace(str, []) do
str
2020-07-17 02:35:27 -04:00
end
2022-11-07 10:43:39 -05:00
2020-07-18 04:22:57 -04:00
def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do
2022-11-07 10:42:08 -05:00
pattern = ~r/\<(\@[!&]?|#)(\d+)\>/um
2020-07-17 02:35:27 -04:00
matches =
Regex.scan(pattern, content)
|> Enum.uniq()
2022-11-07 10:43:39 -05:00
|> Enum.map(&get_id_info(&1, guild))
2022-11-07 10:42:08 -05:00
content
|> do_replace(matches)
2020-07-17 02:35:27 -04:00
end
def from_discord(
%{attachments: attachments, author: user, guild_id: guild, sticker_items: nil} = msg
) do
usr =
case DiscordAPI.get_guild_member(guild, user.id) do
{:ok, %{nick: nick}} when is_binary(nick) -> nick
_ -> "#{user.username}\##{user.discriminator}"
end
cpart =
msg
|> fixdiscordidstrings
|> String.split("\n")
apart =
attachments
|> Enum.map(& &1.url)
messages =
(cpart ++ apart)
|> Enum.filter(&(&1 != ""))
# discord may give... many lines. split and format.
case Enum.count(messages) do
0 ->
{usr, "#{messages[0]}"}
_ ->
{usr,
messages
|> Enum.map(fn m -> "#{m}" end)}
end
end
def from_discord(
%{attachments: attachments, author: user, guild_id: guild, sticker_items: stickers} = msg
) do
2020-07-18 04:22:57 -04:00
usr =
case DiscordAPI.get_guild_member(guild, user.id) do
{:ok, %{nick: nick}} when is_binary(nick) -> nick
_ -> "#{user.username}\##{user.discriminator}"
end
2020-07-17 03:24:44 -04:00
cpart =
2020-07-18 04:22:57 -04:00
msg
|> fixdiscordidstrings
2020-07-17 02:35:27 -04:00
|> String.split("\n")
2020-07-16 23:11:18 -04:00
2020-07-17 03:24:44 -04:00
apart =
attachments
|> Enum.map(& &1.url)
spart =
stickers
|> Enum.map(fn s -> "#{@discordcdn}stickers/#{s.id}.png" end)
2020-07-17 03:24:44 -04:00
messages =
(cpart ++ apart ++ spart)
2020-07-17 03:24:44 -04:00
|> Enum.filter(&(&1 != ""))
2020-07-16 23:11:18 -04:00
# discord may give... many lines. split and format.
case Enum.count(messages) do
0 ->
{usr, "#{messages[0]}"}
2020-07-16 23:11:18 -04:00
2020-07-17 02:35:27 -04:00
_ ->
{usr,
messages
|> Enum.map(fn m -> "#{m}" end)}
2020-07-16 23:11:18 -04:00
end
end
end