2018-08-28 14:33:18 +00:00
|
|
|
import time
|
2018-10-02 13:29:51 +00:00
|
|
|
from src import EventManager, ModuleManager, Utils
|
2016-04-05 17:43:17 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
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-09-05 11:58:12 +00:00
|
|
|
user_words = event["channel"].get_user_setting(user.get_id(),
|
2018-08-18 23:19:53 +00:00
|
|
|
"words", 0)
|
2018-08-05 16:16:15 +00:00
|
|
|
user_words += word_count
|
2018-08-28 14:33:18 +00:00
|
|
|
|
|
|
|
if user.get_setting("first-words", None) == None:
|
|
|
|
user.set_setting("first-words", time.time())
|
|
|
|
|
2018-09-05 11:58:12 +00:00
|
|
|
event["channel"].set_user_setting(user.get_id(), "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)
|
2018-10-02 13:29:51 +00:00
|
|
|
@Utils.hook("received.message.channel",
|
|
|
|
priority=EventManager.PRIORITY_MONITOR)
|
2018-08-08 09:33:33 +00:00
|
|
|
def channel_message(self, event):
|
|
|
|
self._channel_message(event["user"], event)
|
2018-10-02 13:29:51 +00:00
|
|
|
@Utils.hook("self.message.channel",
|
|
|
|
priority=EventManager.PRIORITY_MONITOR)
|
2018-08-08 09:33:33 +00:00
|
|
|
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
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.words", channel_only=True)
|
2016-04-05 17:43:17 +00:00
|
|
|
def words(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: See how many words you or the given nickname have used
|
|
|
|
:usage: [nickname]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-05 17:43:17 +00:00
|
|
|
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
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.trackword", min_args=1)
|
2016-04-10 16:32:33 +00:00
|
|
|
def track_word(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Start tracking a word
|
|
|
|
:usage: <word>
|
|
|
|
:permission: track-word
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-10 16:32:33 +00:00
|
|
|
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:
|
2018-08-18 17:29:06 +00:00
|
|
|
event["stderr"].write("Already tracking '%s'" % word)
|
2016-04-10 16:32:33 +00:00
|
|
|
|
2018-09-30 16:29:09 +00:00
|
|
|
@Utils.hook("received.command.wordusers", min_args=1)
|
2016-04-10 16:32:33 +00:00
|
|
|
def word_users(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show who has used a tracked word the most
|
|
|
|
:usage: <word>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-04-10 16:32:33 +00:00
|
|
|
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, [])
|
2018-08-09 12:49:44 +00:00
|
|
|
items = [(word_user[0], word_user[1]) for word_user in word_users]
|
2018-08-03 12:44:50 +00:00
|
|
|
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-10 12:56:45 +00:00
|
|
|
top_10 = ", ".join("%s (%d)" % (Utils.prevent_highlight(event[
|
|
|
|
"server"].get_user(nickname).nickname), word_users[nickname]
|
2018-08-05 12:14:40 +00:00
|
|
|
) 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")
|