From 4fac5895dbf60f337c1a7183016d4e252bf3e6c0 Mon Sep 17 00:00:00 2001 From: Rachel Fae Fox Date: Thu, 10 Nov 2022 11:55:08 -0500 Subject: [PATCH] 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 handler --- lib/discordhandler.ex | 25 +++++++++++-------- lib/formatter.ex | 20 +++++---------- lib/irc_bot.ex | 58 ++++++++++++++++++++++++------------------- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/lib/discordhandler.ex b/lib/discordhandler.ex index 5645d7c..7f59fa3 100755 --- a/lib/discordhandler.ex +++ b/lib/discordhandler.ex @@ -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 diff --git a/lib/formatter.ex b/lib/formatter.ex index 3f751ca..523bc69 100755 --- a/lib/formatter.ex +++ b/lib/formatter.ex @@ -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 diff --git a/lib/irc_bot.ex b/lib/irc_bot.ex index a0464d6..a9946cd 100755 --- a/lib/irc_bot.ex +++ b/lib/irc_bot.ex @@ -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}