i'm on a plane

This commit is contained in:
Ben Harris 2018-09-09 22:12:40 -04:00
parent 0e7a946792
commit 0a48aa4c61
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
13 changed files with 130 additions and 38 deletions

View File

@ -1,21 +1,13 @@
# Zaphod
# zaphod
**TODO: Add description**
zaphod is a proof-of-concept irc bot using ExIRC and a custom commands module.
## Installation
## installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `zaphod` to your list of dependencies in `mix.exs`:
* clone the repo.
```elixir
def deps do
[
{:zaphod, "~> 0.1.0"}
]
end
```
* `mix deps.get`
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/zaphod](https://hexdocs.pm/zaphod).
* `iex -S mix`
development channel for zaphod is #zaphod on the [tilde.chat](https://tilde.chat) network.

BIN
hhgtg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -81,7 +81,7 @@ defmodule Zaphod.Bot do
{:noreply, config}
end
@doc "handle commands with prefix"
# handle commands with prefix
def handle_info({:received, msg, %SenderInfo{:nick => nick}, channel}, config) do
Logger.info("#{nick} from #{channel}: #{msg}")
@ -94,7 +94,7 @@ defmodule Zaphod.Bot do
Logger.info("#{nick} used command #{command} in #{channel}")
case Commands.handle_command!(config, {command, args, nick, channel}) do
case Commands.handle_command!(config, command, args, nick, channel) do
{:ok, reply} ->
Client.msg(config.client, :privmsg, channel, reply)
@ -106,7 +106,7 @@ defmodule Zaphod.Bot do
:ok
end
# handle botlist for tilde.chat
# handle botlist command for tilde.chat
if msg == "!botlist" do
reply =
"hey i'm the president of the galaxy. ben wrote my code. see #{config.prefix}help. i have the following modules: #{
@ -119,7 +119,7 @@ defmodule Zaphod.Bot do
{:noreply, config}
end
@doc "handle mentions"
# handle mentions
def handle_info({:mentioned, msg, %SenderInfo{:nick => nick}, channel}, config) do
Logger.warn("#{nick} mentioned you in #{channel}")
@ -136,12 +136,12 @@ defmodule Zaphod.Bot do
{:noreply, config}
end
@doc "handle commands in /querys"
# handle commands in /querys
def handle_info({:received, msg, %SenderInfo{:nick => nick}}, config) do
Logger.warn("#{nick}: #{msg}")
[command | args] = String.split(msg)
case Commands.handle_command!(config, {command, args, nick, nick}) do
case Commands.handle_command!(config, command, args, nick, nick) do
{:ok, reply} ->
Client.msg(config.client, :privmsg, nick, reply)
@ -152,6 +152,7 @@ defmodule Zaphod.Bot do
{:noreply, config}
end
# join channels to which we're invited
def handle_info({:invited, sender, channel}, config) do
Logger.info("#{sender.nick} invited to #{channel}")
Client.join(config.client, channel)

View File

@ -3,12 +3,14 @@ defmodule Zaphod.Commands do
usage: nil,
helptext: nil
@doc "includes Zaphod.Commands.* and returns a map of name -> module"
@spec get_commands() :: %{name: %Zaphod.Commands{}}
def get_commands() do
with {:ok, list} <- :application.get_key(:zaphod, :modules) do
list
|> Enum.filter(fn module ->
split = Module.split(module)
Enum.at(split, 1) == "Commands" and Enum.count(split) > 2
Enum.at(split, 1) == "Commands" and Enum.count(split) == 3
end)
end
|> Enum.reduce(%{}, fn x, acc ->
@ -16,7 +18,15 @@ defmodule Zaphod.Commands do
end)
end
def handle_command!(config, {command, args, sender, channel}) do
@doc "delegate a command to a Commands module with matching name"
@spec handle_command!(
config :: %Zaphod.Bot.Config{},
command :: binary,
args :: list(binary),
sender :: binary,
channel :: binary
) :: {:ok, reply :: binary} | {:command_not_found}
def handle_command!(config, command, args, sender, channel) do
commands = get_commands()
case Map.has_key?(commands, command) do

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Admin do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "admin",
@ -7,7 +9,7 @@ defmodule Zaphod.Commands.Admin do
}
end
def run(config, args, sender, _channel) do
def run(config, args, sender, channel) do
owner = config.owner
case sender do
@ -18,7 +20,7 @@ defmodule Zaphod.Commands.Admin do
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
"stop" ->
ExIRC.Client.stop!(config.client)

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Ascii do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "ascii",
@ -7,14 +9,14 @@ defmodule Zaphod.Commands.Ascii do
}
end
def run(_config, args, _sender, _channel) do
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
_ ->
{:ok, Enum.join(args, " ")}

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Hhgtg do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "hhgtg",
@ -7,14 +9,14 @@ defmodule Zaphod.Commands.Hhgtg do
}
end
def run(_config, args, _sender, _channel) do
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
_ ->
:ok

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Source do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "source",
@ -7,14 +9,14 @@ defmodule Zaphod.Commands.Source do
}
end
def run(_config, args, _sender, _channel) do
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
_ ->
:ok

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Talk do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "talk",
@ -7,14 +9,14 @@ defmodule Zaphod.Commands.Talk do
}
end
def run(_config, args, _sender, _channel) do
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
_ ->
:ok

62
lib/commands/tictactoe.ex Normal file
View File

@ -0,0 +1,62 @@
defmodule Zaphod.Commands.Tictactoe do
alias Zaphod.Commands.Help
defmodule GameState do
defstruct channel: nil,
nicks: nil,
board: nil,
active: nil
end
def meta() do
%Zaphod.Commands{
name: "tictactoe",
usage: "start <opponent>|stop|<movenum>",
helptext: "play a game of tictactoe"
}
end
@spec print_board(board :: list()) :: binary
defp print_board(board) do
Enum.reduce(board, "", fn x, acc ->
acc = "#{Enum.join(x, " | ")}\n"
end)
end
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
Help.run(config, [meta().name], sender, channel)
"start" ->
[_ | rest] = args
opponent = hd(rest)
nicks = ExIRC.Client.who(config.client, channel)
IO.inspect(nicks)
cond do
opponent in nicks ->
{:ok, "starting a game"}
true ->
{:ok, "user not on this channel"}
end
"stop" ->
{:ok, "stopping the current game"}
_ ->
msg = Enum.join(args, " ")
{:ok, msg}
end
false ->
Help.run(config, [meta().name], sender, channel)
end
end
end

View File

@ -1,4 +1,6 @@
defmodule Zaphod.Commands.Time do
alias Zaphod.Commands.Help
def meta() do
%Zaphod.Commands{
name: "time",
@ -7,14 +9,14 @@ defmodule Zaphod.Commands.Time do
}
end
def run(_config, args, _sender, _channel) do
def run(config, args, sender, channel) do
case Enum.any?(args) do
true ->
subcmd = hd(args)
case subcmd do
"help" ->
{:ok, meta().helptext}
Help.run(config, [meta().name], sender, channel)
_ ->
:ok

16
mix.exs
View File

@ -4,10 +4,19 @@ defmodule Zaphod.MixProject do
def project do
[
app: :zaphod,
version: "0.1.0",
version: "0.1.0-dev",
elixir: "> 1.7.0",
start_permanent: Mix.env() == :prod,
deps: deps()
deps: deps(),
name: "Zaphod",
source_url: "https://tildegit.org/ben/zaphod",
homepage_url: "https://tilde.team/~ben/zaphod/",
docs: [
# The main page in the docs
main: "Zaphod",
logo: "hhgtg.jpg",
extras: ["README.md"]
]
]
end
@ -21,7 +30,8 @@ defmodule Zaphod.MixProject do
defp deps do
[
{:exirc, git: "https://github.com/benharri/exirc", tag: "1.0.2"}
{:exirc, git: "https://github.com/benharri/exirc", tag: "1.0.2"},
{:ex_doc, "~> 0.19", only: :dev, runtime: false}
]
end
end

View File

@ -1,3 +1,8 @@
%{
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"exirc": {:git, "https://github.com/benharri/exirc", "9fd1fa30023e6d149b74777b2e07785799fba44b", [tag: "1.0.2"]},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.8.0", "1204a2f5b4f181775a0e456154830524cf2207cf4f9112215c05e0b76e4eca8b", [:mix], [{:makeup, "~> 0.5.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.2.2", "d526b23bdceb04c7ad15b33c57c4526bf5f50aaa70c7c141b4b4624555c68259", [:mix], [], "hexpm"},
}