diff options
| author | Nathan Perry <np@nathanperry.dev> | 2022-12-02 09:24:18 -0500 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2022-12-02 09:24:18 -0500 |
| commit | d876225c920b94798c01fd01f7a21aaf3aed9a55 (patch) | |
| tree | 698334c7ed6c094431cbd3a5aac649248a15b3a2 | |
| parent | 802b9dde7e8ecc17c42f6ddb122899892b8600a3 (diff) | |
bot: echo server
| -rw-r--r-- | .editorconfig | 23 | ||||
| -rw-r--r-- | config/config.exs | 18 | ||||
| -rw-r--r-- | config/runtime.exs | 1 | ||||
| -rw-r--r-- | flake.nix | 1 | ||||
| -rw-r--r-- | lib/application.ex | 10 | ||||
| -rw-r--r-- | lib/consumer.ex | 25 | ||||
| -rw-r--r-- | lib/message.ex | 47 | ||||
| -rw-r--r-- | lib/thulani.ex | 18 | ||||
| -rw-r--r-- | mix.exs | 11 | ||||
| -rw-r--r-- | mix.lock | 23 | ||||
| -rw-r--r-- | test/thulani_test.exs | 4 |
11 files changed, 154 insertions, 27 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ea19b66 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,23 @@ +# vim: ft=dosini : +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.nix] +indent_size = 2 + +[*.{yml,yaml}] +indent_size = 2 + +[Makefile] +indent_style = tab + +[*.{js,ts,jsx,tsx}] +indent_size = 2 + +[*.{ex,exs}] +indent_size = 2 diff --git a/config/config.exs b/config/config.exs new file mode 100644 index 0000000..2cac044 --- /dev/null +++ b/config/config.exs @@ -0,0 +1,18 @@ +import Config + +config :nostrum, + token: "NTM4MjA0NTYyMTU1MjQxNDgz.GHQMG6.WMNuny5iGpCvUKfLuQx_w9k6zkow7AhNNbw-B4", + gateway_intents: [ + :guild_messages, + :guild_message_reactions, + :message_content + ] + +config :logger, + compile_time_purge_matching: [ + [application: :nostrum, level_lower_than: :info] + ] + +config :logger, :console, + format: "[$level] $message ($metadata)\n", + metadata: [:module] diff --git a/config/runtime.exs b/config/runtime.exs new file mode 100644 index 0000000..becde76 --- /dev/null +++ b/config/runtime.exs @@ -0,0 +1 @@ +import Config @@ -55,7 +55,6 @@ in { devShells.default = pkgs.mkShell { buildInputs = (with pkgs; [ - (elixir_ls.override { elixir = local_elixir; }) local_elixir ]) ++ (deps pkgs); }; diff --git a/lib/application.ex b/lib/application.ex new file mode 100644 index 0000000..3445cf0 --- /dev/null +++ b/lib/application.ex @@ -0,0 +1,10 @@ +require Logger + +defmodule Thulani do + use Application + + def start(_typ, _arg) do + Logger.info('starting up') + Supervisor.start_link([Thulani.Consumer], strategy: :one_for_one) + end +end diff --git a/lib/consumer.ex b/lib/consumer.ex new file mode 100644 index 0000000..ace0ba8 --- /dev/null +++ b/lib/consumer.ex @@ -0,0 +1,25 @@ +require Logger +require OK + +alias Nostrum.Api + +defmodule Thulani.Consumer do + use Nostrum.Consumer + + def start_link do + Logger.info('starting consumer') + Nostrum.Consumer.start_link(__MODULE__) + end + + @spec handle_event(Nostrum.Consumer.message_create()) :: nil + def handle_event({:MESSAGE_CREATE, message, _ws_state}) do + case Thulani.Message.message(message) do + {:ok, content} -> Api.create_message!(message.channel_id, content) + :ignore -> nil + end + + nil + end + + def handle_event(_event), do: :noop +end diff --git a/lib/message.ex b/lib/message.ex new file mode 100644 index 0000000..f04e1e7 --- /dev/null +++ b/lib/message.ex @@ -0,0 +1,47 @@ +require Logger + +alias Nostrum.Struct.Message +alias Nostrum.Struct.User +alias Nostrum.Api + +defmodule Thulani.Message do + @prefix Application.compile_env(:thulani, :prefix, "!thulani") + + @spec message(Message.t()) :: {:ok, String.t()} | :ignore + def message(%Message{ + content: @prefix <> rest, + guild_id: guild_id, + author: %User{ + bot: nil + } + }) + when guild_id != nil do + {:ok, rest} + end + + def message(%Message{ + guild_id: guild_id, + author: %User{ + username: username, + discriminator: discriminator, + bot: bot + } + }) + when guild_id != nil do + {:ok, guild} = Api.get_guild(guild_id) + + Logger.debug(%{ + ignored: true, + guild: guild.name, + author: "#{username}##{discriminator} (bot: #{bot})" + }) + + :ignore + end + + def message(msg) do + Logger.debug(%{message: "unmatched message", msg: msg}) + + :ignore + end +end diff --git a/lib/thulani.ex b/lib/thulani.ex deleted file mode 100644 index 05b10ea..0000000 --- a/lib/thulani.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Thulani do - @moduledoc """ - Documentation for `Thulani`. - """ - - @doc """ - Hello world. - - ## Examples - - iex> Thulani.hello() - :world - - """ - def hello do - :world - end -end @@ -11,18 +11,21 @@ defmodule Thulani.MixProject do ] end - # Run "mix help compile.app" to learn about applications. def application do [ + mod: {Thulani, []}, extra_applications: [:logger] ] end - # Run "mix help deps" to learn about dependencies. defp deps do [ - # {:dep_from_hexpm, "~> 0.3.0"}, - # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + {:nostrum, "~> 0.6"}, + {:witchcraft, "~> 1.0"}, + {:algae, "~> 1.2"}, + {:quark, "~> 2.3"}, + {:exceptional, "~> 2.1"}, + {:ok, "~> 2.3"} ] end end diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..03f2879 --- /dev/null +++ b/mix.lock @@ -0,0 +1,23 @@ +%{ + "algae": {:hex, :algae, "1.3.1", "65c1a4747a80221ae3978524d621f3da0f7b7b53f99818464f3817a82d7b49fe", [:mix], [{:quark, "~> 2.2", [hex: :quark, repo: "hexpm", optional: false]}, {:type_class, "~> 1.2", [hex: :type_class, repo: "hexpm", optional: false]}, {:witchcraft, "~> 1.0", [hex: :witchcraft, repo: "hexpm", optional: false]}], "hexpm", "5d43987ab861082b461746a6814f75606f98a6d4b997c3f4bafe85c89996eb12"}, + "certifi": {:hex, :certifi, "2.10.0", "a4ab316320bfca83bd0b57fd022d091555d42a30eefabb38063887158294773a", [:rebar3], [], "hexpm", "e87a9dd6e7fe9c5804887850d4cdbcd83db4da7a27f928174f11e4e06fb7902e"}, + "chacha20": {:hex, :chacha20, "1.0.3", "26372176993172260968b36b4e7bc2e007e6b2b397ae08083e4836df67cdf03c", [:mix], [], "hexpm", "27f23b680e63f04b5bced77a9d8867033b381c091f92d544de0ad93accfb7cec"}, + "cowlib": {:hex, :remedy_cowlib, "2.11.1", "7abb4d0779a7d1c655f7642dc0bd0af754951e95005dfa01b500c68fe35a5961", [:rebar3], [], "hexpm", "0b613dc308e080cb6134285f1b1b55c3873e101652e70c70010fc6651c91b130"}, + "curve25519": {:hex, :curve25519, "1.0.4", "e570561b832c29b3ce4fd8b9fcd9c9546916188860568f1c1fc9428d7cb00894", [:mix], [], "hexpm", "1a068bf9646e7067bf6aa5bf802b404002734e09bb5300f098109df03e31f9f5"}, + "ed25519": {:hex, :ed25519, "1.4.1", "479fb83c3e31987c9cad780e6aeb8f2015fb5a482618cdf2a825c9aff809afc4", [:mix], [], "hexpm", "0dacb84f3faa3d8148e81019ca35f9d8dcee13232c32c9db5c2fb8ff48c80ec7"}, + "equivalex": {:hex, :equivalex, "1.0.3", "170d9a82ae066e0020dfe1cf7811381669565922eb3359f6c91d7e9a1124ff74", [:mix], [], "hexpm", "46fa311adb855117d36e461b9c0ad2598f72110ad17ad73d7533c78020e045fc"}, + "exceptional": {:hex, :exceptional, "2.1.3", "cb17cb9b7c4882e763b82db08ba317678157ca95970fae96b31b3c90f5960c3d", [:mix], [], "hexpm", "59d67ae2df6784e7a957087742ae9011f220c3d1523706c5cd7ee0741bca5897"}, + "gen_stage": {:hex, :gen_stage, "1.1.2", "b1656cd4ba431ed02c5656fe10cb5423820847113a07218da68eae5d6a260c23", [:mix], [], "hexpm", "9e39af23140f704e2b07a3e29d8f05fd21c2aaf4088ff43cb82be4b9e3148d02"}, + "gun": {:hex, :remedy_gun, "2.0.1", "0f0caed812ed9e4da4f144df2d5bf73b0a99481d395ecde990a3791decf321c6", [:rebar3], [{:cowlib, "~> 2.11.1", [hex: :remedy_cowlib, repo: "hexpm", optional: false]}], "hexpm", "b6685a85fbd12b757f86809be1b3d88fcef365b77605cd5aa34db003294c446e"}, + "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, + "kcl": {:hex, :kcl, "1.4.2", "8b73a55a14899dc172fcb05a13a754ac171c8165c14f65043382d567922f44ab", [:mix], [{:curve25519, ">= 1.0.4", [hex: :curve25519, repo: "hexpm", optional: false]}, {:ed25519, "~> 1.3", [hex: :ed25519, repo: "hexpm", optional: false]}, {:poly1305, "~> 1.0", [hex: :poly1305, repo: "hexpm", optional: false]}, {:salsa20, "~> 1.0", [hex: :salsa20, repo: "hexpm", optional: false]}], "hexpm", "9f083dd3844d902df6834b258564a82b21a15eb9f6acdc98e8df0c10feeabf05"}, + "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, + "nostrum": {:hex, :nostrum, "0.6.1", "aaad13e8e8ce8ace1f218685c6824972e7ea4f979e5ba3242a98f22bda52e605", [:mix], [{:certifi, "~> 2.8", [hex: :certifi, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.11 or ~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:gun, "2.0.1", [hex: :remedy_gun, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:kcl, "~> 1.4", [hex: :kcl, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm", "27925a47e413766664928fbeb38c9887a7fd23066cdba831d5fd8241072bf76a"}, + "ok": {:hex, :ok, "2.3.0", "0a3d513ec9038504dc5359d44e14fc14ef59179e625563a1a144199cdc3a6d30", [:mix], [], "hexpm", "f0347b3f8f115bf347c704184b33cf084f2943771273f2b98a3707a5fa43c4d5"}, + "operator": {:hex, :operator, "0.2.1", "4572312bbd3e63a5c237bf15c3a7670d568e3651ea744289130780006e70e5f5", [:mix], [], "hexpm", "1990cc6dc651d7fff04636eef06fc64e6bc1da83a1da890c08ca3432e17e267a"}, + "poly1305": {:hex, :poly1305, "1.0.4", "7cdc8961a0a6e00a764835918cdb8ade868044026df8ef5d718708ea6cc06611", [:mix], [{:chacha20, "~> 1.0", [hex: :chacha20, repo: "hexpm", optional: false]}, {:equivalex, "~> 1.0", [hex: :equivalex, repo: "hexpm", optional: false]}], "hexpm", "e14e684661a5195e149b3139db4a1693579d4659d65bba115a307529c47dbc3b"}, + "quark": {:hex, :quark, "2.3.2", "066e0d431440d077684469967f54d732443ea2a48932e0916e974633e8b39c95", [:mix], [], "hexpm", "2f6423779b02afe7e3e4af3cfecfcd94572f2051664d4d8329ffa872d24b10a8"}, + "salsa20": {:hex, :salsa20, "1.0.3", "fb900fc9b26b713a98618f3c6d6b6c35a5514477c6047caca8d03f3a70175ab0", [:mix], [], "hexpm", "91cbfa537f369d074a79f926f36a6c2ac24fba12cbadcb23aa04a759282887fe"}, + "type_class": {:hex, :type_class, "1.2.8", "349db84be8c664e119efaae1a09a44b113bc8e81af1d032f4e3e38feef4fac32", [:mix], [{:exceptional, "~> 2.1", [hex: :exceptional, repo: "hexpm", optional: false]}], "hexpm", "bb93de2cacfd6f0ee43f4616f7a139816a73deba4ae8ee3364bcfa4abe3eef3e"}, + "witchcraft": {:hex, :witchcraft, "1.0.4", "8733ac0ee769d4d2f73610de5a2b601a4ccbe385d1fca6419280f2511d21d0c9", [:mix], [{:exceptional, "~> 2.1", [hex: :exceptional, repo: "hexpm", optional: false]}, {:operator, "~> 0.2", [hex: :operator, repo: "hexpm", optional: false]}, {:quark, "~> 2.2", [hex: :quark, repo: "hexpm", optional: false]}, {:type_class, "~> 1.2", [hex: :type_class, repo: "hexpm", optional: false]}], "hexpm", "a380f439f1962d2e56cdad874ed7eb4612ddd6ec5ee3c6ad0c5d63e60539e6b0"}, +} diff --git a/test/thulani_test.exs b/test/thulani_test.exs index 032d9e4..cbf84f8 100644 --- a/test/thulani_test.exs +++ b/test/thulani_test.exs @@ -1,8 +1,4 @@ defmodule ThulaniTest do use ExUnit.Case doctest Thulani - - test "greets the world" do - assert Thulani.hello() == :world - end end |
