async def levels(self, ctx): ranks = open_jfile('ranks.json') l = {} for user_id in ranks.keys(): username = self.bot.get_user(int(user_id)) l.update({username: [user_id, ranks[user_id]['rank']]}) l = {k: v for k, v in sorted(l.items(), key=lambda item: item[1][1])} embed = discord.Embed(title='Top:', description='Global Rank', color=0xff004b, timestamp=datetime.utcnow()) embed_list = [] n = 0 for username in l.keys(): if l[username][1] < 1: inline = False else: inline = True embed.add_field( name=f'Rank #{l[username][1]}', value=f"{username} (Lv {ranks[l[username][0]]['level']})", inline=inline) n += 1 if n % 25 == 0: embed_list.append(embed) embed = discord.Embed(title='Top:', description='Global Rank', color=0xff004b, timestamp=datetime.utcnow()) paginator = BotEmbedPaginator(ctx, embed_list) await paginator.run()
def gain_xp(id): def existing_profile(id, ranks): def get_current_rank(id, ranks): def total_xp(lv, current): total = current while lv != 0: previous_level_xp = round( (1000 + 1.125**((lv - 1) / 12.5) - 1 - 1.28**( (lv - 1) / 100) * (lv - 1) / 1000) * 1000) total += previous_level_xp lv -= 1 return total r = {} for i in ranks.keys(): total = total_xp(ranks[i]['level'], ranks[i]['current_xp']) r.update({f'{i}': total}) r = { key: rank for rank, key in enumerate( sorted(r, key=r.get, reverse=True), 1) } return r[id] rank_up = False level = ranks[id]['level'] if level < 500: ranks[id]['current_xp'] += 5000 next_level_xp = round( (1000 + 1.125**(level / 12.5) - 1 - 1.28**(level / 100) * level / 1000) * 1000) if ranks[id]['current_xp'] >= next_level_xp: ranks[id]['current_xp'] -= next_level_xp ranks[id]['level'] += 1 rank_up = True for i in ranks.keys(): ranks[i]['rank'] = get_current_rank(i, ranks) - 2 return ranks, rank_up def new_profile(ranks): y = {"level": 0, "rank": len(ranks) - 1, "current_xp": 5000} x = {f'{id}': y} ranks.update(x) return ranks ranks = open_jfile('ranks.json') id = str(id) rank_up = False if id in ranks: ranks, rank_up = existing_profile(id, ranks) else: ranks = new_profile(ranks) write_json(ranks, 'ranks.json', 3) return rank_up, ranks[id]['level']
async def rank(self, ctx, avamember: discord.Member = None): def next_level_xp(level): if level >= 500: xp = 0 else: xp = round((1000 + 1.125**(level / 12.5) - 1 - 1.28**(level / 100) * level / 1000) * 1000) return xp def previous_level_xp(level): if level == 0: xp = 0 else: xp = round((1000 + 1.125**((level - 1) / 12.5) - 1 - 1.28**( (level - 1) / 100) * (level - 1) / 1000) * 1000) return xp ranks = open_jfile('ranks.json') if avamember == None: info = ranks[f'{ctx.author.id}'] username = ctx.author.name avatar = ctx.author.avatar_url else: info = ranks[str(avamember.id)] username = avamember avatar = avamember.avatar_url boosting = False if int(info['rank']) == -1: color = '861D9E' elif int(info['rank']) == 0: color = 'FF004B' else: color = 'FCBA41' gen_card = await vac_api.rank_card( username=username, avatar=avatar, level=int(info['level']), rank=int(info['rank']), current_xp=int(info['current_xp']), next_level_xp=next_level_xp(int(info['level'])), previous_level_xp=previous_level_xp(int(info['level'])), #custom_bg = card_pic, xp_color=color, is_boosting=boosting) rank_bytes = await gen_card.read() #await ctx.send(f"{username}'s rank in {ctx.guild.name}", #file = discord.File(rank_bytes, f'{username}_rank.png') #) await ctx.send(f"{username}'s global rank", file=discord.File(rank_bytes, f'{username}_rank.png'))
#L_Maid(Discord_Bot).py #Lilin莉莉(路姐的傲嬌女僕 (Discord Bot) #Created on 5/7/2020 by Lucifer from discord.ext import commands from cmds.jfile import open_jfile import os bot = commands.Bot(command_prefix = 'm!', description = '') jdata = open_jfile('settings.json') TOKEN = jdata['TOKEN'] bot.remove_command('help') for filename in os.listdir('./cmds'): if filename.endswith('.py') and filename != 'jfile.py': bot.load_extension(f'cmds.{filename[:-3]}') @bot.event async def on_ready(): print('Bot is alive.') if __name__ == '__main__': bot.run(TOKEN)