Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command refactor #37

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/fruitbot/command.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Fruitbot.Command do
defstruct aliases: [], handler: nil
end
31 changes: 31 additions & 0 deletions lib/fruitbot/command_handler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Fruitbot.CommandHandler do
import Fruitbot.Commands, only: [all_commands: 0]

def handle_command(payload) do
IO.inspect(payload)
[command | tail] = String.split(payload)
query = Enum.join(tail)
# how to pass query????

case find_command(command) do
nil ->
IO.puts("Command not found: #{command}")
{:error, :bad_command}
c -> execute_command(c, query)
end
end

defp find_command(message) do
IO.puts(message)
Enum.find(all_commands(), fn command ->
IO.puts(command.aliases)
c = message |> String.split("!") |> List.last()
IO.puts(c)
Enum.any?(command.aliases, &(&1 == c))
end)
end

defp execute_command(%Fruitbot.Command{handler: handler}, query) do
handler.(query)
end
end
Loading