"open_account" 未定义 error/variables 不工作

"open_account" is not defined error/variables not working

您好,我的问题是代码在 main.py 文件中运行得非常好,但是一旦 我尝试将其转换为齿轮,所有变量都停止工作。我确定这是一个愚蠢的修复,但学习就是学习 :D

        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))

经过一番摸索和摆弄,我没有将“self”传递到我的参数中。

现在都解决了,但我会举个例子,以防有人遇到同样的问题。

@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)

基本上,如果有人遇到同样的问题,请记住始终使用“self”。并在必要时对其进行定义。