Fix mentions.
parent
f0929ea281
commit
4f6cc19c40
|
@ -5,15 +5,19 @@ defmodule Discordirc.DiscordInfo do
|
|||
alias Nostrum.Api
|
||||
|
||||
def get_nick_by_id(guild_id, id) do
|
||||
{:ok, user} = Api.get_user(id)
|
||||
{:ok, member} = Api.get_guild_member(guild_id, id)
|
||||
member.nick
|
||||
case Api.get_guild_member(guild_id, id) do
|
||||
{:ok, x = %{nick: nil}} ->
|
||||
"#{x.user.username}##{x.user.discriminator}"
|
||||
{:ok, %{nick: n}} ->
|
||||
n
|
||||
end
|
||||
end
|
||||
|
||||
def get_username_by_id(id) do
|
||||
{:ok, %{username: u, discriminator: d}} = Api.get_user(id)
|
||||
"#{u}##{d}"
|
||||
end
|
||||
|
||||
def get_channel_name_by_id(id) do
|
||||
{:ok, %{name: c}} = Api.get_channel(id)
|
||||
c
|
||||
|
|
129
lib/formatter.ex
129
lib/formatter.ex
|
@ -3,6 +3,7 @@ defmodule Discordirc.Formatter do
|
|||
Transforms messages to/from discord from/to irc
|
||||
"""
|
||||
alias Nostrum.Api, as: DiscordAPI
|
||||
alias Discordirc.DiscordInfo
|
||||
|
||||
def from_irc(nick, msg, ctcp \\ false) do
|
||||
# strip or replace IRC formatting.
|
||||
|
@ -27,127 +28,39 @@ defmodule Discordirc.Formatter do
|
|||
end
|
||||
end
|
||||
|
||||
defmodule DiscordUserInfo do
|
||||
defstruct id: nil, username: nil, discriminator: nil, nickname: nil
|
||||
|
||||
def from_id(id, guild) do
|
||||
case DiscordAPI.get_user(id) do
|
||||
{:ok, user} ->
|
||||
case DiscordAPI.get_guild_member(guild, user.id()) do
|
||||
{:error, _} ->
|
||||
nil
|
||||
|
||||
{:ok, member} ->
|
||||
%DiscordUserInfo{
|
||||
id: id,
|
||||
username: user.username,
|
||||
discriminator: user.discriminator,
|
||||
nickname: member.nick
|
||||
}
|
||||
end
|
||||
|
||||
{:error, _} ->
|
||||
nil
|
||||
end
|
||||
def get_id_info([match, type, id], guild) do
|
||||
i = String.to_integer(id)
|
||||
case type do
|
||||
"#" ->
|
||||
{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_role_name_by_id(guild, i)}
|
||||
end
|
||||
end
|
||||
|
||||
defmodule DiscordChannelInfo do
|
||||
defstruct id: nil, channel: nil
|
||||
|
||||
def from_id(id) do
|
||||
case DiscordAPI.get_channel(id) do
|
||||
{:ok, channel} ->
|
||||
%DiscordChannelInfo{
|
||||
id: id,
|
||||
channel: channel.name
|
||||
}
|
||||
|
||||
{:error, _} ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def tryreplace(s, m) do
|
||||
case m do
|
||||
%{dui: nil} ->
|
||||
s
|
||||
|
||||
%{cui: nil} ->
|
||||
s
|
||||
|
||||
%{str: r, dui: dui} ->
|
||||
if String.match?(s, ~r/\<\@(\d+)\>/) do
|
||||
if s == r do
|
||||
if is_binary(dui.nickname) do
|
||||
String.replace(s, r, "@" <> dui.nickname)
|
||||
else
|
||||
String.replace(s, r, "@" <> dui.username <> "#" <> dui.discriminator)
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
s
|
||||
end
|
||||
|
||||
%{str: r, cui: cui} ->
|
||||
if String.match?(s, ~r/\<#(\d+)\>/) do
|
||||
if s == r do
|
||||
String.replace(s, r, "#" <> cui.channel)
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def doallreplacements(split, matches, acc) do
|
||||
[shead | stail] = split
|
||||
{str, [m | mtail]} = acc
|
||||
|
||||
s = tryreplace(shead, m)
|
||||
|
||||
if is_nil(s) do
|
||||
doallreplacements(split, matches, {str, mtail})
|
||||
else
|
||||
if stail == [] do
|
||||
str <> s
|
||||
else
|
||||
doallreplacements(stail, matches, {str <> s, matches})
|
||||
end
|
||||
def do_replace(str, [head | tail]) do
|
||||
{fst,snd} = head
|
||||
do_replace(String.replace(str, fst, snd, global: true), tail)
|
||||
end
|
||||
def do_replace(str, []) do
|
||||
str
|
||||
end
|
||||
|
||||
def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do
|
||||
pattern = ~r/\<(\@!?|#)(\d+)\>/um
|
||||
pattern = ~r/\<(\@[!&]?|#)(\d+)\>/um
|
||||
|
||||
matches =
|
||||
Regex.scan(pattern, content)
|
||||
|> Enum.uniq()
|
||||
|> Enum.map(fn
|
||||
[fst, "@", lst] ->
|
||||
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
|
||||
|> Enum.map(&(get_id_info(&1,guild)))
|
||||
|
||||
[fst, "@!", lst] ->
|
||||
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
|
||||
|
||||
[fst, "#", lst] ->
|
||||
%{str: fst, id: lst, cui: DiscordChannelInfo.from_id(String.to_integer(lst))}
|
||||
end)
|
||||
|
||||
if matches == [] do
|
||||
content
|
||||
else
|
||||
doallreplacements(
|
||||
Regex.split(pattern, content, include_captures: true),
|
||||
matches,
|
||||
{"", matches}
|
||||
)
|
||||
end
|
||||
|> do_replace(matches)
|
||||
|
||||
end
|
||||
|
||||
def from_discord(msg) do
|
||||
|
|
Loading…
Reference in New Issue