1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import re
import random
import types
import net.mapserv as mapserv
from utils import extends
import chat
__all__ = [ 'PLUGIN', 'init', 'answer', 'add_command', 'remove_command' ]
PLUGIN = {
'name': 'chatbot',
'requires': (),
'blocks': (),
}
commands = {}
def answer_info(nick, message, is_whisper, match):
if is_whisper:
chat.send_whisper(nick, "This is an experimental bot.")
def answer_random(nick, message, is_whisper, answers):
resp = random.choice(answers)
if is_whisper:
chat.send_whisper(nick, resp)
else:
mapserv.cmsg_chat_message(resp)
def answer(nick, message, is_whisper):
try:
for regex, action in commands.iteritems():
match = regex.match(message)
if match:
if isinstance(action, types.ListType):
answer_random(nick, message, is_whisper, action)
elif isinstance(action, types.FunctionType):
action(nick, message, is_whisper, match)
else:
raise ValueError("must be either list or function")
except:
answer_random(nick, message, is_whisper, action)
@extends('smsg_being_chat')
def being_chat(data):
idx = data.message.find(' : ')
if idx > -1:
nick = data.message[:idx]
message = data.message[idx + 3:]
answer(nick, message, False)
@extends('smsg_whisper')
def got_whisper(data):
nick, message = data.nick, data.message
answer(nick, message, True)
def add_command(cmd, action):
cmd_re = re.compile(cmd)
commands[cmd_re] = action
def remove_command(cmd):
cmd_re = re.compile(cmd)
commands.remove(cmd_re)
def init(config):
add_command('!info', answer_info)
add_command('!random', ['asd', 'Ciao!'])
|