gpt4 book ai didi

python - Discord 金钱机器人将用户 ID 保存在 json 文件中。当 Not 重新启动时,它会为每个人创建一个新的(但相同的)ID

转载 作者:行者123 更新时间:2023-12-01 08:07:07 24 4
gpt4 key购买 nike

当这段代码运行时,它可以从discord中获取用户ID,并将他们有100 block 钱放入json中,但是一旦你重新启动机器人,你就必须再次注册,它会在json文件中写入相同的用户ID,认为这是一个新用户,当它不是时。

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

{"56789045678956789": 100, "56789045678956789": 100}

即使在机器人关闭并重新打开后,也需要它能够识别用户 ID。

最佳答案

发生这种情况是因为 JSON 对象始终具有“键”字符串。因此 json.dump 将整数键转换为字符串。您可以通过在使用用户 ID 之前将其转换为字符串来执行相同的操作。

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

关于python - Discord 金钱机器人将用户 ID 保存在 json 文件中。当 Not 重新启动时,它会为每个人创建一个新的(但相同的)ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com