第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Discord.py 代碼未正確讀取/寫入 JSON 文件

Discord.py 代碼未正確讀取/寫入 JSON 文件

蠱毒傳說 2022-12-14 20:39:33
我檢查用戶是否在表中的代碼:@client.eventasync def on_message(ctx):    id = ctx.author.id    with open('coins.json') as coins:        coinData = json.load(coins)    with open('shop.json') as shop:        shopData = json.load(shop)    await client.process_commands(ctx)    if id in coinData:        print('exists')    else:        coinData["players"][id] = 0        with open('coins.json', 'w') as coins:            json.dump(coinData, coins)它正在讀取的 JSON 文件:{"players": {"325616103143505932": 0}}當(dāng)有人發(fā)送消息時(shí)會(huì)發(fā)生什么:{"players": {"325616103143505932": 0, "325616103143505932": 0}}而且它不會(huì)在控制臺(tái)中打印“存在”,無論該人發(fā)送了多少消息,但它只添加了兩次鍵值對(duì)。
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

在 python 中,字符串值和整數(shù)值是不同的。


>>> a = 1

>>> b = '1'

>>> a == b

False

因此,您應(yīng)該將現(xiàn)有的 json 文件轉(zhuǎn)換為使用整數(shù) ID(通過刪除引號(hào))或使用str()將整數(shù) ID 轉(zhuǎn)換為字符串。


這里使用的是字符串轉(zhuǎn)換(對(duì)于整數(shù),您無需更改任何內(nèi)容,只需更新您的文件):


@client.event

async def on_message(ctx):

    id = str(ctx.author.id) # this line

    with open('coins.json') as coins:

        coinData = json.load(coins)


    with open('shop.json') as shop:

        shopData = json.load(shop)


    await client.process_commands(ctx)


    if id in coinData:

        print('exists')

    else:

        coinData["players"][id] = 0

        with open('coins.json', 'w') as coins:

            json.dump(coinData, coins)


查看完整回答
反對(duì) 回復(fù) 2022-12-14
  • 1 回答
  • 0 關(guān)注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)