当明确输入Discord.py类定义时未获取变量

时间:2019-10-14 22:20:40

标签: python discord.py

from discord.ext import commands
import asyncio
import os
import random
import re

prefix = 'bz '
client = commands.Bot(command_prefix=prefix)




@client.event
async def on_ready():
    print('Logged in as: ')
    print(client.user.name)
    print(client.user.id)
    print('---------------')
    await client.change_presence(activity=discord.Game(name='bz help'))


class DataCreate(commands.Cog):
    def __int__(self, client):
        self.client = client
    async def createdatacash(self, member):
        path = 'bal_' + member + '.txt'
        print('createddatacash')
        if not os.path.isfile(path):
            file = open(path, 'w+')
            file.write('0')
            file.close()
            self.data = True
    async def writefile(self, member, inputthing):
        path = 'bal_' + member + '.txt'
        if not os.path.isfile(path):
            await DataCreate.createdatacash(member)
        file = open(path, 'w+')
        file.write(str(inputthing))
        self.data = True





class GetCash(commands.Cog):
    def __init__(self, client):
        self.client = client
    async def view_cash(self, member):
        path = 'bal_' + member + '.txt'
        if not os.path.isfile(path):
            datacreate = DataCreate
            await datacreate.createdatacash(member)
        file = open(path, 'r')
        output= file.read()
        file.close()
        self.data = output


class CashModify(commands.Cog):
    def __init__(self, client):
        self.client = client

    async def addtobal(self, member, amount):
        path = 'bal_' + member + '.txt'
        if not os.path.isfile(path):
            datacreate = DataCreate
            await datacreate.createdatacash(member)
        getcash = GetCash
        bal_amount = getcash.view_cash(member)
        output = int(bal_amount) + int(amount)
        datacreate = DataCreate
        await datacreate.writefile(member, output)





@client.command(pass_context=True)
async def daily(ctx):
    numlist = []
    x=99
    person = str(ctx.message.author.id)
    for x in range(99,600):
        x+=1
        numlist.append(x)
    secure_random = random.SystemRandom()
    cash = secure_random.choice(numlist)
    print(str(cash))
    cashmodify = CashModify
    print('ran')
    await cashmodify.addtobal(person, cash)
    getcash = GetCash
    channel = client.get_channel(ctx.message.channel.id)
    channel.send("You gained " + str(cash) + ' credits, spend wisely!")




我的源代码在上面,我希望这足够了

出现错误missing 1 required positional argument: 'amount'

此错误显示在:await cashmodify.addtobal(person, cash)

我一直在尝试用python 3.7编写一个带有不和谐重写的经济机器人,我决定这次让自己更容易的是将它们保留在类中,然后在需要时使用它们,但是只要我运行命令不一致,它表示我缺少一个称为“金额”的必需位置参数。我尝试过从字符串和整数切换输入,但我回头看了一下该类,它仍然无法正常工作。

我尝试过的另一件事是删除命令。Cog,但这仍然行不通。 因此,我很感谢任何人都能给我的帮助

1 个答案:

答案 0 :(得分:0)

创建类的实例并调用其方法之一时,self(代表类的实例)将作为参数传递给它。

此代码不会创建您的类的实例,而只是创建一个类对象:

>>> cashmodify = CashModify
>>> cashmodify
<class '__main__.CashModify'>

addtobal方法的定义与此addtobal(self, member, amount)相似,而您正在像这样addtobal(person, cash)对其进行调用。

由于未创建类CashModify的任何实例而导致错误:没有self属性,因此您的personcash变量被传递为selfmember自变量,什么也不会传递给amount

要解决此问题,您必须根据您的__init__方法实例化您的类(创建它的实例),并将其传递给客户变量:

cashmodify = CashModify(client)