Fix mentions.
parent
f0929ea281
commit
4f6cc19c40
|
@ -5,15 +5,19 @@ defmodule Discordirc.DiscordInfo do
|
||||||
alias Nostrum.Api
|
alias Nostrum.Api
|
||||||
|
|
||||||
def get_nick_by_id(guild_id, id) do
|
def get_nick_by_id(guild_id, id) do
|
||||||
{:ok, user} = Api.get_user(id)
|
case Api.get_guild_member(guild_id, id) do
|
||||||
{:ok, member} = Api.get_guild_member(guild_id, id)
|
{:ok, x = %{nick: nil}} ->
|
||||||
member.nick
|
"#{x.user.username}##{x.user.discriminator}"
|
||||||
|
{:ok, %{nick: n}} ->
|
||||||
|
n
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_username_by_id(id) do
|
def get_username_by_id(id) do
|
||||||
{:ok, %{username: u, discriminator: d}} = Api.get_user(id)
|
{:ok, %{username: u, discriminator: d}} = Api.get_user(id)
|
||||||
"#{u}##{d}"
|
"#{u}##{d}"
|
||||||
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
|
||||||
|
|
135
lib/formatter.ex
135
lib/formatter.ex
|
@ -3,7 +3,8 @@ defmodule Discordirc.Formatter do
|
||||||
Transforms messages to/from discord from/to irc
|
Transforms messages to/from discord from/to irc
|
||||||
"""
|
"""
|
||||||
alias Nostrum.Api, as: DiscordAPI
|
alias Nostrum.Api, as: DiscordAPI
|
||||||
|
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 =
|
||||||
|
@ -27,127 +28,39 @@ defmodule Discordirc.Formatter do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule DiscordUserInfo do
|
def get_id_info([match, type, id], guild) do
|
||||||
defstruct id: nil, username: nil, discriminator: nil, nickname: nil
|
i = String.to_integer(id)
|
||||||
|
case type do
|
||||||
def from_id(id, guild) do
|
"#" ->
|
||||||
case DiscordAPI.get_user(id) do
|
{match, "#" <> DiscordInfo.get_channel_name_by_id(i)}
|
||||||
{:ok, user} ->
|
"@" ->
|
||||||
case DiscordAPI.get_guild_member(guild, user.id()) do
|
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
|
||||||
{:error, _} ->
|
"@!" ->
|
||||||
nil
|
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
|
||||||
|
"@&" ->
|
||||||
{:ok, member} ->
|
{match, "@" <> DiscordInfo.get_role_name_by_id(guild, i)}
|
||||||
%DiscordUserInfo{
|
|
||||||
id: id,
|
|
||||||
username: user.username,
|
|
||||||
discriminator: user.discriminator,
|
|
||||||
nickname: member.nick
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
{:error, _} ->
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule DiscordChannelInfo do
|
def do_replace(str, [head | tail]) do
|
||||||
defstruct id: nil, channel: nil
|
{fst,snd} = head
|
||||||
|
do_replace(String.replace(str, fst, snd, global: true), tail)
|
||||||
def from_id(id) do
|
|
||||||
case DiscordAPI.get_channel(id) do
|
|
||||||
{:ok, channel} ->
|
|
||||||
%DiscordChannelInfo{
|
|
||||||
id: id,
|
|
||||||
channel: channel.name
|
|
||||||
}
|
|
||||||
|
|
||||||
{:error, _} ->
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
def do_replace(str, []) do
|
||||||
def tryreplace(s, m) do
|
str
|
||||||
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
|
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
|
|
||||||
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(fn
|
|> Enum.map(&(get_id_info(&1,guild)))
|
||||||
[fst, "@", lst] ->
|
|
||||||
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
|
|
||||||
|
|
||||||
[fst, "@!", lst] ->
|
content
|
||||||
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
|
|> do_replace(matches)
|
||||||
|
|
||||||
[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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def from_discord(msg) do
|
def from_discord(msg) do
|
||||||
|
|
Loading…
Reference in New Issue