summaryrefslogtreecommitdiff
path: root/commands/economy.py
blob: 720a00d81de5ff522b304fdd5bc75050e60f088e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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")