From 0e1ba152743c5cdf239cdc48cf0a5405786b8e35 Mon Sep 17 00:00:00 2001 From: snipe <72265661+notsniped@users.noreply.github.com> Date: Mon, 6 Mar 2023 05:59:38 +0000 Subject: [PATCH] Move all IsoCoin commands from main file to a cog --- cogs/isocoin.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 32 ++------------------------- 2 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 cogs/isocoin.py diff --git a/cogs/isocoin.py b/cogs/isocoin.py new file mode 100644 index 00000000..2baef25e --- /dev/null +++ b/cogs/isocoin.py @@ -0,0 +1,58 @@ +"""The isobot cog file for the IsoCoin system.""" + +# Imports +import random +import json +import discord +from discord import ApplicationContext, SlashCommandGroup +from discord.ext import commands + +# Variables +with open("database/isotokens.json", 'r', encoding="utf-8") as f: isocoins = json.load(f) + +def save(): + with open("database/isotokens.json", 'w+', encoding="utf-8") as f: json.dump(isocoins, f) + +# Functions +def create_isocoin_key(user_id: int) -> int: + """Creates a new isocoin key and value for the specified user.\n\nReturns `0` if successful, returns `1` if user is already cached.""" + if str(user_id) not in isocoins: + isocoins[str(user_id)] = 0 + return 0 + else: return 1 + +# Commands +class IsoCoin(commands.Cog): + def __init__(self, bot): + self.bot = bot + + isocoin_system = SlashCommandGroup("isocoin", "Commands related to the IsoCoin rewards system.") + + @isocoin_system.command( + name="balance", + description="See your IsoCoin balances" + ) + async def isocoin_balance(self, ctx: ApplicationContext): + localembed = discord.Embed(description=f"You currently have **{isocoins[str(ctx.author.id)]}** IsoCoins.") + await ctx.respond(embed=localembed) + + @isocoin_system.command( + name="daily", + description="Collect your daily reward of IsoCoins" + ) + @commands.cooldown(1, 86400, commands.BucketType.user) + async def isocoin_daily(self, ctx: ApplicationContext): + isocoins_reward = random.randint(2500, 5000) + isocoins[str(ctx.author.id)] += isocoins_reward + save() + await ctx.respond(f"You have earned {isocoins_reward} IsoCoins from this daily. Come back in 24 hours for the next one!") + + @isocoin_system.command( + name="shop", + description="See all the items that you can buy using your IsoCoins." + ) + async def isocoin_shop(self, ctx: ApplicationContext): + await ctx.respond("IsoCoin shop is coming soon! Check back later for new items.") + +# Cog Initialization +def setup(bot): bot.add_cog(IsoCoin(bot)) diff --git a/main.py b/main.py index 057a482b..28a00c9e 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,7 @@ from discord.ext import commands from discord.ext.commands import * from cogs.economy import get_wallet, get_bank, new_bank, new_wallet +from cogs.isocoin import create_isocoin_key # Slash option types: # Just use variable types to define option types. @@ -136,7 +137,7 @@ async def on_ready(): async def on_message(ctx): new_wallet(ctx.author.id) new_bank(ctx.author.id) - if str(ctx.author.id) not in isocoins: isocoins[str(ctx.author.id)] = 0 + create_isocoin_key(ctx.author.id) if str(ctx.guild.id) not in warnings: warnings[str(ctx.guild.id)] = {} if str(ctx.author.id) not in warnings[str(ctx.guild.id)]: warnings[str(ctx.guild.id)][str(ctx.author.id)] = [] if str(ctx.author.id) not in items: items[str(ctx.author.id)] = {} @@ -301,35 +302,6 @@ async def reload(ctx: ApplicationContext, cog: str): await ctx.respond(embed=discord.Embed(description=f"{cog} cog successfully reloaded.", color=discord.Color.green())) except: await ctx.respond(embed=discord.Embed(description=f"{cog} cog not found.", color=discord.Color.red())) -# IsoCoins commands -isocoin_system = client.create_group("isocoin", "Commands related to the IsoCoin rewards system.") - -isocoin_system.command( - name="balance", - description="See your IsoCoin balances" -) -async def isocoin_balance(ctx: ApplicationContext): - localembed = discord.Embed(description=f"You currently have **{isocoins[str(ctx.author.id)]}** IsoCoins.") - await ctx.respond(embed=localembed) - -isocoin_system.command( - name="daily", - description="Collect your daily reward of IsoCoins" -) -@commands.cooldown(1, 86400, commands.BucketType.user) -async def isocoin_daily(ctx: ApplicationContext): - isocoins_reward = random.randint(2500, 5000) - isocoins[str(ctx.author.id)] += isocoins_reward - save() - await ctx.respond(f"You have earned {isocoins_reward} IsoCoins from this daily. Come back in 24 hours for the next one!") - -isocoin_system.command( - name="shop", - description="See all the items that you can buy using your IsoCoins." -) -async def isocoin_shop(ctx: ApplicationContext): - await ctx.respond("IsoCoin shop is coming soon! Check back later for new items.") - # Initialization active_cogs = [ "economy",