diff options
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/thulani_bot/.formatter.exs | 3 | ||||
| -rw-r--r-- | apps/thulani_bot/.gitignore | 24 | ||||
| -rw-r--r-- | apps/thulani_bot/README.md | 2 | ||||
| -rw-r--r-- | apps/thulani_bot/lib/thulani/bot/application.ex | 13 | ||||
| -rw-r--r-- | apps/thulani_bot/lib/thulani/bot/config.ex | 58 | ||||
| -rw-r--r-- | apps/thulani_bot/lib/thulani/bot/consumer.ex | 23 | ||||
| -rw-r--r-- | apps/thulani_bot/mix.exs | 32 | ||||
| -rw-r--r-- | apps/thulani_bot/test/test_helper.exs | 1 | ||||
| -rw-r--r-- | apps/thulani_bot/test/thulani/bot/application_test.exs | 4 | ||||
| -rw-r--r-- | apps/util/.formatter.exs | 4 | ||||
| -rw-r--r-- | apps/util/.gitignore | 24 | ||||
| -rw-r--r-- | apps/util/README.md | 21 | ||||
| -rw-r--r-- | apps/util/lib/thulani/util.ex | 2 | ||||
| -rw-r--r-- | apps/util/lib/thulani/util/compose.ex | 13 | ||||
| -rw-r--r-- | apps/util/lib/thulani/util/curry.ex | 16 | ||||
| -rw-r--r-- | apps/util/mix.exs | 27 | ||||
| -rw-r--r-- | apps/util/test/test_helper.exs | 1 | ||||
| -rw-r--r-- | apps/util/test/thulani/util_test.exs | 4 |
18 files changed, 272 insertions, 0 deletions
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 diff --git a/apps/util/.formatter.exs b/apps/util/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/apps/util/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/apps/util/.gitignore b/apps/util/.gitignore new file mode 100644 index 0000000..d8dbee4 --- /dev/null +++ b/apps/util/.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"). +util-*.tar + diff --git a/apps/util/README.md b/apps/util/README.md new file mode 100644 index 0000000..4b69d4c --- /dev/null +++ b/apps/util/README.md @@ -0,0 +1,21 @@ +# Thulani.Util + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `util` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:util, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/util](https://hexdocs.pm/util). + diff --git a/apps/util/lib/thulani/util.ex b/apps/util/lib/thulani/util.ex new file mode 100644 index 0000000..fad5e39 --- /dev/null +++ b/apps/util/lib/thulani/util.ex @@ -0,0 +1,2 @@ +defmodule Thulani.Util do +end diff --git a/apps/util/lib/thulani/util/compose.ex b/apps/util/lib/thulani/util/compose.ex new file mode 100644 index 0000000..cd4435e --- /dev/null +++ b/apps/util/lib/thulani/util/compose.ex @@ -0,0 +1,13 @@ +defmodule Thulani.Util.Compose do + import Thulani.Util.Curry + + def f <|> g, do: compose(f, g) + + def compose(f, g) when is_function(g) do + fn arg -> compose(curry(f), curry(g).(arg)) end + end + + def compose(f, arg) do + f.(arg) + end +end diff --git a/apps/util/lib/thulani/util/curry.ex b/apps/util/lib/thulani/util/curry.ex new file mode 100644 index 0000000..b03afab --- /dev/null +++ b/apps/util/lib/thulani/util/curry.ex @@ -0,0 +1,16 @@ +defmodule Thulani.Util.Curry do + @moduledoc false + + def curry(f) do + {_, arity} = :erlang.fun_info(f, :arity) + curry(f, arity, []) + end + + defp curry(f, 0, args) do + apply(f, Enum.reverse(args)) + end + + defp curry(f, arity, args) do + fn arg -> curry(f, arity - 1, [arg | args]) end + end +end diff --git a/apps/util/mix.exs b/apps/util/mix.exs new file mode 100644 index 0000000..a0caff9 --- /dev/null +++ b/apps/util/mix.exs @@ -0,0 +1,27 @@ +defmodule Thulani.Util.MixProject do + use Mix.Project + + def project do + [ + app: :util, + 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] + ] + end + + defp deps do + [] + end +end diff --git a/apps/util/test/test_helper.exs b/apps/util/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/apps/util/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/apps/util/test/thulani/util_test.exs b/apps/util/test/thulani/util_test.exs new file mode 100644 index 0000000..9246f40 --- /dev/null +++ b/apps/util/test/thulani/util_test.exs @@ -0,0 +1,4 @@ +defmodule Thulani.UtilTest do + use ExUnit.Case + doctest Thulani.Util +end |
