beefed up the words.py module.

This commit is contained in:
jesopo 2016-04-10 17:32:33 +01:00
parent 1750c2b1ec
commit ab05ea6509
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3

View file

@ -9,6 +9,14 @@ class Module(object):
bot.events.on("received").on("command").on("words" bot.events.on("received").on("command").on("words"
).hook(self.words, channel_only=True, ).hook(self.words, channel_only=True,
usage="<nickname>") usage="<nickname>")
bot.events.on("received").on("command").on("trackword"
).hook(self.track_word, min_args=1,
help="Start tracking a word", usage="<word>",
permission="track-word")
bot.events.on("received").on("command").on("wordusers"
).hook(self.word_users, min_args=1,
help="Show who has used a tracked word the most",
usage="<word>")
def new_server(self, event): def new_server(self, event):
event["server"].tracked_words = set([]) event["server"].tracked_words = set([])
@ -20,11 +28,11 @@ class Module(object):
def channel_message(self, event): def channel_message(self, event):
words = list(filter(None, event["message_split"])) words = list(filter(None, event["message_split"]))
word_count = len(words) word_count = len(words)
words = event["user"].get_setting("words", {}) user_words = event["user"].get_setting("words", {})
if not event["channel"].name in words: if not event["channel"].name in user_words:
words[event["channel"].name] = 0 user_words[event["channel"].name] = 0
words[event["channel"].name] += word_count user_words[event["channel"].name] += word_count
event["user"].set_setting("words", words) event["user"].set_setting("words", user_words)
for word in words: for word in words:
if word.lower() in event["server"].tracked_words: if word.lower() in event["server"].tracked_words:
setting = "word-%s" % word setting = "word-%s" % word
@ -47,3 +55,23 @@ class Module(object):
total += words[channel] total += words[channel]
event["stdout"].write("%s has used %d words (%d in %s)" % ( event["stdout"].write("%s has used %d words (%d in %s)" % (
target.nickname, total, this_channel, event["target"].name)) target.nickname, total, this_channel, event["target"].name))
def track_word(self, event):
word = event["args_split"][0].lower()
if not word in event["server"].tracked_words:
event["server"].tracked_words.add(word)
event["server"].set_setting("word-%s" % word, {})
event["stdout"].write("Now tracking '%s'" % word)
else:
event["stderr"].wrote("Already tracking '%s'" % word)
def word_users(self, event):
word = event["args_split"][0].lower()
if word in event["server"].tracked_words:
word_users = event["server"].get_setting("word-%s" % word)
top_10 = sorted(word_users, key=word_users.get, reverse=True)[:10]
top_10 = ", ".join("%s (%d)" % (nickname, word_users[nickname
]) for nickname in top_10)
event["stdout"].write("Top '%s' users: %s" % (word, top_10))
else:
event["stderr"].write("That word is not being tracked")