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(开发者_Go百科<<"#",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
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
精彩评论