或类似的功能,以防万一的声明?或者也许是更通用的模式?

发布于 2024-11-26 07:49:48 字数 717 浏览 0 评论 0原文

我已经学习 Erlang 一段时间了,为了学习它,我正在编写一个 IRC 机器人。该 IRC 机器人应该侦听“!command”和“Nick: command”形式的命令。我预先解析了 IRC 协议,以便我只需匹配发送消息。我正在使用这样的二进制模式来执行此操作:

case Msg of
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<"!rock">>] ->
        irckybot_api:privmsg(<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<BNick:Len/binary,": rock">>] ->
        irckybot_api:privmsg(<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
end

我必须为此编写两个模式,对吗?我不能将两种模式合并为一种吗?也许有更通用的模式?我真的不知道……

LG, CK

I've been learning Erlang for a while now, and for to learn it I'm writing a IRC bot. This IRC bot should listen to commands in the „!command“ and the „Nick: command“ form. I pre-parse the IRC protocol so that I have to match the send message only. I'm doing this with binary patterns like this:

case Msg of
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<"!rock">>] ->
        irckybot_api:privmsg(<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
    [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, <<BNick:Len/binary,": rock">>] ->
        irckybot_api:privmsg(<<"#",Channel/binary>>, [Nick, choose_hand(rock)]),
        {ok, State};
end

Am I right that I have to write two patterns for this? Can't I consolidate the two patterns to one? Maybe with a more generic pattern? I don't really know…

LG,
CK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-12-03 07:49:48

我想它可以更好地写成:

case Msg of
  [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, X] ->
    case X of
       <<"!rock">> -> ....;
       <<BNick:Len/binary,": rock">> -> .......
    end;
  _ -> .......
end

I guess it could be better written as :

case Msg of
  [Nick, _, <<"PRIVMSG">>, <<"#",Channel/binary>>, X] ->
    case X of
       <<"!rock">> -> ....;
       <<BNick:Len/binary,": rock">> -> .......
    end;
  _ -> .......
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文