2016-04-05 17:43:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
bot.events.on("received").on("message").on("channel"
|
|
|
|
).hook(self.channel_message)
|
2018-08-08 09:33:33 +00:00
|
|
|
bot.events.on("self").on("message").on("channel"
|
|
|
|
).hook(self.self_channel_message)
|
2016-04-05 17:43:17 +00:00
|
|
|
bot.events.on("received").on("command").on("words"
|
2016-04-06 11:02:44 +00:00
|
|
|
).hook(self.words, channel_only=True,
|
2018-08-03 15:25:27 +00:00
|
|
|
usage="<nickname>", help=
|
|
|
|
"See how many words you or the given nickname have used")
|
2016-04-10 16:32:33 +00:00
|
|
|
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>")
|
2016-04-05 17:43:17 +00:00
|
|
|
|
2018-08-08 09:33:33 +00:00
|
|
|
def _channel_message(self, user, event):
|
2016-04-06 08:40:49 +00:00
|
|
|
words = list(filter(None, event["message_split"]))
|
|
|
|
word_count = len(words)
|
2018-08-03 12:44:50 +00:00
|
|
|
|
2018-08-05 16:16:15 +00:00
|
|
|
user_words = event["channel"].get_user_setting(
|
2018-08-08 09:33:33 +00:00
|
|
|
user.nickname, "words", 0)
|
2018-08-05 16:16:15 +00:00
|
|
|
user_words += word_count
|
2018-08-08 09:33:33 +00:00
|
|
|
event["channel"].set_user_setting(user.nickname,
|
2018-08-05 16:16:15 +00:00
|
|
|
"words", user_words)
|
2018-08-03 12:44:50 +00:00
|
|
|
|
|
|
|
tracked_words = set(event["server"].get_setting(
|
|
|
|
"tracked-words", []))
|
2016-04-06 08:40:49 +00:00
|
|
|
for word in words:
|
2018-08-03 12:44:50 +00:00
|
|
|
if word.lower() in tracked_words:
|
2016-04-06 08:40:49 +00:00
|
|
|
setting = "word-%s" % word
|
2018-08-08 09:33:33 +00:00
|
|
|
word_count = user.get_setting(setting, 0)
|
2018-08-03 12:44:50 +00:00
|
|
|
word_count += 1
|
2018-08-08 09:33:33 +00:00
|
|
|
user.set_setting(setting, word_count)
|
|
|
|
def channel_message(self, event):
|
|
|
|
self._channel_message(event["user"], event)
|
|
|
|
def self_channel_message(self, event):
|
|
|
|
self._channel_message(event["server"].get_user(
|
|
|
|
event["server"].nickname), event)
|
2016-04-05 17:43:17 +00:00
|
|
|
|
|
|
|
def words(self, event):
|
|
|
|
if event["args_split"]:
|
|
|
|
target = event["server"].get_user(event["args_split"
|
|
|
|
][0])
|
|
|
|
else:
|
|
|
|
target = event["user"]
|
2018-08-05 16:16:15 +00:00
|
|
|
words = dict(target.get_channel_settings_per_setting(
|
|
|
|
"words", []))
|
2016-04-05 17:43:17 +00:00
|
|
|
this_channel = words.get(event["target"].name, 0)
|
2018-08-05 16:16:15 +00:00
|
|
|
|
2016-04-05 17:43:17 +00:00
|
|
|
total = 0
|
|
|
|
for channel in words:
|
|
|
|
total += words[channel]
|
|
|
|
event["stdout"].write("%s has used %d words (%d in %s)" % (
|
|
|
|
target.nickname, total, this_channel, event["target"].name))
|
2016-04-10 16:32:33 +00:00
|
|
|
|
|
|
|
def track_word(self, event):
|
|
|
|
word = event["args_split"][0].lower()
|
2018-08-03 12:44:50 +00:00
|
|
|
tracked_words = event["server"].get_setting("tracked-words", [])
|
|
|
|
if not word in tracked_words:
|
|
|
|
tracked_words.append(word)
|
|
|
|
event["server"].set_setting("tracked-words", tracked_words)
|
2016-04-10 16:32:33 +00:00
|
|
|
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()
|
2018-08-03 12:44:50 +00:00
|
|
|
if word in event["server"].get_setting("tracked-words", []):
|
|
|
|
word_users = event["server"].get_all_user_settings(
|
|
|
|
"word-%s" % word, [])
|
|
|
|
items = [(word_user[0], word_user[2]) for word_user in word_users]
|
|
|
|
word_users = dict(items)
|
|
|
|
|
2016-04-18 15:51:06 +00:00
|
|
|
top_10 = sorted(word_users.keys())
|
|
|
|
top_10 = sorted(top_10, key=word_users.get, reverse=True)[:10]
|
2018-08-05 12:14:40 +00:00
|
|
|
top_10 = ", ".join("%s (%d)" % (event["server"].get_user(
|
|
|
|
nickname).nickname, word_users[nickname]
|
|
|
|
) for nickname in top_10)
|
2016-04-10 16:32:33 +00:00
|
|
|
event["stdout"].write("Top '%s' users: %s" % (word, top_10))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("That word is not being tracked")
|