mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
don't give up too easily with invalid command, instead search factoid keys with wildcard first.
This commit is contained in:
parent
b922890b6f
commit
e71ee8fbb1
@ -60,6 +60,11 @@ conf.registerChannelValue(Factoids, 'replyWhenInvalidCommand',
|
||||
registry.Boolean(True, _("""Determines whether the bot will reply to invalid
|
||||
commands by searching for a factoid; basically making the whatis
|
||||
unnecessary when you want all factoids for a given key.""")))
|
||||
conf.registerChannelValue(Factoids, 'replyWhenInvalidCommandSearchKeys',
|
||||
registry.Boolean(True, _("""If replyWhenInvalidCommand is True, and you
|
||||
supply a nonexistent factoid as a command, this setting make the bot try a
|
||||
wildcard search for factoid keys, returning a list of matching keys,
|
||||
before giving up with an invalid command error.""")))
|
||||
conf.registerChannelValue(Factoids, 'format',
|
||||
FactoidFormat(_('$key could be $value.'), _("""Determines the format of
|
||||
the response given when a factoid's value is requested. All the standard
|
||||
|
@ -199,6 +199,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
|
||||
return cursor.fetchall()
|
||||
#return [t[0] for t in cursor.fetchall()]
|
||||
|
||||
def _searchFactoid(self, channel, key):
|
||||
db = self.getDb(channel)
|
||||
cursor = db.cursor()
|
||||
key = '%' + key + '%'
|
||||
cursor.execute("""SELECT key FROM keys
|
||||
WHERE key LIKE ?
|
||||
LIMIT 20""", (key,))
|
||||
return cursor.fetchall()
|
||||
|
||||
|
||||
def _updateRank(self, channel, factoids):
|
||||
if self.registryValue('keepRankInfo', channel):
|
||||
db = self.getDb(channel)
|
||||
@ -248,7 +258,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
|
||||
if self.registryValue('replyWhenInvalidCommand', channel):
|
||||
key = ' '.join(tokens)
|
||||
factoids = self._lookupFactoid(channel, key)
|
||||
if factoids:
|
||||
self._replyFactoids(irc, msg, key, channel, factoids, error=False)
|
||||
else:
|
||||
if self.registryValue('replyWhenInvalidCommandSearchKeys'):
|
||||
factoids = self._searchFactoid(channel, key)
|
||||
#print 'searchfactoids result:', factoids, '>'
|
||||
if factoids:
|
||||
keylist = ["'%s'" % (fact[0],) for fact in factoids]
|
||||
keylist = ', '.join(keylist)
|
||||
irc.reply("I do not know about '%s', but I do know about these similar topics: %s" % (key, keylist))
|
||||
|
||||
@internationalizeDocstring
|
||||
def whatis(self, irc, msg, args, channel, words):
|
||||
|
@ -157,6 +157,9 @@ class FactoidsTestCase(ChannelPluginTestCase):
|
||||
replyWhenInvalidCommand.setValue(True)
|
||||
self.assertNotError('learn foo as bar')
|
||||
self.assertRegexp('foo', 'bar')
|
||||
self.assertNotError('learn mooz as cowz')
|
||||
self.assertRegexp('moo', 'mooz')
|
||||
self.assertError('nosuchthing')
|
||||
finally:
|
||||
conf.supybot.plugins.Factoids.\
|
||||
replyWhenInvalidCommand.setValue(orig)
|
||||
|
Loading…
Reference in New Issue
Block a user