-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerybot.py
More file actions
71 lines (61 loc) · 3.07 KB
/
Copy pathquerybot.py
File metadata and controls
71 lines (61 loc) · 3.07 KB
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
#encoding: utf-8
from irc import IRCBot
from google.googleSearch import GoogleSearch
from music.musicStatus import MusicStatus
from urlshortener.shortener import Shortener
from wolfram.wolfram import Wolfram
from ConfigParser import SafeConfigParser
import random
import codecs
config_name_g = u'config'
class QueryBot(IRCBot):
def __init__(self, *args, **kwargs):
# enable by default modules that don't require configuration, do it before the innit
# of the super class because otherwise it tries to register the callbacks and crashes
# when the list is not defined
shortener = Shortener()
modules = [shortener, GoogleSearch(), MusicStatus(), Wolfram()]
self.enabled_modules = [module for module in modules if not module.uses_settings()]
#last response given, to avoid spam
self.last_response = u''
conf = False
if(config_name_g in kwargs):
parser = SafeConfigParser()
with codecs.open(kwargs[config_name_g], 'r', encoding='utf-8') as f:
parser.readfp(f)
if parser.sections() != []:
conf = True
#log "configuration file has " + "been loaded" if conf else "not been loaded"
if conf:
#fit all the configuration into a {str: {str: str}}
configs = dict([(module, {option: value for (option, value) in parser.items(module)})
for module in parser.sections()])
#enable only the modules which get configured properly
get_conf_name = lambda m: (m, m.get_settings_name())
for module, name in map(get_conf_name, modules):
if name in configs:
try:
module.set_configuration(configs[name])
self.enabled_modules.append(module)
except RuntimeError:
print ('Couldn\'t load configuration for module "' + str(module)) + '"'
# now that we have decided what modules can function let's finish configuring them
if(shortener in self.enabled_modules):
for module in self.enabled_modules:
module.set_url_shortener(shortener)
# essential to do now because we have now the enabled modules
super(QueryBot, self).__init__(*args, **kwargs)
def command_patterns(self):
return [com_pat for module in self.enabled_modules for com_pat in module.get_command_patterns()] + [
('(?i)^f+$', self._fuckYou),
('^\.h(elp)?', self._help)
]
def _fuckYou(self, nick, message, channel):
response = u'Fuck you, %s' % nick
if random.random() < 0.4 and response != self.last_response:
return response.encode('utf-8', errors='ignore')
def _help(self, nick, message, channel):
result = 'Comandos disponibles: google: ".g <busqueda>" --- conversor/calculadora: ".c <operacion>" --- wolfram|alpha: ".wa <pregunta>"'
if result != self.last_response:
self.last_response = result
return result.encode('utf-8', errors='ignore')