Compare commits

...

5 Commits

Author SHA1 Message Date
Rachel Fae Fox 0421ec9264 forgot to mix format 2022-11-07 10:43:39 -05:00
Rachel Fae Fox 4f6cc19c40 Fix mentions. 2022-11-07 10:42:08 -05:00
Rachel Fae Fox f0929ea281 begin refactor of "Info" modules into a single discordinfo module. 2022-11-07 09:25:00 -05:00
Rachel Fae Fox d34a78cb99 modifications to make credo happy mostly 2022-11-07 09:24:39 -05:00
Rachel Fae Fox 8b1e19b56d add credo and other tools for later. 2022-11-07 09:22:44 -05:00
9 changed files with 114 additions and 127 deletions

View File

@ -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

View File

@ -1,4 +1,7 @@
defmodule Discordirc.DiscordHandler do
@moduledoc """
discord bot
"""
use Nostrum.Consumer
alias Nostrum.Api
alias Discordirc.ChannelMap

35
lib/discordinfo.ex Executable file
View File

@ -0,0 +1,35 @@
defmodule Discordirc.DiscordInfo do
@moduledoc """
helper functions for discord text things
"""
alias Nostrum.Api
def get_nick_by_id(guild_id, id) do
case Api.get_guild_member(guild_id, id) do
{:ok, x = %{nick: nil}} ->
"#{x.user.username}##{x.user.discriminator}"
{:ok, %{nick: n}} ->
n
end
end
def get_username_by_id(id) do
{:ok, %{username: u, discriminator: d}} = Api.get_user(id)
"#{u}##{d}"
end
def get_channel_name_by_id(id) do
{:ok, %{name: c}} = Api.get_channel(id)
c
end
def get_role_name_by_id(guild_id, id) do
{:ok, roles} = Api.get_guild_roles(guild_id)
roles
|> Enum.filter(fn %{id: i} -> i == id end)
|> List.first()
|> Map.get(:name)
end
end

View File

@ -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 ++

View File

@ -1,5 +1,9 @@
defmodule Discordirc.Formatter do
@moduledoc """
Transforms messages to/from discord from/to irc
"""
alias Nostrum.Api, as: DiscordAPI
alias Discordirc.DiscordInfo
def from_irc(nick, msg, ctcp \\ false) do
# strip or replace IRC formatting.
@ -24,127 +28,43 @@ defmodule Discordirc.Formatter do
end
end
defmodule DiscordUserInfo do
defstruct id: nil, username: nil, discriminator: nil, nickname: nil
def get_id_info([match, type, id], guild) do
i = String.to_integer(id)
def from_id(id, guild) do
case DiscordAPI.get_user(id) do
{:ok, user} ->
case DiscordAPI.get_guild_member(guild, user.id()) do
{:error, _} ->
nil
case type do
"#" ->
{match, "#" <> DiscordInfo.get_channel_name_by_id(i)}
{:ok, member} ->
%DiscordUserInfo{
id: id,
username: user.username,
discriminator: user.discriminator,
nickname: member.nick
}
end
"@" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
{:error, _} ->
nil
end
"@!" ->
{match, "@" <> DiscordInfo.get_nick_by_id(guild, i)}
"@&" ->
{match, "@" <> DiscordInfo.get_role_name_by_id(guild, i)}
end
end
defmodule DiscordChannelInfo do
defstruct id: nil, channel: nil
def from_id(id) do
case DiscordAPI.get_channel(id) do
{:ok, channel} ->
%DiscordChannelInfo{
id: id,
channel: channel.name
}
{:error, _} ->
nil
end
end
def do_replace(str, [head | tail]) do
{fst, snd} = head
do_replace(String.replace(str, fst, snd, global: true), tail)
end
def tryreplace(s, m) do
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
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
def do_replace(str, []) do
str
end
def fixdiscordidstrings(%{:content => content, :guild_id => guild}) do
pattern = ~r/\<(\@!?|#)(\d+)\>/um
pattern = ~r/\<(\@[!&]?|#)(\d+)\>/um
matches =
Regex.scan(pattern, content)
|> Enum.uniq()
|> Enum.map(fn
[fst, "@", lst] ->
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
|> Enum.map(&get_id_info(&1, guild))
[fst, "@!", lst] ->
%{str: fst, id: lst, dui: DiscordUserInfo.from_id(String.to_integer(lst), guild)}
[fst, "#", lst] ->
%{str: fst, id: lst, cui: DiscordChannelInfo.from_id(String.to_integer(lst))}
end)
unless matches == [] do
doallreplacements(
Regex.split(pattern, content, include_captures: true),
matches,
{"", matches}
)
else
content
end
content
|> do_replace(matches)
end
def from_discord(msg) do

View File

@ -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

View File

@ -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,21 @@ 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

View File

@ -23,7 +23,11 @@ defmodule Discordirc.MixProject do
defp deps do
[
{:nostrum, "~> 0.4"},
{:exirc, "~> 1.1.0"}
{:exirc, "~> 1.1.0"},
{:ex_machina, "~> 2.4", only: :test},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:mock, "~> 0.3.5", only: :test},
{:mox, "~> 1.0", only: :test}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]

View File

@ -1,13 +1,17 @@
%{
"artificery": {:hex, :artificery, "0.4.3", "0bc4260f988dcb9dda4b23f9fc3c6c8b99a6220a331534fdf5bf2fd0d4333b02", [:mix], [], "hexpm", "12e95333a30e20884e937abdbefa3e7f5e05609c2ba8cf37b33f000b9ffc0504"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"chacha20": {:hex, :chacha20, "1.0.3", "26372176993172260968b36b4e7bc2e007e6b2b397ae08083e4836df67cdf03c", [:mix], [], "hexpm", "27f23b680e63f04b5bced77a9d8867033b381c091f92d544de0ad93accfb7cec"},
"cowlib": {:hex, :remedy_cowlib, "2.11.1", "7abb4d0779a7d1c655f7642dc0bd0af754951e95005dfa01b500c68fe35a5961", [:rebar3], [], "hexpm", "0b613dc308e080cb6134285f1b1b55c3873e101652e70c70010fc6651c91b130"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"curve25519": {:hex, :curve25519, "1.0.4", "e570561b832c29b3ce4fd8b9fcd9c9546916188860568f1c1fc9428d7cb00894", [:mix], [], "hexpm", "1a068bf9646e7067bf6aa5bf802b404002734e09bb5300f098109df03e31f9f5"},
"distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm", "bbc7008b0161a6f130d8d903b5b3232351fccc9c31a991f8fcbf2a12ace22995"},
"ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"},
"equivalex": {:hex, :equivalex, "1.0.3", "170d9a82ae066e0020dfe1cf7811381669565922eb3359f6c91d7e9a1124ff74", [:mix], [], "hexpm", "46fa311adb855117d36e461b9c0ad2598f72110ad17ad73d7533c78020e045fc"},
"ex_machina": {:hex, :ex_machina, "2.7.0", "b792cc3127fd0680fecdb6299235b4727a4944a09ff0fa904cc639272cd92dc7", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "419aa7a39bde11894c87a615c4ecaa52d8f107bbdd81d810465186f783245bf8"},
"exirc": {:hex, :exirc, "1.1.0", "212a86124a113d87752ce528d52f8e2e2dab3accada66e5330591b9de2c628f9", [:mix], [], "hexpm", "71d01fd9c3378e3f13acd855188ec2b4f225884d2d33fabdb77b77e34b7f5dc7"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"},
"gun": {:hex, :remedy_gun, "2.0.1", "0f0caed812ed9e4da4f144df2d5bf73b0a99481d395ecde990a3791decf321c6", [:rebar3], [{:cowlib, "~> 2.11.1", [hex: :remedy_cowlib, repo: "hexpm", optional: false]}], "hexpm", "b6685a85fbd12b757f86809be1b3d88fcef365b77605cd5aa34db003294c446e"},
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
@ -15,9 +19,12 @@
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"kcl": {:hex, :kcl, "1.4.2", "8b73a55a14899dc172fcb05a13a754ac171c8165c14f65043382d567922f44ab", [:mix], [{:curve25519, ">= 1.0.4", [hex: :curve25519, repo: "hexpm", optional: false]}, {:ed25519, "~> 1.3", [hex: :ed25519, repo: "hexpm", optional: false]}, {:poly1305, "~> 1.0", [hex: :poly1305, repo: "hexpm", optional: false]}, {:salsa20, "~> 1.0", [hex: :salsa20, repo: "hexpm", optional: false]}], "hexpm", "9f083dd3844d902df6834b258564a82b21a15eb9f6acdc98e8df0c10feeabf05"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mock": {:hex, :mock, "0.3.7", "75b3bbf1466d7e486ea2052a73c6e062c6256fb429d6797999ab02fa32f29e03", [:mix], [{:meck, "~> 0.9.2", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "4da49a4609e41fd99b7836945c26f373623ea968cfb6282742bcb94440cf7e5c"},
"mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"},
"nostrum": {:hex, :nostrum, "0.6.0", "773b0a8953f74abb3d7633be94d5dc2d780f8ecaabba161e57f656afd6620c92", [:mix], [{:certifi, "~> 2.8", [hex: :certifi, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.11 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "== 2.0.1", [hex: :remedy_gun, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:kcl, "~> 1.4", [hex: :kcl, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm", "f44846457e5e40137948164633ed2ea4cc21c434c224c69073f4df1cc90a3f90"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},