Python 3.x 如何让discord.py机器人为每个玩家显示不同的统计数据?

Python 3.x 如何让discord.py机器人为每个玩家显示不同的统计数据?,python-3.x,discord.py,Python 3.x,Discord.py,不久前,我尝试制作一个货币机器人,但结果是服务器中的每个人共享相同数量的货币 我如何让机器人的每个用户都有单独的帐户,而不是共享相同的余额 谢谢你的帮助 您可以设置成员的字典,以确定货币金额。我可能会使用成员ID,以便您可以在关闭bot时保存文件 from discord.ext import commands import discord import json bot = commands.Bot('!') amounts = {} @bot.event async def on_re

不久前,我尝试制作一个货币机器人,但结果是服务器中的每个人共享相同数量的货币

我如何让机器人的每个用户都有单独的帐户,而不是共享相同的余额


谢谢你的帮助

您可以设置
成员
的字典,以确定货币金额。我可能会使用成员ID,以便您可以在关闭bot时保存文件

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 bot.say("You have {} in the bank".format(amounts[id]))
    else:
        await bot.say("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 bot.say("You are now registered")
        _save()
    else:
        await bot.say("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 bot.say("You do not have an account")
    elif other_id not in amounts:
        await bot.say("The other party does not have an account")
    elif amounts[primary_id] < amount:
        await bot.say("You cannot afford this transaction")
    else:
        amounts[primary_id] -= amount
        amounts[other_id] += amount
        await bot.say("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.ext导入命令
进口不和
导入json
bot=commands.bot(“!”)
金额={}
@机器人事件
_ready()上的异步定义:
全球金额
尝试:
将open('amounts.json')作为f:
amounts=json.load(f)
除FileNotFoundError外:
打印(“无法加载amounts.json”)
金额={}
@命令(pass\u context=True)
异步def余额(ctx):
id=ctx.message.author.id
如果id的金额为:
wait bot.say(“你在银行里有{}”。格式(金额[id]))
其他:
等待bot。说(“你没有账户”)
@命令(pass\u context=True)
异步def寄存器(ctx):
id=ctx.message.author.id
如果id不在金额中:
金额[id]=100
等待机器人。说(“您现在已注册”)
_保存()
其他:
等待bot。说(“你已经有一个帐户”)
@命令(pass\u context=True)
异步def传输(ctx,金额:int,其他:discord.Member):
primary_id=ctx.message.author.id
other_id=other.id
如果主id不在金额中:
等待bot。说(“你没有账户”)
elif其他不在金额中的id:
等待bot.say(“另一方没有账户”)
elif金额[主id]<金额:
等待bot.say(“您负担不起这笔交易”)
其他:
金额[主id]-=金额
金额[其他id]+=金额
等待bot.say(“交易完成”)
_保存()
def_save():
将open('amounts.json','w+')作为f:
json.dump(金额,f)
@bot.command()
异步def save():
_保存()
bot.run(“令牌”)

一些玩家字典中的数字?我该怎么做@帕特里克豪伊德克霍夫。可以用一个例子来回答吗?@PatrickHaugh Please是一个可以通过异步接口访问的数据库。对于Postgres数据库,
asyncpg
,我听说过一些好消息。@PatrickHaugh是的,但我没有数据库设置的示例代码。我问了一个问题,但没有收到任何人的答复。文件“”,第1行,在文件“/home/zorin/Desktop/den.py”中,第36行,在异步def传输中(ctx,金额:int,其他:discord.Member):名称错误:未定义名称“discord”error@asdwdwdad添加
import discord
您是否以除此bot之外的任何方式修改
amounts.json
?我脑子里唯一能想到的是,如果你在使用“重写”,你的ID将是整数,而dict中的ID将是字符串。正如所写的那样,
amounts
是短暂的
dict
,除非你手动调用
!保存
将其写入文件。重构这个函数,将功能从
save
移到其他函数中,然后在所有修改
amounts
的协同程序结束时调用它,这将是很简单的,当然,只需检查
len(amounts)