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/untappd_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/untappd_plugin.ex')
-rw-r--r-- | lib/nola_plugins/untappd_plugin.ex | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/nola_plugins/untappd_plugin.ex b/lib/nola_plugins/untappd_plugin.ex new file mode 100644 index 0000000..50b0c4d --- /dev/null +++ b/lib/nola_plugins/untappd_plugin.ex @@ -0,0 +1,66 @@ +defmodule Nola.IRC.UntappdPlugin do + + def irc_doc() do + """ + # [Untappd](https://untappd.com) + + * `!beer <beer name>` Information about the first beer matching `<beer name>` + * `?beer <beer name>` List the 10 firsts beer matching `<beer name>` + + _Note_: The best way to search is always "Brewery Name + Beer Name", such as "Dogfish 60 Minute". + + Link your Untappd account to the bot (for automated checkins on [alcoolog](#alcoolog), ...) with the `enable-untappd` command, in private. + """ + end + + def start_link() do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init(_) do + {:ok, _} = Registry.register(IRC.PubSub, "trigger:beer", [plugin: __MODULE__]) + {:ok, %{}} + end + + def handle_info({:irc, :trigger, _, m = %IRC.Message{trigger: %{type: :bang, args: args}}}, state) do + case Untappd.search_beer(Enum.join(args, " "), limit: 1) do + {:ok, %{"response" => %{"beers" => %{"count" => count, "items" => [result | _]}}}} -> + %{"beer" => beer, "brewery" => brewery} = result + description = Map.get(beer, "beer_description") + |> String.replace("\n", " ") + |> String.replace("\r", " ") + |> String.trim() + beer_s = "#{Map.get(brewery, "brewery_name")}: #{Map.get(beer, "beer_name")} - #{Map.get(beer, "beer_abv")}°" + city = get_in(brewery, ["location", "brewery_city"]) + location = [Map.get(brewery, "brewery_type"), city, Map.get(brewery, "country_name")] + |> Enum.filter(fn(x) -> x end) + |> Enum.join(", ") + extra = "#{Map.get(beer, "beer_style")} - IBU: #{Map.get(beer, "beer_ibu")} - #{location}" + m.replyfun.([beer_s, extra, description]) + err -> + m.replyfun.("Error") + end + {:noreply, state} + end + + + def handle_info({:irc, :trigger, _, m = %IRC.Message{trigger: %{type: :query, args: args}}}, state) do + case Untappd.search_beer(Enum.join(args, " ")) do + {:ok, %{"response" => %{"beers" => %{"count" => count, "items" => results}}}} -> + beers = for %{"beer" => beer, "brewery" => brewery} <- results do + "#{Map.get(brewery, "brewery_name")}: #{Map.get(beer, "beer_name")} - #{Map.get(beer, "beer_abv")}°" + end + |> Enum.intersperse(", ") + |> Enum.join("") + m.replyfun.("#{count}. #{beers}") + err -> + m.replyfun.("Error") + end + {:noreply, state} + end + + def handle_info(info, state) do + {:noreply, state} + end + +end |