cosmicbot/commands/wordcount.py

45 lines
1.1 KiB
Python

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","<username>")
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])