forgot to mix format

master
Rachel Fae Fox 2022-11-07 10:43:39 -05:00
parent 4f6cc19c40
commit 0421ec9264
3 changed files with 28 additions and 20 deletions

View File

@ -7,9 +7,10 @@ defmodule Discordirc.DiscordInfo do
def get_nick_by_id(guild_id, id) do def get_nick_by_id(guild_id, id) do
case Api.get_guild_member(guild_id, id) do case Api.get_guild_member(guild_id, id) do
{:ok, x = %{nick: nil}} -> {:ok, x = %{nick: nil}} ->
"#{x.user.username}##{x.user.discriminator}" "#{x.user.username}##{x.user.discriminator}"
{:ok, %{nick: n}} -> {:ok, %{nick: n}} ->
n n
end end
end end
@ -19,15 +20,16 @@ defmodule Discordirc.DiscordInfo do
end end
def get_channel_name_by_id(id) do def get_channel_name_by_id(id) do
{:ok, %{name: c}} = Api.get_channel(id) {:ok, %{name: c}} = Api.get_channel(id)
c c
end end
def get_role_name_by_id(guild_id, id) do def get_role_name_by_id(guild_id, id) do
{:ok, roles} = Api.get_guild_roles(guild_id) {:ok, roles} = Api.get_guild_roles(guild_id)
roles roles
|> Enum.filter(fn (%{id: i}) -> i == id end) |> Enum.filter(fn %{id: i} -> i == id end)
|> List.first |> List.first()
|> Map.get(:name) |> Map.get(:name)
end end
end end

View File

@ -4,7 +4,7 @@ defmodule Discordirc.Formatter do
""" """
alias Nostrum.Api, as: DiscordAPI alias Nostrum.Api, as: DiscordAPI
alias Discordirc.DiscordInfo alias Discordirc.DiscordInfo
def from_irc(nick, msg, ctcp \\ false) do def from_irc(nick, msg, ctcp \\ false) do
# strip or replace IRC formatting. # strip or replace IRC formatting.
fmsg = fmsg =
@ -30,37 +30,41 @@ defmodule Discordirc.Formatter do
def get_id_info([match, type, id], guild) do def get_id_info([match, type, id], guild) do
i = String.to_integer(id) i = String.to_integer(id)
case type do case type do
"#" -> "#" ->
{match, "#" <> DiscordInfo.get_channel_name_by_id(i)} {match, "#" <> DiscordInfo.get_channel_name_by_id(i)}
"@" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)} "@" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
"@!" -> "@!" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)} {match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
"@&" -> "@&" ->
{match, "@" <> DiscordInfo.get_role_name_by_id(guild, i)} {match, "@" <> DiscordInfo.get_role_name_by_id(guild, i)}
end end
end end
def do_replace(str, [head | tail]) do def do_replace(str, [head | tail]) do
{fst,snd} = head {fst, snd} = head
do_replace(String.replace(str, fst, snd, global: true), tail) do_replace(String.replace(str, fst, snd, global: true), tail)
end end
def do_replace(str, []) do def do_replace(str, []) do
str str
end end
def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do
pattern = ~r/\<(\@[!&]?|#)(\d+)\>/um pattern = ~r/\<(\@[!&]?|#)(\d+)\>/um
matches = matches =
Regex.scan(pattern, content) Regex.scan(pattern, content)
|> Enum.uniq() |> Enum.uniq()
|> Enum.map(&(get_id_info(&1,guild))) |> Enum.map(&get_id_info(&1, guild))
content content
|> do_replace(matches) |> do_replace(matches)
end end
def from_discord(msg) do def from_discord(msg) do

View File

@ -55,10 +55,12 @@ defmodule Discordirc.WebhookService do
30_007 when retry >= 1 -> 30_007 when retry >= 1 ->
raise "too many webhooks" raise "too many webhooks"
40_001 -> 40_001 ->
raise "no permissions" raise "no permissions"
50_035 ->
raise "invalid form body" 50_035 ->
raise "invalid form body"
end end
end end
end end