diff --git a/lib/channel_map.ex b/lib/channel_map.ex index 5eafabe..10ca0e7 100755 --- a/lib/channel_map.ex +++ b/lib/channel_map.ex @@ -1,8 +1,10 @@ defmodule Discordirc.ChannelMap do - @cmap Application.fetch_env!(:discordirc, :channels) + @moduledoc """ + maps discord channels to irc channels + """ def discord(network, channel) do id = - @cmap + Application.fetch_env!(:discordirc, :channels) |> Enum.filter(&(&1.ircnetwork == network and &1.ircchannel == channel)) |> List.first() @@ -17,7 +19,7 @@ defmodule Discordirc.ChannelMap do def irc(id) do channel = - @cmap + Application.fetch_env!(:discordirc, :channels) |> Enum.filter(&(&1.discordid == id)) |> List.first() @@ -31,7 +33,7 @@ defmodule Discordirc.ChannelMap do end def getircchannels(network) do - @cmap + Application.fetch_env!(:discordirc, :channels) |> Enum.filter(&(&1.ircnetwork == network)) |> Enum.map(& &1.ircchannel) end diff --git a/lib/discordhandler.ex b/lib/discordhandler.ex index 4fd1757..5645d7c 100755 --- a/lib/discordhandler.ex +++ b/lib/discordhandler.ex @@ -1,4 +1,7 @@ defmodule Discordirc.DiscordHandler do + @moduledoc """ + discord bot + """ use Nostrum.Consumer alias Nostrum.Api alias Discordirc.ChannelMap diff --git a/lib/discordirc.ex b/lib/discordirc.ex index d0cfa5e..c2c640e 100755 --- a/lib/discordirc.ex +++ b/lib/discordirc.ex @@ -1,5 +1,7 @@ defmodule Discordirc do - @networks Application.get_env(:discordirc, :networks) + @moduledoc """ + Entrypoint + """ use Application alias Discordirc.IRC @@ -7,7 +9,9 @@ defmodule Discordirc do def start(_type, _args) do import Supervisor.Spec - ircnets = @networks |> Enum.map(fn net -> worker(IRC, [net], id: net.network) end) + ircnets = + Application.get_env(:discordirc, :networks) + |> Enum.map(fn net -> worker(IRC, [net], id: net.network) end) children = ircnets ++ diff --git a/lib/formatter.ex b/lib/formatter.ex index f48b7ba..a7fd562 100755 --- a/lib/formatter.ex +++ b/lib/formatter.ex @@ -1,4 +1,7 @@ defmodule Discordirc.Formatter do + @moduledoc """ + Transforms messages to/from discord from/to irc + """ alias Nostrum.Api, as: DiscordAPI def from_irc(nick, msg, ctcp \\ false) do @@ -136,14 +139,14 @@ defmodule Discordirc.Formatter do %{str: fst, id: lst, cui: DiscordChannelInfo.from_id(String.to_integer(lst))} end) - unless matches == [] do + if matches == [] do + content + else doallreplacements( Regex.split(pattern, content, include_captures: true), matches, {"", matches} ) - else - content end end diff --git a/lib/irc_bot.ex b/lib/irc_bot.ex index a0553a8..f8209a1 100755 --- a/lib/irc_bot.ex +++ b/lib/irc_bot.ex @@ -1,4 +1,7 @@ defmodule Discordirc.IRC do + @moduledoc """ + IRC bot portion + """ use GenServer require Logger @@ -98,9 +101,10 @@ defmodule Discordirc.IRC do def discord_ircsplit(msg, nick, target) do pfx = "PRIVMSG #{target} :" |> String.length() nkl = "<#{nick}> " |> String.length() + msg |> String.split("\n") - |> Enum.map(&ircsplit(&1, pfx+nkl)) + |> Enum.map(&ircsplit(&1, pfx + nkl)) |> List.flatten() end diff --git a/lib/webhookservice.ex b/lib/webhookservice.ex index 2bff605..b111e3f 100755 --- a/lib/webhookservice.ex +++ b/lib/webhookservice.ex @@ -1,4 +1,8 @@ defmodule Discordirc.WebhookService do + @moduledoc """ + This module manages the webhooks that we output + to discord channels with + """ use GenServer require Logger alias Nostrum.Api, as: DiscordAPI @@ -11,11 +15,13 @@ defmodule Discordirc.WebhookService do def clear_old_hooks(channel_id) do {:ok, webhooks} = DiscordAPI.get_channel_webhooks(channel_id) - webhooks - |> Enum.filter(fn wh -> - wh.user.id == Nostrum.Snowflake.dump(DiscordAPI.get_current_user!().id) - end) - |> Enum.map(&DiscordAPI.delete_webhook(&1.id, "clearing old hooks")) + deadhooks = + webhooks + |> Enum.filter(fn wh -> + wh.user.id == Nostrum.Snowflake.dump(DiscordAPI.get_current_user!().id) + end) + + for h <- deadhooks, do: DiscordAPI.delete_webhook(h.id, "clearing old hooks") :ok end @@ -40,19 +46,19 @@ defmodule Discordirc.WebhookService do {:error, e} -> case e.response.code do - 10003 -> + 10_003 -> raise "unknown channel" - 30007 -> - if retry < 1 do - clear_old_hooks(channel_id) - create_hook(state, channel_id, retry + 1) - else - raise "too many webhooks" - end + 30_007 when retry < 1 -> + clear_old_hooks(channel_id) + create_hook(state, channel_id, retry + 1) - 40001 -> + 30_007 when retry >= 1 -> + raise "too many webhooks" + 40_001 -> raise "no permissions" + 50_035 -> + raise "invalid form body" end end end