diff options
author | Jordan Bracco <href@random.sh> | 2022-12-20 00:21:54 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-20 19:29:41 +0100 |
commit | 2d83df8b32bff7f0028923bb5b64dc0b55f20d03 (patch) | |
tree | 1207e67b5b15f540963db05e7be89f3ca950e724 /lib/nola_plugins/seen_plugin.ex | |
parent | Nola rename, the end. pt 6. Refs T77. (diff) |
Nola rename: The Big Move, Refs T77
Diffstat (limited to 'lib/nola_plugins/seen_plugin.ex')
-rw-r--r-- | lib/nola_plugins/seen_plugin.ex | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/nola_plugins/seen_plugin.ex b/lib/nola_plugins/seen_plugin.ex new file mode 100644 index 0000000..2a4d0dd --- /dev/null +++ b/lib/nola_plugins/seen_plugin.ex @@ -0,0 +1,59 @@ +defmodule Nola.IRC.SeenPlugin do + @moduledoc """ + # seen + + * **!seen `<nick>`** + """ + + def irc_doc, do: @moduledoc + def start_link() do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init([]) do + regopts = [plugin: __MODULE__] + {:ok, _} = Registry.register(IRC.PubSub, "triggers", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "messages", regopts) + dets_filename = (Nola.data_path() <> "/seen.dets") |> String.to_charlist() + {:ok, dets} = :dets.open_file(dets_filename, []) + {:ok, %{dets: dets}} + end + + def handle_info({:irc, :trigger, "seen", m = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: [nick]}}}, state) do + witness(m, state) + m.replyfun.(last_seen(m.channel, nick, state)) + {:noreply, state} + end + + def handle_info({:irc, :trigger, _, m}, state) do + witness(m, state) + {:noreply, state} + end + + def handle_info({:irc, :text, m}, state) do + witness(m, state) + {:noreply, state} + end + + defp witness(%IRC.Message{channel: channel, text: text, sender: %{nick: nick}}, %{dets: dets}) do + :dets.insert(dets, {{channel, nick}, DateTime.utc_now(), text}) + :ok + end + + defp last_seen(channel, nick, %{dets: dets}) do + case :dets.lookup(dets, {channel, nick}) do + [{_, date, text}] -> + diff = round(DateTime.diff(DateTime.utc_now(), date)/60) + cond do + diff >= 30 -> + duration = Timex.Duration.from_minutes(diff) + format = Timex.Format.Duration.Formatter.lformat(duration, "fr", :humanized) + "#{nick} a parlé pour la dernière fois il y a #{format}: “#{text}”" + true -> "#{nick} est là..." + end + [] -> + "je ne connais pas de #{nick}" + end + end + +end |