refactor karma to be per-user. added data migration script for this
This commit is contained in:
parent
3ce947bb77
commit
499e83a1cb
3 changed files with 117 additions and 55 deletions
45
migration/v01.16.00-karma.py
Normal file
45
migration/v01.16.00-karma.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
# Used to migrate karma from prior to v1.16.0
|
||||||
|
# usage: $ python3 migration/v01.16.00-karma.py ~/.bitbot/bot.db
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser(description="Migrate pre-v1.16.0 karma")
|
||||||
|
parser.add_argument("database")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
import json, sqlite3
|
||||||
|
database = sqlite3.connect(args.database)
|
||||||
|
|
||||||
|
cursor = database.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
"""SELECT server_id, setting, value FROM server_settings
|
||||||
|
WHERE setting LIKE 'karma-%'""")
|
||||||
|
results = cursor.fetchall()
|
||||||
|
|
||||||
|
cursor.execute("SELECT nickname, user_id FROM users")
|
||||||
|
users = dict(cursor.fetchall())
|
||||||
|
|
||||||
|
cursor.execute("SELECT server_id, alias FROM servers")
|
||||||
|
servers = dict(cursor.fetchall())
|
||||||
|
|
||||||
|
server_users = {}
|
||||||
|
for server_id, setting, karma in results:
|
||||||
|
if not server_id in server_users:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO users (server_id, nickname) VALUES (?, ?)",
|
||||||
|
[server_id, "*karma"])
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT user_id FROM users WHERE server_id=? AND nickname=?",
|
||||||
|
[server_id, "*karma"])
|
||||||
|
server_users[server_id] = cursor.fetchone()[0]
|
||||||
|
|
||||||
|
print("[%s] Migrating '%s' (%s)" %
|
||||||
|
(servers[server_id], setting.replace("karma-", "", 1), karma))
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO user_settings VALUES (?, ?, ?)",
|
||||||
|
[server_users[server_id], setting, karma])
|
||||||
|
|
||||||
|
database.commit()
|
||||||
|
database.close()
|
||||||
|
|
||||||
|
print()
|
||||||
|
print("Migration successful!")
|
107
modules/karma.py
107
modules/karma.py
|
@ -12,8 +12,6 @@ REGEX_PARENS = re.compile(r"\(([^)]+)\)(\+\+|--)")
|
||||||
|
|
||||||
@utils.export("channelset", utils.BoolSetting("karma-pattern",
|
@utils.export("channelset", utils.BoolSetting("karma-pattern",
|
||||||
"Enable/disable parsing ++/-- karma format"))
|
"Enable/disable parsing ++/-- karma format"))
|
||||||
@utils.export("serverset", utils.BoolSetting("karma-nickname-only",
|
|
||||||
"Enable/disable karma being for nicknames only"))
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
def _karma_str(self, karma):
|
def _karma_str(self, karma):
|
||||||
karma_str = str(karma)
|
karma_str = str(karma)
|
||||||
|
@ -43,36 +41,27 @@ class Module(ModuleManager.BaseModule):
|
||||||
else:
|
else:
|
||||||
user._last_negative_karma = time.time()
|
user._last_negative_karma = time.time()
|
||||||
|
|
||||||
|
def _change_karma(self, server, sender, target, positive):
|
||||||
|
if not self._check_throttle(sender, positive):
|
||||||
|
return False, "Try again in a couple of seconds"
|
||||||
|
|
||||||
def _karma(self, server, sender, target, positive):
|
if " " in target and server.has_user(target):
|
||||||
if self._check_throttle(sender, positive):
|
target = server.get_user_nickname(target.get_id())
|
||||||
nickname_only = server.get_setting("karma-nickname-only", False)
|
else:
|
||||||
|
target = target.lower()
|
||||||
if server.irc_lower(target) == sender.name:
|
|
||||||
return False, "You cannot change your own karma"
|
|
||||||
|
|
||||||
setting = "karma-%s" % target
|
setting = "karma-%s" % target
|
||||||
setting_target = server
|
karma = sender.get_setting(setting, 0)
|
||||||
if nickname_only:
|
|
||||||
user = server.get_user(target, create=False)
|
|
||||||
if user == None:
|
|
||||||
return False, "No such user"
|
|
||||||
setting = "karma"
|
|
||||||
setting_target = user
|
|
||||||
|
|
||||||
karma = setting_target.get_setting(setting, 0)
|
|
||||||
karma += 1 if positive else -1
|
karma += 1 if positive else -1
|
||||||
|
|
||||||
if not karma == 0:
|
if karma == 0:
|
||||||
setting_target.set_setting(setting, karma)
|
sender.del_setting(setting)
|
||||||
else:
|
else:
|
||||||
setting_target.del_setting(setting)
|
sender.set_setting(setting, karma)
|
||||||
|
|
||||||
karma_str = self._karma_str(karma)
|
|
||||||
self._set_throttle(sender, positive)
|
self._set_throttle(sender, positive)
|
||||||
|
karma_str = self._karma_str(karma)
|
||||||
return True, "%s now has %s karma" % (target, karma_str)
|
return True, "%s now has %s karma" % (target, karma_str)
|
||||||
else:
|
|
||||||
return False, "Try again in a couple of seconds"
|
|
||||||
|
|
||||||
@utils.hook("command.regex", pattern=REGEX_WORD)
|
@utils.hook("command.regex", pattern=REGEX_WORD)
|
||||||
@utils.hook("command.regex", pattern=REGEX_PARENS)
|
@utils.hook("command.regex", pattern=REGEX_PARENS)
|
||||||
|
@ -81,8 +70,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
if event["target"].get_setting("karma-pattern", False):
|
if event["target"].get_setting("karma-pattern", False):
|
||||||
target = event["match"].group(1)
|
target = event["match"].group(1)
|
||||||
positive = event["match"].group(2)=="++"
|
positive = event["match"].group(2)=="++"
|
||||||
success, message = self._karma(event["server"], event["user"],
|
success, message = self._change_karma(
|
||||||
target, positive)
|
event["server"], event["user"], target, positive)
|
||||||
event["stdout" if success else "stderr"].write(message)
|
event["stdout" if success else "stderr"].write(message)
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,8 +81,8 @@ class Module(ModuleManager.BaseModule):
|
||||||
@utils.kwarg("usage", "<target>")
|
@utils.kwarg("usage", "<target>")
|
||||||
def changepoint(self, event):
|
def changepoint(self, event):
|
||||||
positive = event["command"] == "addpoint"
|
positive = event["command"] == "addpoint"
|
||||||
success, message = self._karma(event["server"], event["user"],
|
success, message = self._change_karma(
|
||||||
event["args"].strip(), positive)
|
event["server"], event["user"], event["args"].strip(), positive)
|
||||||
event["stdout" if success else "stderr"].write(message)
|
event["stdout" if success else "stderr"].write(message)
|
||||||
|
|
||||||
@utils.hook("received.command.karma")
|
@utils.hook("received.command.karma")
|
||||||
|
@ -108,26 +97,50 @@ class Module(ModuleManager.BaseModule):
|
||||||
target = event["user"].nickname
|
target = event["user"].nickname
|
||||||
target = target.strip()
|
target = target.strip()
|
||||||
|
|
||||||
if event["server"].get_setting("karma-nickname-only", False):
|
settings = dict(
|
||||||
karma = event["server"].get_user(target).get_setting("karma", 0)
|
event["server"].get_all_user_settings("karma-%s" % target))
|
||||||
else:
|
|
||||||
karma = event["server"].get_setting("karma-%s" % target, 0)
|
|
||||||
karma_str = self._karma_str(karma)
|
|
||||||
event["stdout"].write("%s has %s karma" % (target, karma_str))
|
|
||||||
|
|
||||||
@utils.hook("received.command.resetkarma", min_args=1)
|
target_lower = event["server"].irc_lower(target)
|
||||||
|
if target_lower in settings:
|
||||||
|
del settings[target_lower]
|
||||||
|
|
||||||
|
karma = self._karma_str(sum(settings.values()))
|
||||||
|
event["stdout"].write("%s has %s karma" % (target, karma))
|
||||||
|
|
||||||
|
|
||||||
|
@utils.hook("received.command.resetkarma")
|
||||||
|
@utils.kwarg("min_args", 2)
|
||||||
|
@utils.kwarg("help", "Reset a specific karma to 0")
|
||||||
|
@utils.kwarg("usage", "by|for <target>")
|
||||||
|
@utils.kwarg("permission", "resetkarma")
|
||||||
def reset_karma(self, event):
|
def reset_karma(self, event):
|
||||||
"""
|
subcommand = event["args_split"][0].lower()
|
||||||
:help: Reset a specified karma to 0
|
target = " ".join(event["args_split"][1:])
|
||||||
:usage: <target>
|
|
||||||
:permission: resetkarma
|
if subcommand == "by":
|
||||||
"""
|
target_user = event["server"].get_user(target)
|
||||||
setting = "karma-%s" % event["args_split"][0]
|
karma = target_user.find_setting(prefix="karma-")
|
||||||
karma = event["server"].get_setting(setting, 0)
|
print(target_user)
|
||||||
if karma == 0:
|
print(target_user.get_id())
|
||||||
event["stderr"].write("%s already has 0 karma" % event[
|
for setting, _ in karma:
|
||||||
"args_split"][0])
|
target_user.del_setting(setting)
|
||||||
|
|
||||||
|
if karma:
|
||||||
|
event["stdout"].write("Cleared karma by %s" %
|
||||||
|
target_user.nickname)
|
||||||
else:
|
else:
|
||||||
event["server"].del_setting(setting)
|
event["stderr"].write("No karma to clear by %s" %
|
||||||
event["stdout"].write("Reset karma for %s" % event[
|
target_user.nickname)
|
||||||
"args_split"][0])
|
elif subcommand == "for":
|
||||||
|
setting = "karma-%s" % target
|
||||||
|
karma = event["server"].get_all_user_settings(setting)
|
||||||
|
for nickname, value in karma:
|
||||||
|
user = event["server"].get_user(nickname)
|
||||||
|
user.del_setting(setting)
|
||||||
|
|
||||||
|
if karma:
|
||||||
|
event["stdout"].write("Cleared karma for %s" % target)
|
||||||
|
else:
|
||||||
|
event["stderr"].write("No karma to clearfor %s" % target)
|
||||||
|
else:
|
||||||
|
raise utils.EventError("Unknown subcommand '%s'" % subcommand)
|
||||||
|
|
|
@ -193,6 +193,10 @@ class Server(IRCObject.Object):
|
||||||
def has_user_id(self, nickname: str) -> bool:
|
def has_user_id(self, nickname: str) -> bool:
|
||||||
id = self.bot.database.users.get_id(self.id, self.irc_lower(nickname))
|
id = self.bot.database.users.get_id(self.id, self.irc_lower(nickname))
|
||||||
return not id == None
|
return not id == None
|
||||||
|
|
||||||
|
def get_user_nickname(self, user_id: int) -> str:
|
||||||
|
return self.bot.database.users.get_nickname(self.id, user_id)
|
||||||
|
|
||||||
def remove_user(self, user: IRCUser.User):
|
def remove_user(self, user: IRCUser.User):
|
||||||
del self.users[user.nickname_lower]
|
del self.users[user.nickname_lower]
|
||||||
for channel in user.channels:
|
for channel in user.channels:
|
||||||
|
|
Loading…
Reference in a new issue