from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext from base import config from models.user import User from models.level import Level import random import yaml class Rank(Cog): def __init__(self, bot: Bot): self.bot = bot with open("config.yaml", "r") as yamlfile: config = yaml.load(yamlfile, Loader=yaml.CLoader) print("Read config successful") self.config = config print(config["discord_server_id"]) @cog_ext.cog_slash( name="rank", description="Show your rank.", guild_ids=[config["discord_server_id"]], ) async def rank(self, ctx: SlashContext): user = User.get(User.discord_id == ctx.author_id) next_level = Level.get(Level.level == user.level + 1) await ctx.send( content=f"Hey {user.display_name}, you are level {user.level}. You're at {user.exp}/{next_level.exp_required}" ) @cog_ext.cog_slash( name="ranks", description="Show the rank leaderboard", guild_ids=[config["discord_server_id"]], ) async def ranks(self, ctx: SlashContext): users = User.select().order_by(User.exp.desc()).limit(10) rank_text = "" for user in users: next_level = Level.get(user.level + 1) rank_text += ( f"{user.display_name}: Level {user.level}, {user.exp}/{next_level.exp_required} \n" ) await ctx.send(content=rank_text) def setup(bot): bot.add_cog(Rank(bot)) print("Loaded Rank")