don't give up too easily with invalid command, instead search factoid keys with wildcard first.

This commit is contained in:
nanotube 2010-03-23 15:46:22 -04:00 committed by Valentin Lorentz
parent b922890b6f
commit e71ee8fbb1
3 changed files with 28 additions and 1 deletions

View File

@ -60,6 +60,11 @@ conf.registerChannelValue(Factoids, 'replyWhenInvalidCommand',
registry.Boolean(True, _("""Determines whether the bot will reply to invalid registry.Boolean(True, _("""Determines whether the bot will reply to invalid
commands by searching for a factoid; basically making the whatis commands by searching for a factoid; basically making the whatis
unnecessary when you want all factoids for a given key."""))) 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', conf.registerChannelValue(Factoids, 'format',
FactoidFormat(_('$key could be $value.'), _("""Determines the format of FactoidFormat(_('$key could be $value.'), _("""Determines the format of
the response given when a factoid's value is requested. All the standard the response given when a factoid's value is requested. All the standard

View File

@ -199,6 +199,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
return cursor.fetchall() return cursor.fetchall()
#return [t[0] for t in 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): def _updateRank(self, channel, factoids):
if self.registryValue('keepRankInfo', channel): if self.registryValue('keepRankInfo', channel):
db = self.getDb(channel) db = self.getDb(channel)
@ -248,7 +258,16 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if self.registryValue('replyWhenInvalidCommand', channel): if self.registryValue('replyWhenInvalidCommand', channel):
key = ' '.join(tokens) key = ' '.join(tokens)
factoids = self._lookupFactoid(channel, key) factoids = self._lookupFactoid(channel, key)
if factoids:
self._replyFactoids(irc, msg, key, channel, factoids, error=False) 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 @internationalizeDocstring
def whatis(self, irc, msg, args, channel, words): def whatis(self, irc, msg, args, channel, words):

View File

@ -157,6 +157,9 @@ class FactoidsTestCase(ChannelPluginTestCase):
replyWhenInvalidCommand.setValue(True) replyWhenInvalidCommand.setValue(True)
self.assertNotError('learn foo as bar') self.assertNotError('learn foo as bar')
self.assertRegexp('foo', 'bar') self.assertRegexp('foo', 'bar')
self.assertNotError('learn mooz as cowz')
self.assertRegexp('moo', 'mooz')
self.assertError('nosuchthing')
finally: finally:
conf.supybot.plugins.Factoids.\ conf.supybot.plugins.Factoids.\
replyWhenInvalidCommand.setValue(orig) replyWhenInvalidCommand.setValue(orig)