Changed find_all_by_setting to not return setting, added !richest

This commit is contained in:
jesopo 2018-08-09 13:49:44 +01:00
parent 3e8f10dff5
commit 213bf999b9
3 changed files with 17 additions and 3 deletions

View file

@ -132,12 +132,12 @@ class UserSettings(Table):
return default
def find_all_by_setting(self, server_id, setting, default=[]):
values = self.database.execute_fetchall(
"""SELECT nickname, setting, value FROM user_settings WHERE
"""SELECT nickname, value FROM user_settings WHERE
server_id=? AND setting=?""",
[server_id, setting])
if values:
for i, value in enumerate(values):
values[i] = value[0], value[1], json.loads(value[2])
values[i] = value[0], json.loads(value[1])
return values
return default
def find(self, server_id, nickname, pattern, default=[]):

View file

@ -9,6 +9,8 @@ class Module(object):
def __init__(self, bot):
bot.events.on("received.command.coins").hook(self.coins,
help="Show how many coins you have")
bot.events.on("received.command.richest").hook(
self.richest, help="Show the top 10 richest users")
bot.events.on("received.command.redeemcoins").hook(
self.redeem_coins, help="Redeem free coins")
bot.events.on("received.command.flip").hook(self.flip,
@ -24,6 +26,18 @@ class Module(object):
event["user"].nickname, coins,
"" if coins == 1 else "s"))
def richest(self, event):
all_coins = event["server"].get_all_user_settings("coins", [])
items = [(coin[0], coin[1]) for coin in all_coins]
all_coins = dict(items)
top_10 = sorted(all_coins.keys())
top_10 = sorted(top_10, key=all_coins.get, reverse=True)[:10]
top_10 = ", ".join("%s (%d)" % (event["server"].get_user(
nickname).nickname, all_coins[nickname]
) for nickname in top_10)
event["stdout"].write("Richest users: %s" % top_10)
def redeem_coins(self, event):
user_coins = event["user"].get_setting("coins", 0)
if user_coins == 0:

View file

@ -75,7 +75,7 @@ class Module(object):
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]
items = [(word_user[0], word_user[1]) for word_user in word_users]
word_users = dict(items)
top_10 = sorted(word_users.keys())