diff options
Diffstat (limited to 'lib/command/meme.ex')
| -rw-r--r-- | lib/command/meme.ex | 84 |
1 files changed, 43 insertions, 41 deletions
diff --git a/lib/command/meme.ex b/lib/command/meme.ex index 5be4582..50e8769 100644 --- a/lib/command/meme.ex +++ b/lib/command/meme.ex @@ -1,56 +1,58 @@ -alias Nostrum.Struct.Message +alias Nosedrum.TextCommand.Predicates +alias Thulani.Command.Util +alias Thulani.Schema.Meme +alias Thulani.Schema.Image +# alias Thulani.Schema.Audio defmodule Thulani.Command.Meme do - use GenServer + @behaviour Nosedrum.TextCommand - @type audio_permission :: :unrestrict | boolean + @impl true + def aliases, do: ["meme"] - defmodule Request do - alias Thulani.Command.Meme + @impl true + def usage, do: ["meme <title:str>"] - @type t :: - %__MODULE__{ - name: :random, - audio: Meme.audio_permission(), - msg: Message.t() - } - | %__MODULE__{ - name: String.t(), - audio: nil, - msg: Message.t() - } + @impl true + def predicates, do: [&Predicates.guild_only/1] - @enforce_keys [:msg] - defstruct name: :random, audio: :unrestrict, msg: nil - end + @impl true + def description, do: "post a random meme" - @spec random_meme(Message.t()) :: GenServer.cast() - def random_meme(msg) do - GenServer.cast(__MODULE__, %Request{msg: msg}) - end + @impl true + def command(msg, args) do + {:ok, meme} = + Thulani.Repo.transaction(fn -> + get(args) + |> Thulani.Repo.one() + |> Thulani.Repo.preload([:image, :audio, :metadata]) + end) - @spec meme(Message.t(), String.t()) :: GenServer.cast() - @spec meme(Message.t(), String.t(), audio_permission) :: GenServer.cast() - def meme(msg, name, audio \\ :unrestrict) do - GenServer.cast(__MODULE__, %Request{name: name, msg: msg, audio: audio}) + meme = post(meme) + msg |> Util.reply(meme) end - def start_link(arg) do - GenServer.start_link(__MODULE__, arg) - end + defp get([]), do: Thulani.Meme.random(Thulani.Meme.any()) + defp get([name]), do: Thulani.Meme.by_name(name) - @impl true - def init(_) do - {:ok, %{}} + @spec post(Meme.t()) :: Nostrum.Api.options() + defp post(%Meme{ + content: content, + image: image + }) do + [content: content] ++ post_image(image) end - @impl true - def handle_cast(%Request{name: :random, msg: _msg, audio: _audio}, state) do - {:noreply, state} - end + defp post_image(%Image{ + filename: filename, + data: data + }), + do: [ + file: %{ + name: filename, + body: data + } + ] - @impl true - def handle_cast(%Request{name: _name, msg: _msg}, state) do - {:noreply, state} - end + defp post_image(_x), do: [] end |
