“open_account” is not defined error/variables not working

Hello so my issue is the code works perfectly fine within the main.py file but as soon as
I try to convert it into a cog all of my variables stop working. I’m sure this is a stupid fix but learning is learning 😀

        class Economy(commands.Cog):
    
        def __init__(self, client):
            self.client = client
    
        @commands.command(aliases = ['money', 'cash', 'bal'])
        async def balance(self, ctx):
            await open_account(ctx.author)
            user = ctx.author
            users = await get_bank_data()
    
            wallet_amt = users[str(user.id)]["wallet"]
            bank_amt = users[str(user.id)]["bank"]
    
            em = discord.Embed(title = f"{ctx.author.name}'s balance", color = discord.Color.red())
            em.add_field(name = "Wallet balance",value = wallet_amt)
            em.add_field(name = "Bank balance",value = bank_amt)
            await ctx.send(embed = em)
    
        @commands.command(aliases = ['take', 'draw',])
        async def withdraw(self, ctx, amount = None):
            await open_account(ctx.author)
    
            if amount == None:
                await ctx.send("Please enter the amount")
                return
    
            bal = await update_bank(ctx.author)
    
            amount = int(amount)
            if amount>bal[1]:
                await ctx.send("You don't have that much money!")
                return
            if amount<0:
                await ctx.send("Amount must be positive!")
                return
    
            await update_bank(ctx.author,amount)
            await update_bank(ctx.author,-1*amount, "bank")
            await ctx.send(f"You withdrew {amount} coins")
    

    def setup(client):
        client.add_cog(Economy(client))

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

After a little poking and messing with it, I wasn’t passing “self” into my paras.

All solved now but I’ll give an example in case anyone has the same issue.

@commands.command(aliases = ['money', 'cash', 'bal'])
async def balance(self,ctx):
    self.bot = self
    await self.open_account(ctx.author)
    user = ctx.author
    users = await self.get_bank_data()

    wallet_amt = users[str(user.id)]["wallet"]
    bank_amt = users[str(user.id)]["bank"]

    em = discord.Embed(title = f"{ctx.author.name}'s balance", color = discord.Color.red())
    em.add_field(name = "Wallet balance",value = wallet_amt)
    em.add_field(name = "Bank balance",value = bank_amt)
    await ctx.send(embed = em)

basically if anyone runs into the same issue remember to always use “self.” and to define it if necessary.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x