From 4099e4de398321cc36161008b96a27cdc0e993d3 Mon Sep 17 00:00:00 2001 From: khuxkm fbexl Date: Tue, 17 Nov 2020 02:28:44 +0000 Subject: [PATCH] Reimplement highscore command with cosmic stuff --- commands/wordcount.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/commands/wordcount.py b/commands/wordcount.py index 7a2246e..4c2375a 100644 --- a/commands/wordcount.py +++ b/commands/wordcount.py @@ -1,8 +1,44 @@ -import plugin, subprocess +import plugin, subprocess, csv, io unhighlight_nick = lambda nick: "_{!s}_".format(nick) +def safeint(n,d=0): + try: + return int(n) + except: + return d + +def readcsv(s): + f = io.StringIO() + f.write(s) + f.seek(0) + ret = list(csv.reader(f)) + f.close() + return ret + @plugin.command("wordcount","") def wordcount(bot,channel,nick,user): output = subprocess.check_output(["/usr/local/bin/wordcount",user]).decode("utf-8").strip() bot.say(channel,nick+": "+unhighlight_nick(user)+" has written "+output+" words on cosmic.voyage") + +plugin.alias("wrote","wordcount") + +@plugin.command("highscore","[count]") +def highscore(bot,channel,nick,*count): + if count: + count = safeint(count[0],5) + else: + count = 5 + if count>10: + bot.say(channel,nick+": No more than 10 at once!") + return + if count<1: + bot.say(channel,nick+": Very funny.") + return + output = subprocess.check_output(["/usr/local/bin/scores"]).decode("utf-8").strip() + users = readcsv(output) + users = users[:count] + out = "" + for user in users: + out+="{} ({:,}), ".format(unhighlight_nick(user[0]),int(user[1])) + bot.say(channel,nick+": Top {} user{}: ".format(count,"s" if count!=1 else "")+out[:-2])