summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/calc_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2018-05-02 19:03:35 +0200
committerhref <href@random.sh>2018-05-02 19:04:28 +0200
commita47dc245808921309f58e8b1c4b6fa028b2df073 (patch)
tree1842d413f2a43d3759b439417f053052b878a1f7 /lib/lsg_irc/calc_plugin.ex
parent (diff)
meh
Diffstat (limited to '')
-rw-r--r--lib/lsg_irc/calc_plugin.ex38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/lsg_irc/calc_plugin.ex b/lib/lsg_irc/calc_plugin.ex
new file mode 100644
index 0000000..6e4e30c
--- /dev/null
+++ b/lib/lsg_irc/calc_plugin.ex
@@ -0,0 +1,38 @@
+defmodule LSG.IRC.CalcPlugin do
+ @moduledoc """
+ # calc
+
+ * **!calc `<expression>`**: évalue l'expression mathématique `<expression>`.
+ """
+
+ def irc_doc, do: @moduledoc
+
+ def start_link() do
+ GenServer.start_link(__MODULE__, [])
+ end
+
+ def init(_) do
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:calc", [])
+ {:ok, nil}
+ end
+
+ def handle_info({:irc, :trigger, "calc", message = %IRC.Message{trigger: %IRC.Trigger{type: :bang, args: expr_list}}}, state) do
+ expr = Enum.join(expr_list, " ")
+ result = try do
+ case Abacus.eval(expr) do
+ {:ok, result} -> result
+ error -> inspect(error)
+ end
+ rescue
+ error -> "#{error.message}"
+ end
+ message.replyfun.("#{message.sender.nick}: #{expr} = #{result}")
+ {:noreply, state}
+ end
+
+ def handle_info(msg, state) do
+ {:noreply, state}
+ end
+
+end
+