slash commands and other new features should no longer crash the bot
try rescue block used. This should be replaced later with matches after a rewrite of the discordmsg handlermaster
parent
7438712201
commit
4fac5895db
|
@ -13,18 +13,23 @@ defmodule Discordirc.DiscordHandler do
|
|||
def is_me_or_my_webhook(msg) do
|
||||
{:ok, me} = Api.get_current_user()
|
||||
|
||||
is_me = msg.author.username == me.username and msg.author.discriminator == me.discriminator
|
||||
is_webhook = msg.webhook_id != nil
|
||||
case msg do
|
||||
%{author: %{username: u, discriminator: d}}
|
||||
when u == me.username and d == me.discriminator ->
|
||||
true
|
||||
|
||||
is_my_webhook =
|
||||
if is_webhook do
|
||||
{:ok, wh} = Api.get_webhook(msg.webhook_id)
|
||||
wh.user.id == Nostrum.Snowflake.dump(me.id)
|
||||
else
|
||||
%{webhook_id: wh} ->
|
||||
case Api.get_webhook(wh) do
|
||||
{:ok, webhook} ->
|
||||
webhook.user.id == Nostrum.Snowflake.dump(me.id)
|
||||
|
||||
{:error, e} ->
|
||||
false
|
||||
end
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
|
||||
is_me or is_my_webhook
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) do
|
||||
|
|
|
@ -72,11 +72,7 @@ defmodule Discordirc.Formatter do
|
|||
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
|
||||
usr = DiscordInfo.get_nick_by_id(guild, user.id)
|
||||
|
||||
cpart =
|
||||
msg
|
||||
|
@ -94,10 +90,10 @@ defmodule Discordirc.Formatter do
|
|||
# discord may give... many lines. split and format.
|
||||
case Enum.count(messages) do
|
||||
0 ->
|
||||
{usr, "#{messages[0]}"}
|
||||
{:error, "empty set"}
|
||||
|
||||
_ ->
|
||||
{usr,
|
||||
{:ok, usr,
|
||||
messages
|
||||
|> Enum.map(fn m -> "#{m}" end)}
|
||||
end
|
||||
|
@ -106,11 +102,7 @@ defmodule Discordirc.Formatter do
|
|||
def from_discord(
|
||||
%{attachments: attachments, author: user, guild_id: guild, sticker_items: stickers} = 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
|
||||
usr = DiscordInfo.get_nick_by_id(guild, user.id)
|
||||
|
||||
cpart =
|
||||
msg
|
||||
|
@ -132,10 +124,10 @@ defmodule Discordirc.Formatter do
|
|||
# discord may give... many lines. split and format.
|
||||
case Enum.count(messages) do
|
||||
0 ->
|
||||
{usr, "#{messages[0]}"}
|
||||
{:error, "empty set"}
|
||||
|
||||
_ ->
|
||||
{usr,
|
||||
{:ok, usr,
|
||||
messages
|
||||
|> Enum.map(fn m -> "#{m}" end)}
|
||||
end
|
||||
|
|
|
@ -70,37 +70,43 @@ defmodule Discordirc.IRC do
|
|||
|
||||
def handle_info({:discordmsg, msg}, state) do
|
||||
channel = ChannelMap.irc(msg.channel_id)
|
||||
{usr, response} = Formatter.from_discord(msg)
|
||||
|
||||
case channel do
|
||||
{:ok, _, chan} ->
|
||||
pfx = ":#{state.me} PRIVMSG #{chan} :" |> byte_size()
|
||||
nkl = "<#{usr}> " |> byte_size()
|
||||
prefixlen = pfx + nkl
|
||||
# irc messages can only be 512b in length
|
||||
split_response =
|
||||
case response do
|
||||
x when is_list(x) ->
|
||||
try do
|
||||
{:ok, usr, response} = Formatter.from_discord(msg)
|
||||
|
||||
case channel do
|
||||
{:ok, _, chan} ->
|
||||
pfx = ":#{state.me} PRIVMSG #{chan} :" |> byte_size()
|
||||
nkl = "<#{usr}> " |> byte_size()
|
||||
prefixlen = pfx + nkl
|
||||
# irc messages can only be 512b in length
|
||||
split_response =
|
||||
case response do
|
||||
x when is_list(x) ->
|
||||
x
|
||||
|
||||
x when is_binary(x) ->
|
||||
[x]
|
||||
end
|
||||
|> Enum.map(fn x ->
|
||||
x
|
||||
|
||||
x when is_binary(x) ->
|
||||
[x]
|
||||
end
|
||||
|> Enum.map(fn x ->
|
||||
x
|
||||
|> String.split("\n")
|
||||
|> Enum.map(&ircsplit(&1, prefixlen))
|
||||
|> String.split("\n")
|
||||
|> Enum.map(&ircsplit(&1, prefixlen))
|
||||
|> List.flatten()
|
||||
end)
|
||||
|> List.flatten()
|
||||
end)
|
||||
|> List.flatten()
|
||||
|
||||
case split_response do
|
||||
x when is_binary(x) ->
|
||||
ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{x}")
|
||||
case split_response do
|
||||
x when is_binary(x) ->
|
||||
ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{x}")
|
||||
|
||||
x when is_list(x) ->
|
||||
for m <- x, do: ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{m}")
|
||||
end
|
||||
x when is_list(x) ->
|
||||
for m <- x, do: ExIRC.Client.msg(state.client, :privmsg, chan, "<#{usr}> #{m}")
|
||||
end
|
||||
end
|
||||
rescue
|
||||
e ->
|
||||
Logger.error("TODO: handle errors instead of using exception\ne: #{inspect(e)}")
|
||||
end
|
||||
|
||||
{:noreply, state}
|
||||
|
|
Loading…
Reference in New Issue