亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Discord.py 類定義在明確輸入時沒有得到變量

Discord.py 類定義在明確輸入時沒有得到變量

Helenr 2022-06-22 18:24:34
from discord.ext import commandsimport asyncioimport osimport randomimport reprefix = 'bz 'client = commands.Bot(command_prefix=prefix)@client.eventasync 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 = Trueclass 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我的源代碼在上面,我希望這就足夠了我得到了錯誤missing 1 required positional argument: 'amount'此錯誤顯示在:await cashmodify.addtobal(person, cash)我一直在嘗試為python 3.7制作一個帶有discord重寫的經濟機器人,我決定這次讓自己更容易將它們保留在類中,然后在需要時使用它們,但每當我在discord中運行命令時,它說我缺少一個稱為數量的必需位置參數。我試過從字符串和整數切換輸入,我回頭看了看課程,它仍然無法工作。我嘗試過的另一件事是刪除 commands.Cog,但這仍然不起作用。所以我很感激任何人能給我的幫助
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

當您創建一個類的實例并調用其方法之一時,self(代表您的類的實例)將作為參數傳遞給它。


此代碼不會創建您的類的實例,它只是創建一個類對象:


>>> cashmodify = CashModify

>>> cashmodify

<class '__main__.CashModify'>

該addtobal方法是這樣定義的addtobal(self, member, amount),而您是這樣調用它的addtobal(person, cash)。


你得到一個錯誤,因為你沒有創建類的任何實例CashModify:沒有self屬性,所以你的person和cash變量分別作為self和member參數傳遞,沒有任何東西傳遞給amount。


要解決此問題,您必須根據您的__init__方法實例化您的類(創建它的實例)并將客戶端變量傳遞給它:


cashmodify = CashModify(client)


查看完整回答
反對 回復 2022-06-22
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號