from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext from base import config from models.user import User import datetime import random import yaml class Economy(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="daily", description="Get some coins.", guild_ids=[config["discord_server_id"]], ) async def daily(self, ctx: SlashContext): user = User.get(User.discord_id == ctx.author_id) time_now = datetime.datetime.utcnow() day_ago = time_now - datetime.timedelta(hours=24) if user.daily_gained_time is None: user.daily_gained_time = day_ago remaining = user.daily_gained_time - day_ago print( f"now: {time_now}, day: {day_ago}, user: {user.exp_gained_time}, remaining: {remaining}" ) if user.daily_gained_time is None or user.daily_gained_time <= day_ago: num_coins = random.randrange(50, 100) user.daily_gained_time = time_now user.add_coins(num_coins) user.save() await ctx.send( content=f"Here you go {user.display_name}, have {num_coins} {config['currency']}" ) else: await ctx.send( content=f"No {config['currency']} for you {user.display_name}, come back in {remaining.seconds // 3600} hours, {(remaining.seconds % 3600) // 60 } minutes." ) @cog_ext.cog_slash( name="coins", description="Check your coins.", guild_ids=[config["discord_server_id"]], ) async def coins(self, ctx: SlashContext): user = User.get(User.discord_id == ctx.author_id) await ctx.send( content=f"{user.display_name}, you have {user.currency} {config['currency']}" ) @cog_ext.cog_slash( name="leaderboard", description="Show the currency leaderboard", guild_ids=[config["discord_server_id"]], ) async def leaderboard(self, ctx: SlashContext): users = User.select().order_by(User.currency.desc()).limit(10) rank_text = "" for user in users: rank_text += f"{user.display_name}: {user.currency} {config['currency']} \n" await ctx.send(content=rank_text) def setup(bot): bot.add_cog(Economy(bot)) print("Loaded Economy")