From 5dadc47eb07be076a8b270458badb9d8cd7dfead Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 28 Jan 2020 16:06:48 -0500 Subject: start elixir conversion --- apps/thulani_bot/.formatter.exs | 3 ++ apps/thulani_bot/.gitignore | 24 +++++++++ apps/thulani_bot/README.md | 2 + apps/thulani_bot/lib/thulani/bot/application.ex | 13 +++++ apps/thulani_bot/lib/thulani/bot/config.ex | 58 ++++++++++++++++++++++ apps/thulani_bot/lib/thulani/bot/consumer.ex | 23 +++++++++ apps/thulani_bot/mix.exs | 32 ++++++++++++ apps/thulani_bot/test/test_helper.exs | 1 + .../test/thulani/bot/application_test.exs | 4 ++ 9 files changed, 160 insertions(+) create mode 100644 apps/thulani_bot/.formatter.exs create mode 100644 apps/thulani_bot/.gitignore create mode 100644 apps/thulani_bot/README.md create mode 100644 apps/thulani_bot/lib/thulani/bot/application.ex create mode 100644 apps/thulani_bot/lib/thulani/bot/config.ex create mode 100644 apps/thulani_bot/lib/thulani/bot/consumer.ex create mode 100644 apps/thulani_bot/mix.exs create mode 100644 apps/thulani_bot/test/test_helper.exs create mode 100644 apps/thulani_bot/test/thulani/bot/application_test.exs (limited to 'apps/thulani_bot') diff --git a/apps/thulani_bot/.formatter.exs b/apps/thulani_bot/.formatter.exs new file mode 100644 index 0000000..d304ff3 --- /dev/null +++ b/apps/thulani_bot/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/apps/thulani_bot/.gitignore b/apps/thulani_bot/.gitignore new file mode 100644 index 0000000..302c179 --- /dev/null +++ b/apps/thulani_bot/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +thulani_bot-*.tar + diff --git a/apps/thulani_bot/README.md b/apps/thulani_bot/README.md new file mode 100644 index 0000000..77f6232 --- /dev/null +++ b/apps/thulani_bot/README.md @@ -0,0 +1,2 @@ +# Thulani.Bot +The actual bot running in discord. diff --git a/apps/thulani_bot/lib/thulani/bot/application.ex b/apps/thulani_bot/lib/thulani/bot/application.ex new file mode 100644 index 0000000..63bd89e --- /dev/null +++ b/apps/thulani_bot/lib/thulani/bot/application.ex @@ -0,0 +1,13 @@ +defmodule Thulani.Bot.Application do + alias Thulani.Bot.Config + use Application + + def start(_type, _args) do + Config.init!() + + children = [] + + opts = [strategy: :one_for_one, name: Thulani.Bot.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/apps/thulani_bot/lib/thulani/bot/config.ex b/apps/thulani_bot/lib/thulani/bot/config.ex new file mode 100644 index 0000000..5542752 --- /dev/null +++ b/apps/thulani_bot/lib/thulani/bot/config.ex @@ -0,0 +1,58 @@ +defmodule Thulani.Bot.Config do + @env_vars %{ + database_url: nil, + spreadsheet_id: nil, + sheets_api_key: nil, + steam_api_key: nil, + max_sheet_column: "zz", + default_hist: "5", + max_hist: "30" + } + + require Logger + + def init!() do + load_env() + |> Enum.each(fn {application, vals} -> + Enum.each(vals, fn {key, val} -> Application.put_env(application, key, val) end) + end) + + if System.get_env("THULANI_DEBUG") do + Application.put_env(:logger, :level, :debug) + end + end + + def load_env() do + if Application.get_env(:thulani, :env) == :dev, do: load_dotenv() + + %{ + nostrum: [ + token: System.fetch_env!("THULANI_TOKEN"), + shards: System.get_env("THULANI_DISCORD_SHARDS", "1") |> Integer.parse() + ], + thulani: thulani_env() + } + end + + defp thulani_env do + @env_vars + |> Enum.map(fn {env_var, default} -> + canonical_env_var = + env_var + |> to_string + |> String.upcase() + |> (fn x -> "THULANI_" <> x end).() + + value = + canonical_env_var + |> System.get_env(default) + + if value == nil do + raise "required environment variable not found: #{canonical_env_var}" + end + + {env_var, value} + end) + end + +end diff --git a/apps/thulani_bot/lib/thulani/bot/consumer.ex b/apps/thulani_bot/lib/thulani/bot/consumer.ex new file mode 100644 index 0000000..f5deec6 --- /dev/null +++ b/apps/thulani_bot/lib/thulani/bot/consumer.ex @@ -0,0 +1,23 @@ +defmodule Thulani.Bot.Consumer do + use Nostrum.Consumer + + alias Nostrum.Api + require Logger + + def start_link do + Consumer.start_link(__MODULE__) + end + + def handle_event({:MESSAGE_CREATE, {msg}, ws_state}, state) do + case msg.content do + "!thulani " <> command -> Logger.debug("got command", command: command) + _ -> :ignore + end + + {:ok, state} + end + + def handle_event(_, state) do + {:ok, state} + end +end diff --git a/apps/thulani_bot/mix.exs b/apps/thulani_bot/mix.exs new file mode 100644 index 0000000..13afa4f --- /dev/null +++ b/apps/thulani_bot/mix.exs @@ -0,0 +1,32 @@ +defmodule Thulani.Bot.MixProject do + use Mix.Project + + def project do + [ + app: :thulani_bot, + version: "0.1.0", + build_path: "../../_build", + config_path: "../../config/config.exs", + deps_path: "../../deps", + lockfile: "../../mix.lock", + elixir: "~> 1.9", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + def application do + [ + extra_applications: [:logger], + mod: {Thulani.Bot.Application, []} + ] + end + + defp deps do + [ + # we look up the token based on an environment variable, so we can't do this + {:nostrum, "~> 0.4"}, + {:util, in_umbrella: true} + ] + end +end diff --git a/apps/thulani_bot/test/test_helper.exs b/apps/thulani_bot/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/apps/thulani_bot/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/apps/thulani_bot/test/thulani/bot/application_test.exs b/apps/thulani_bot/test/thulani/bot/application_test.exs new file mode 100644 index 0000000..2cc18fb --- /dev/null +++ b/apps/thulani_bot/test/thulani/bot/application_test.exs @@ -0,0 +1,4 @@ +defmodule Thulani.Bot.ApplicationTest do + use ExUnit.Case + doctest Thulani.Bot.Application +end -- cgit v1.3.1