from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext from discord import Embed 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 = 10 + user.level * 2 user.daily_gained_time = time_now user.add_coins(num_coins) user.save() embed = Embed( description=f"Here you go {ctx.author.mention}, have your daily coins", colour=0x00FF00, ) embed.add_field(name="Gained:", value=f"{num_coins} {config['currency']}") else: embed = Embed( description=f"No {config['currency']} for you {user.display_name}, come back in {remaining.seconds // 3600} hours, {(remaining.seconds % 3600) // 60 } minutes.", colour=0xFF8800, ) await ctx.send(embed=embed) @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) embed = Embed( description=f"{ctx.author.mention}, here is your current balance:", colour=0x0000FF, ) embed.add_field(name="Currency:", value=f"{user.currency} {config['currency']}") await ctx.send(embed=embed) @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) embed = Embed( title="Currency Leaderboard", colour=0x0000FF, ) i = 1 for user in users: embed.add_field( name=f"#{i} {user.display_name}", value=f"{user.currency} {config['currency']}" ) i += 1 await ctx.send(embed=embed) def setup(bot): bot.add_cog(Economy(bot)) print("Loaded Economy")