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__, [], name: __MODULE__)
end
def init(_) do
{:ok, _} = Registry.register(IRC.PubSub, "trigger:calc", [plugin: __MODULE__])
{: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 -> if(error[:message], do: "#{error.message}", else: "erreur")
end
message.replyfun.("#{message.sender.nick}: #{expr} = #{result}")
{:noreply, state}
end
def handle_info(msg, state) do
{:noreply, state}
end
end