modifications to make credo happy mostly
parent
8b1e19b56d
commit
d34a78cb99
|
@ -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
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
defmodule Discordirc.DiscordHandler do
|
||||
@moduledoc """
|
||||
discord bot
|
||||
"""
|
||||
use Nostrum.Consumer
|
||||
alias Nostrum.Api
|
||||
alias Discordirc.ChannelMap
|
||||
|
|
|
@ -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 ++
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
deadhooks =
|
||||
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"))
|
||||
|
||||
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
|
||||
30_007 when retry < 1 ->
|
||||
clear_old_hooks(channel_id)
|
||||
create_hook(state, channel_id, retry + 1)
|
||||
else
|
||||
raise "too many webhooks"
|
||||
end
|
||||
|
||||
40001 ->
|
||||
30_007 when retry >= 1 ->
|
||||
raise "too many webhooks"
|
||||
40_001 ->
|
||||
raise "no permissions"
|
||||
50_035 ->
|
||||
raise "invalid form body"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue