from discord.ext import commands from discord.utils import find from .utils import config from .utils import checks import re import operator class Stats: """Leaderboard/stats related commands""" def __init__(self, bot): self.bot = bot @commands.command(pass_context=True, no_pm=True) @checks.customPermsOrRole("send_messages") async def mostboops(self, ctx): """Shows the person you have 'booped' the most, as well as how many times""" boops = config.getContent('boops') members = ctx.message.server.members if not boops.get(ctx.message.author.id): await self.bot.say("You have not booped anyone {} Why the heck not...?".format(ctx.message.author.mention)) return most_boops = 0 for b_id, amt in boops.get(ctx.message.author.id).items(): member = find(lambda m: m.id == b_id, self.bot.get_all_members()) if member in members and amt > most_boops: most_boops = amt most_id = b_id member = find(lambda m: m.id == most_id, self.bot.get_all_members()) await self.bot.say("{0} you have booped {1} the most amount of times, coming in at {2} times".format( ctx.message.author.mention, member.mention, most_boops)) @commands.command(pass_context=True, no_pm=True) @checks.customPermsOrRole("send_messages") async def listboops(self, ctx): """Lists all the users you have booped and the amount of times""" members = ctx.message.server.members boops = config.getContent('boops') if boops is None or boops.get(ctx.message.author.id) is None: await self.bot.say("You have not booped anyone {} Why the heck not...?".format(ctx.message.author.mention)) return output = "You have booped:" for b_id, amt in boops.get(ctx.message.author.id).items(): member = find(lambda m: m.id == b_id, self.bot.get_all_members()) if member in members: output += "\n{0.name}: {1} times".format(member, amt) await self.bot.say("```{}```".format(output)) @commands.command(pass_context=True, no_pm=True) @checks.customPermsOrRole("send_messages") async def leaderboard(self, ctx): """Prints a leaderboard of everyone in the server's battling record""" battles = config.getContent('battle_records') server_member_ids = [member.id for member in ctx.message.server.members] server_members = {member_id:stats for member_id,stats in battles.items() if member_id in server_member_ids} sorted_members = sorted(server_members.items(), key=(operator.itemgetter(1)).get('rating')) fmt = "" count = 1 for member_id,stats in sorted_members: member = discord.utils.get(ctx.message.server.members,id=member_id) fmt += "#{}) {} - {}".format(count,member.display_name,stats.get('rating')) count += 1 await self.bot.say("```{}```".format(fmt)) def setup(bot): bot.add_cog(Stats(bot))