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")
|
2020-02-06 16:52:09 +00:00
|
|
|
REGISTERED_SETTING = utils.BoolSetting("word-tracking-registered",
|
|
|
|
"Whether or not word tracking is registered-users-only")
|
2019-09-10 09:34:44 +00:00
|
|
|
|
|
|
|
@utils.export("set", SETTING)
|
|
|
|
@utils.export("channelset", SETTING)
|
2020-02-06 16:52:09 +00:00
|
|
|
@utils.export("serverset", REGISTERED_SETTING)
|
|
|
|
@utils.export("channelset", REGISTERED_SETTING)
|
2020-01-23 14:15:26 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("words-prevent-highlight",
|
|
|
|
"Whether or not to prevent highlights in wordiest lists"))
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2020-02-06 16:40:46 +00:00
|
|
|
def on_load(self):
|
|
|
|
if not self.bot.database.has_table("words"):
|
|
|
|
self.bot.database.execute("""CREATE TABLE words
|
|
|
|
(user_id INTEGER, channel_id INTEGER, date TEXT, count INTEGER,
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users(user_id),
|
|
|
|
FOREIGN KEY (channel_id) REFERENCES channels(channel_id),
|
|
|
|
PRIMARY KEY (user_id, channel_id, date))""")
|
|
|
|
|
|
|
|
def _get_words_date(self, user, channel, date):
|
|
|
|
words = self.bot.database.execute_fetchone("""SELECT count FROM words
|
|
|
|
WHERE user_id=? AND channel_id=? AND date=?""",
|
|
|
|
[user.get_id(), channel.id, date])
|
|
|
|
return (words or [0])[0]
|
|
|
|
def _set_words_date(self, user, channel, date, count):
|
|
|
|
self.bot.database.execute("""
|
|
|
|
INSERT OR REPLACE INTO words (user_id, channel_id, date, count)
|
|
|
|
VALUES (?, ?, ?, ?)""", [user.get_id(), channel.id, date, count])
|
|
|
|
|
|
|
|
def _channel_between_dates(self, channel, date1, date2):
|
|
|
|
return self.bot.database.execute_fetchall("""
|
|
|
|
SELECT user_id, count FROM words
|
|
|
|
WHERE channel_id=? AND date>=? AND date<=?""",
|
|
|
|
[channel.id, date1, date2])
|
|
|
|
def _channel_all(self, channel):
|
|
|
|
return self.bot.database.execute_fetchall(
|
|
|
|
"SELECT user_id, count FROM words WHERE channel_id=?",
|
|
|
|
[channel.id])
|
|
|
|
|
|
|
|
def _user_between_dates(self, user, channel, date1, date2):
|
|
|
|
return self.bot.datebase.execute_fetchall("""
|
|
|
|
SELECT count FROM words
|
|
|
|
WHERE user_id=? AND channel_id=? AND date>=? AND date<=?""",
|
|
|
|
[user.get_id(), channel.id, date1, date2])
|
|
|
|
def _user_all(self, user):
|
|
|
|
return self.bot.database.execute_fetchall(
|
|
|
|
"SELECT channel_id, count FROM words WHERE user_id=?",
|
|
|
|
[user.get_id()])
|
|
|
|
|
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
|
|
|
|
|
2020-02-06 16:52:09 +00:00
|
|
|
if event["channel"].get_setting("word-tracking-registered",
|
|
|
|
event["server"].get_setting("word-tracking-registered", False)):
|
2020-03-08 14:14:00 +00:00
|
|
|
if not self.exports.get("is-identified")(event["user"]):
|
2020-02-06 16:52:09 +00:00
|
|
|
return
|
|
|
|
|
2018-08-28 14:33:18 +00:00
|
|
|
if user.get_setting("first-words", None) == None:
|
|
|
|
user.set_setting("first-words", time.time())
|
|
|
|
|
2020-02-06 16:40:46 +00:00
|
|
|
words = list(filter(None, event["message_split"]))
|
|
|
|
word_count = len(words)
|
|
|
|
|
|
|
|
date = utils.datetime.format.date_human(utils.datetime.utcnow())
|
|
|
|
user_words = self._get_words_date(event["user"], event["channel"], date)
|
|
|
|
self._set_words_date(event["user"], event["channel"], date,
|
|
|
|
user_words+word_count)
|
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
|
|
|
|
2020-03-08 22:06:14 +00:00
|
|
|
@utils.hook("received.command.words")
|
2020-02-06 16:40:46 +00:00
|
|
|
@utils.kwarg("help",
|
|
|
|
"See how many words you or the given nickname have used")
|
2020-03-08 22:06:14 +00:00
|
|
|
@utils.spec("?<nickname>ouser")
|
2016-04-05 17:43:17 +00:00
|
|
|
def words(self, event):
|
2020-03-08 22:06:14 +00:00
|
|
|
if event["spec"][0] and event["is_channel"]:
|
|
|
|
target_user = event["spec"][0]
|
|
|
|
else:
|
|
|
|
target_user = event["user"]
|
2020-02-06 16:40:46 +00:00
|
|
|
|
2020-05-12 00:16:34 +00:00
|
|
|
word_items = self._user_all(target_user)
|
|
|
|
|
|
|
|
words = {}
|
|
|
|
for channel_id, count in word_items:
|
|
|
|
if not channel_id in words:
|
|
|
|
words[channel_id] = 0
|
|
|
|
words[channel_id] += count
|
|
|
|
total = sum(words.values())
|
2019-11-15 12:13:16 +00:00
|
|
|
|
|
|
|
since = ""
|
2020-02-06 16:40:46 +00:00
|
|
|
first_words = target_user.get_setting("first-words", None)
|
2019-11-15 12:13:16 +00:00
|
|
|
if not first_words == None:
|
2020-01-30 21:12:31 +00:00
|
|
|
since = " since %s" % utils.datetime.format.date_human(
|
|
|
|
utils.datetime.timestamp(first_words))
|
2019-11-15 12:13:16 +00:00
|
|
|
|
2020-03-08 22:06:14 +00:00
|
|
|
if event["is_channel"]:
|
|
|
|
this_channel = words.get(event["target"].id, 0)
|
|
|
|
event["stdout"].write("%s has used %d words (%d in %s)%s" % (
|
|
|
|
target_user.nickname, total, this_channel, event["target"].name,
|
|
|
|
since))
|
|
|
|
else:
|
|
|
|
event["stdout"].write("%s has used %d words%s" % (
|
|
|
|
target_user.nickname, total, since))
|
2016-04-10 16:32:33 +00:00
|
|
|
|
2020-02-15 00:26:07 +00:00
|
|
|
@utils.hook("received.command.trackword")
|
|
|
|
@utils.kwarg("help", "Start tracking a word")
|
|
|
|
@utils.kwarg("permission", "track-word")
|
|
|
|
@utils.spec("!<word>wordlower")
|
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:
|
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")
|
2020-02-15 00:26:07 +00:00
|
|
|
@utils.kwarg("help",
|
|
|
|
"List which words are being tracked on the current network")
|
2018-10-18 11:53:27 +00:00
|
|
|
def tracked_words(self, event):
|
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", [])))
|
|
|
|
|
2020-01-23 14:15:26 +00:00
|
|
|
def _get_nickname(self, server, target, nickname):
|
|
|
|
nickname = server.get_user(nickname).nickname
|
|
|
|
if target.get_setting("words-prevent-highlight", True):
|
|
|
|
nickname = utils.prevent_highlight(nickname)
|
|
|
|
return nickname
|
|
|
|
|
2020-02-15 00:26:07 +00:00
|
|
|
@utils.hook("received.command.wordusers")
|
|
|
|
@utils.kwarg("help", "Show who has used a tracked word the most")
|
|
|
|
@utils.spec("!<word>wordlower")
|
2016-04-10 16:32:33 +00:00
|
|
|
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, [])
|
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,
|
2020-01-23 14:15:26 +00:00
|
|
|
convert_key=lambda nickname: self._get_nickname(
|
|
|
|
event["server"], event["target"], 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")
|
2020-02-15 00:26:07 +00:00
|
|
|
@utils.kwarg("help", "Show wordiest users")
|
2020-02-06 16:40:46 +00:00
|
|
|
@utils.spec("!-channelonly ?<start>date ?<end>date")
|
2018-10-04 04:33:15 +00:00
|
|
|
def wordiest(self, event):
|
2020-02-06 16:40:46 +00:00
|
|
|
date_str = ""
|
|
|
|
|
|
|
|
if event["spec"][0]:
|
|
|
|
date1 = utils.datetime.format.date_human(event["spec"][0])
|
|
|
|
date2 = utils.datetime.format.date_human(
|
|
|
|
event["spec"][1] or utils.datetime.utcnow())
|
|
|
|
|
|
|
|
date_str = f" ({date1} to {date2})"
|
|
|
|
words = self._channel_between_dates(event["target"], date1, date2)
|
|
|
|
else:
|
|
|
|
words = self._channel_all(event["target"])
|
|
|
|
|
2018-10-04 04:33:15 +00:00
|
|
|
user_words = {}
|
2020-02-06 16:40:46 +00:00
|
|
|
for user_id, word_count in words:
|
|
|
|
_, nickname = self.bot.database.users.by_id(user_id)
|
2020-05-12 00:20:47 +00:00
|
|
|
if not nickname in user_words:
|
|
|
|
user_words[nickname] = 0
|
|
|
|
user_words[nickname] += word_count
|
2018-10-04 04:33:15 +00:00
|
|
|
|
|
|
|
top_10 = utils.top_10(user_words,
|
2020-01-10 11:51:46 +00:00
|
|
|
convert_key=lambda nickname: self._get_nickname(
|
|
|
|
event["server"], event["target"], nickname))
|
2020-02-06 16:40:46 +00:00
|
|
|
event["stdout"].write("wordiest in %s%s: %s" %
|
|
|
|
(str(event["target"]), date_str, ", ".join(top_10)))
|