aboutsummaryrefslogtreecommitdiff
path: root/apps/thulani_bot/lib
diff options
context:
space:
mode:
Diffstat (limited to 'apps/thulani_bot/lib')
-rw-r--r--apps/thulani_bot/lib/thulani/bot/application.ex13
-rw-r--r--apps/thulani_bot/lib/thulani/bot/config.ex58
-rw-r--r--apps/thulani_bot/lib/thulani/bot/consumer.ex23
3 files changed, 94 insertions, 0 deletions
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