启动ExIrc主管失败

时间:2016-04-05 13:39:03

标签: elixir irc

我正在修改ExIrc以制作一个简单的机器人,但我无法使它工作。

我收到此错误:

** (Mix) Could not start application streamingutils: TwitchSniper.start(:normal,
 []) returned an error: shutdown: failed to start child: TwitchSniper.Bot
    ** (EXIT) an exception was raised:
        ** (ArgumentError) argument error
            :erlang.apply([%TwitchSniper.Bot.State{client: nil, handlers: [], host: "irc.chat.twitch.tv", name: "Paul Schoenfelder", nick: "hajtosek", pass: "my password", port: 6667, user: "hajtosek"}], :host, [])
            (streamingutils) TwitchSniper.Bot.init/1
            (stdlib) gen_server.erl:328: :gen_server.init_it/6
            (stdlib) proc_lib.erl:239: :proc_lib.init_p_do_apply/3

我试图使用ExIrc库:https://github.com/bitwalker/exirc

我刚刚从自述文件中复制了大部分代码,只是交换了数据

代码:

defmodule State do
    defstruct host: "irc.chat.twitch.tv",
              port: 6667,
              pass: "password",
              nick: "hajtosek",
              user: "hajtosek",
              name: "Paul Schoenfelder",
              client: nil,
              handlers: []
end

def start_link(_) do
    GenServer.start_link(__MODULE__, [%State{}])
end

def init(state) do
    # Start the client and handler processes, the ExIrc supervisor is automatically started when your app runs
    {:ok, client}  = ExIrc.start_client!()
    #{:ok, handler} = ExampleHandler.start_link(nil)

    # Register the event handler with ExIrc
    ExIrc.Client.add_handler client, self

    # Connect and logon to a server, join a channel and send a simple message
    ExIrc.Client.connect!   client, state.host, state.port
    ExIrc.Client.logon      client, state.pass, state.nick, state.user, state.name
    ExIrc.Client.join       client, "#channel"
    ExIrc.Client.msg        client, :privmsg, "#channel", "Hello world!"
    ExIrc.Client.msg        client, :ctcp, "#channel", "Hello world!"

    IO.inspect "IRC activated"

    {:ok, %{state | :client => client, :handlers => [self]}}
end

def terminate(_, state) do
    # Quit the channel and close the underlying client connection when the process is terminating
    ExIrc.Client.quit state.client, "Goodbye, cruel world."
    ExIrc.Client.stop! state.client
    :ok
end

1 个答案:

答案 0 :(得分:2)

这似乎是自述文件中的拼写错误。 init的state参数始终是一个列表(它接收给GenServer.start_link的列表)。所以问题在于,当它不是一个结构时,你试图使用类似结构的状态。只需将init的函数头更改为[state]而不是state,您就可以了。

编辑:另外值得注意的是,你应该看看GitHub上的examples文件夹中有关使用exirc的完整应用程序,它们是更现实的例子。