2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2018-08-28 14:33:18 +00:00
|
|
|
import time
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
2016-04-05 17:43:17 +00:00
|
|
|
|
2018-10-12 07:38:29 +00:00
|
|
|
WORD_DELIM = "\"'…~*`"
|
|
|
|
WORD_START = WORD_DELIM+"“({<"
|
|
|
|
WORD_STOP = WORD_DELIM+"”)}>;:.,!?"
|
2018-10-11 11:55:19 +00:00
|
|
|
|
2019-09-10 09:34:44 +00:00
|
|
|
SETTING = utils.BoolSetting("word-tracking",
|
|
|
|
"Disable/enable tracking your wordcounts")
|
|
|
|
|
|
|
|
@utils.export("set", SETTING)
|
|
|
|
@utils.export("channelset", SETTING)
|
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):
|
2019-09-10 09:34:44 +00:00
|
|
|
if not event["channel"].get_setting("word-tracking", True
|
|
|
|
) or not user.get_setting("word-tracking", True):
|
2019-05-15 21:44:47 +00:00
|
|
|
return
|
|
|
|
|
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:
|
2019-01-04 12:13:36 +00:00
|
|
|
stripped_word = word.lstrip(WORD_START).rstrip(WORD_STOP)
|
|
|
|
found = None
|
2018-08-03 12:44:50 +00:00
|
|
|
if word.lower() in tracked_words:
|
2019-01-04 12:13:36 +00:00
|
|
|
found = word.lower()
|
2019-01-04 12:36:38 +00:00
|
|
|
elif stripped_word.lower() in tracked_words:
|
2019-01-04 12:13:36 +00:00
|
|
|
found = stripped_word.lower()
|
|
|
|
|
|
|
|
if found:
|
|
|
|
setting = "word-%s" % found
|
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-03 12:22:37 +00:00
|
|
|
@utils.hook("received.message.channel",
|
2018-10-02 13:29:51 +00:00
|
|
|
priority=EventManager.PRIORITY_MONITOR)
|
2018-08-08 09:33:33 +00:00
|
|
|
def channel_message(self, event):
|
|
|
|
self._channel_message(event["user"], event)
|
2018-11-14 14:41:28 +00:00
|
|
|
@utils.hook("send.message.channel",
|
2018-10-02 13:29:51 +00:00
|
|
|
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-10-03 12:22:37 +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-10-03 12:22:37 +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-10-18 11:53:27 +00:00
|
|
|
@utils.hook("received.command.trackedwords")
|
|
|
|
def tracked_words(self, event):
|
|
|
|
"""
|
|
|
|
:help: List which words are being tracked on the current network
|
|
|
|
"""
|
2018-10-18 11:54:37 +00:00
|
|
|
event["stdout"].write("Tracked words: %s" % ", ".join(
|
2018-10-18 11:53:27 +00:00
|
|
|
event["server"].get_setting("tracked-words", [])))
|
|
|
|
|
2018-10-03 12:22:37 +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)
|
2018-10-04 04:33:15 +00:00
|
|
|
top_10 = utils.top_10(word_users,
|
2019-05-04 22:28:51 +00:00
|
|
|
convert_key=lambda nickname:
|
|
|
|
event["server"].get_user(nickname).nickname)
|
2018-10-04 04:33:15 +00:00
|
|
|
event["stdout"].write("Top '%s' users: %s" % (word,
|
|
|
|
", ".join(top_10)))
|
2016-04-10 16:32:33 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("That word is not being tracked")
|
2018-10-04 04:33:15 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.wordiest")
|
|
|
|
def wordiest(self, event):
|
|
|
|
"""
|
|
|
|
:help: Show wordiest users
|
|
|
|
:usage: [channel]
|
|
|
|
"""
|
|
|
|
channel_query = None
|
|
|
|
word_prefix = ""
|
|
|
|
if event["args_split"]:
|
|
|
|
channel_query = event["args_split"][0].lower()
|
|
|
|
word_prefix = " (%s)" % channel_query
|
|
|
|
|
|
|
|
words = event["server"].find_all_user_channel_settings("words")
|
|
|
|
user_words = {}
|
|
|
|
for channel_name, nickname, word_count in words:
|
|
|
|
if not channel_query or channel_name == channel_query:
|
|
|
|
if not nickname in user_words:
|
|
|
|
user_words[nickname] = 0
|
|
|
|
user_words[nickname] += word_count
|
|
|
|
|
|
|
|
top_10 = utils.top_10(user_words,
|
2019-05-04 22:28:51 +00:00
|
|
|
convert_key=lambda nickname:
|
|
|
|
event["server"].get_user(nickname).nickname)
|
2018-10-04 04:33:15 +00:00
|
|
|
event["stdout"].write("wordiest%s: %s" % (
|
|
|
|
word_prefix, ", ".join(top_10)))
|