diff options
-rw-r--r-- | lib/lsg_irc/last_fm_plugin.ex | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/lib/lsg_irc/last_fm_plugin.ex b/lib/lsg_irc/last_fm_plugin.ex index 8497f2d..5bf582d 100644 --- a/lib/lsg_irc/last_fm_plugin.ex +++ b/lib/lsg_irc/last_fm_plugin.ex @@ -4,11 +4,14 @@ defmodule LSG.IRC.LastFmPlugin do @moduledoc """ # last.fm - * **!lastfm `[nick|username]`** - * **!lastfmall** - * **+lastfm `<username last.fm>`, -lastfm** + * **!lastfm|np `[nick|username]`** + * **.lastfm|np** + * **+lastfm, -lastfm `<username last.fm>; ?lastfm`** Configurer un nom d'utilisateur last.fm """ + @single_trigger ~w(lastfm np) + @pubsub_topics ~w(trigger:lastfm trigger:np) + defstruct dets: nil def irc_doc, do: @moduledoc @@ -19,9 +22,7 @@ defmodule LSG.IRC.LastFmPlugin do def init([]) do regopts = [type: __MODULE__] - {:ok, _} = Registry.register(IRC.PubSub, "account", regopts) - {:ok, _} = Registry.register(IRC.PubSub, "trigger:lastfm", regopts) - {:ok, _} = Registry.register(IRC.PubSub, "trigger:lastfmall", regopts) + for t <- @pubsub_topics, do: {:ok, _} = Registry.register(IRC.PubSub, t, type: __MODULE__) dets_filename = (LSG.data_path() <> "/" <> "lastfm.dets") |> String.to_charlist {:ok, dets} = :dets.open_file(dets_filename, []) {:ok, %__MODULE__{dets: dets}} @@ -36,25 +37,34 @@ defmodule LSG.IRC.LastFmPlugin do def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :minus, args: []}}}, state) do text = case :dets.lookup(state.dets, message.account.id) do + [{_nick, _username}] -> + :dets.delete(state.dets, message.account.id) + message.replyfun.("#{message.sender.nick}: nom d'utilisateur last.fm enlevé.") + _ -> nil + end + {:noreply, state} + end + + def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :query, args: []}}}, state) 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é.") + message.replyfun.("#{message.sender.nick}: #{username}.") _ -> nil end {:noreply, state} end - def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :bang, args: []}}}, state) do + def handle_info({:irc, :trigger, _, message = %{trigger: %{type: :bang, args: []}}}, state) do irc_now_playing(message.account.id, message, state) {:noreply, state} end - def handle_info({:irc, :trigger, "lastfm", message = %{trigger: %{type: :bang, args: [nick_or_user]}}}, state) do + def handle_info({:irc, :trigger, _, message = %{trigger: %{type: :bang, args: [nick_or_user]}}}, state) do irc_now_playing(nick_or_user, message, state) {:noreply, state} end - def handle_info({:irc, :trigger, "lastfmall", message = %{trigger: %{type: :bang}}}, state) do + def handle_info({:irc, :trigger, _, message = %{trigger: %{type: :dot}}}, state) do members = IRC.Membership.members(message.network, message.channel) foldfun = fn({nick, user}, acc) -> [{nick,user}|acc] end usernames = :dets.foldl(foldfun, [], state.dets) @@ -79,15 +89,15 @@ 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 + id_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 + username = case :dets.lookup(state.dets, id_or_user) do + [{_, username}] -> username + _ -> id_or_user end case now_playing(username) do @@ -102,7 +112,7 @@ defmodule LSG.IRC.LastFmPlugin do message.replyfun.("#{username}: pas de résultat") end other -> - message.replyfun.("erreur http") + message.replyfun.("erreur :(") end end |