問題描述
當這段代碼運行時,它可以從 discord 中獲取用戶 ID,并將他們有 100 錢放入 json,但是一旦你重新啟動機器人,你必須再次注冊,它會在 json 文件中寫入相同的用戶 ID,認為這是一個如果不是新用戶.
When this code runs it works getting the user ID from discord and putting they have 100 money in the json, but once you restart the bot you have to register again and it writes the same user ID in the json file thinking it's a new user when it is not.
from discord.ext import commands
import discord
import json
bot = commands.Bot('!')
amounts = {}
@bot.event
async def on_ready():
global amounts
try:
with open('amounts.json') as f:
amounts = json.load(f)
except FileNotFoundError:
print("Could not load amounts.json")
amounts = {}
@bot.command(pass_context=True)
async def balance(ctx):
id = ctx.message.author.id
if id in amounts:
await ctx.send("You have {} in the bank".format(amounts[id]))
else:
await ctx.send("You do not have an account")
@bot.command(pass_context=True)
async def register(ctx):
id = ctx.message.author.id
if id not in amounts:
amounts[id] = 100
await ctx.send("You are now registered")
_save()
else:
await ctx.send("You already have an account")
@bot.command(pass_context=True)
async def transfer(ctx, amount: int, other: discord.Member):
primary_id = ctx.message.author.id
other_id = other.id
if primary_id not in amounts:
await ctx.send("You do not have an account")
elif other_id not in amounts:
await ctx.send("The other party does not have an account")
elif amounts[primary_id] < amount:
await ctx.send("You cannot afford this transaction")
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
await ctx.send("Transaction complete")
_save()
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
@bot.command()
async def save():
_save()
bot.run("Token")
機器人關閉并重新打開并注冊兩次后的 JSON(假用戶 ID):
JSON after the bot is turned off and back on and registered twice (fake user IDs):
{"56789045678956789": 100, "56789045678956789": 100}
即使在機器人關閉并重新打開后也需要它能夠識別用戶 ID.
Need it to be able to recognize the user IDs even after the bot is turned off and back on.
推薦答案
發(fā)生這種情況是因為 JSON 對象總是有鍵"的字符串.所以 json.dump
將整數(shù)鍵轉(zhuǎn)換為字符串.您可以通過在使用用戶 ID 之前將其轉(zhuǎn)換為字符串來執(zhí)行相同的操作.
This is happening because JSON objects always have strings for the "keys". So json.dump
converts the integer keys to strings. You can do the same by converting the user ids to strings before you use them.
from discord.ext import commands
import discord
import json
bot = commands.Bot('!')
amounts = {}
@bot.event
async def on_ready():
global amounts
try:
with open('amounts.json') as f:
amounts = json.load(f)
except FileNotFoundError:
print("Could not load amounts.json")
amounts = {}
@bot.command(pass_context=True)
async def balance(ctx):
id = str(ctx.message.author.id)
if id in amounts:
await ctx.send("You have {} in the bank".format(amounts[id]))
else:
await ctx.send("You do not have an account")
@bot.command(pass_context=True)
async def register(ctx):
id = str(ctx.message.author.id)
if id not in amounts:
amounts[id] = 100
await ctx.send("You are now registered")
_save()
else:
await ctx.send("You already have an account")
@bot.command(pass_context=True)
async def transfer(ctx, amount: int, other: discord.Member):
primary_id = str(ctx.message.author.id)
other_id = str(other.id)
if primary_id not in amounts:
await ctx.send("You do not have an account")
elif other_id not in amounts:
await ctx.send("The other party does not have an account")
elif amounts[primary_id] < amount:
await ctx.send("You cannot afford this transaction")
else:
amounts[primary_id] -= amount
amounts[other_id] += amount
await ctx.send("Transaction complete")
_save()
def _save():
with open('amounts.json', 'w+') as f:
json.dump(amounts, f)
@bot.command()
async def save():
_save()
bot.run("Token")
這篇關于Discord money bot 將用戶 ID 保存在 json 文件中.當 Bot 重新啟動時,它會為每個人創(chuàng)建一個新的(但相同的)ID的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!