summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/last_fm_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2020-07-07 21:39:10 +0200
committerhref <href@random.sh>2020-07-07 21:39:51 +0200
commitd6ee134a5957e299c3ad59011df320b3c41e6e61 (patch)
tree29567e6635466f8a3415a935b3cc8a777019f5bc /lib/lsg_irc/last_fm_plugin.ex
parentbleh (diff)
pouet
Diffstat (limited to 'lib/lsg_irc/last_fm_plugin.ex')
-rw-r--r--lib/lsg_irc/last_fm_plugin.ex20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/lsg_irc/last_fm_plugin.ex b/lib/lsg_irc/last_fm_plugin.ex
index 2446473..067a44e 100644
--- a/lib/lsg_irc/last_fm_plugin.ex
+++ b/lib/lsg_irc/last_fm_plugin.ex
@@ -18,6 +18,7 @@ defmodule LSG.IRC.LastFmPlugin do
end
def init([]) do
+ {:ok, _} = Registry.register(IRC.PubSub, "account", [])
{:ok, _} = Registry.register(IRC.PubSub, "trigger:lastfm", [])
{:ok, _} = Registry.register(IRC.PubSub, "trigger:lastfmall", [])
dets_filename = (LSG.data_path() <> "/" <> "lastfm.dets") |> String.to_charlist
@@ -27,13 +28,13 @@ defmodule LSG.IRC.LastFmPlugin do
def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :plus, args: [username]}}}, state) do
username = String.strip(username)
- :ok = :dets.insert(state.dets, {String.downcase(message.sender.nick), username})
+ :ok = :dets.insert(state.dets, {message.account.id, username})
message.replyfun.("#{message.sender.nick}: nom d'utilisateur last.fm configuré: \"#{username}\".")
{:noreply, state}
end
def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :minus, args: []}}}, state) do
- text = case :dets.lookup(state.dets, message.sender.nick) do
+ text = case :dets.lookup(state.dets, message.account.id) do
[{_nick, username}] ->
:dets.delete(state.dets, String.downcase(message.sender.nick))
message.replyfun.("#{message.sender.nick}: nom d'utilisateur last.fm enlevé.")
@@ -43,7 +44,7 @@ defmodule LSG.IRC.LastFmPlugin do
end
def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :bang, args: []}}}, state) do
- irc_now_playing(message.sender.nick, message, state)
+ irc_now_playing(message.account.id, message, state)
{:noreply, state}
end
@@ -53,9 +54,12 @@ defmodule LSG.IRC.LastFmPlugin do
end
def handle_info({:irc, :trigger, "lastfmall", message = %{trigger: %{type: :bang}}}, state) do
- foldfun = fn({_nick, user}, acc) -> [user|acc] end
+ members = IRC.Membership.members(message.network, message.channel)
+ foldfun = fn({nick, user}, acc) -> [{nick,user}|acc] end
usernames = :dets.foldl(foldfun, [], state.dets)
- |> Enum.uniq
+ |> Enum.uniq()
+ |> Enum.filter(fn({acct,_}) -> Enum.member?(members, acct) end)
+ |> Enum.map(fn({_, u}) -> u end)
for u <- usernames, do: irc_now_playing(u, message, state)
{:noreply, state}
end
@@ -74,6 +78,12 @@ defmodule LSG.IRC.LastFmPlugin do
defp irc_now_playing(nick_or_user, message, state) do
nick_or_user = String.strip(nick_or_user)
+ if account = IRC.Account.find_always_by_nick(message.network, message.channel, nick_or_user) do
+ account.id
+ else
+ nick_or_user
+ end
+
username = case :dets.lookup(state.dets, String.downcase(nick_or_user)) do
[{^nick_or_user, username}] -> username
_ -> nick_or_user