如何使discord.py机器人为每个玩家显示不同的统计信息? [英] How to get discord.py bot to show different stats for every player?

查看:85
本文介绍了如何使discord.py机器人为每个玩家显示不同的统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试过一段时间制作一个货币bot,但最终服务器上的每个人都共享相同数量的货币。

So, I tried to make a currency bot a while back, but it ended up that everyone in the server shared the same amount of currency.

我该如何使漫游器的用户各自拥有单独的帐户,而并非所有人都拥有相同的余额?

How would I make the users of a bot each have seperate accounts, and not all share the same balance?

将不胜感激!

推荐答案

您可以设置会员 s表示货币金额。我可能会使用成员ID,以便当您想关闭机器人程序时可以保存文件。

You can set up a dictionary of Members to amounts of currency. I would probably use the member ids, so that you can save the file when you want to shut off your 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.py机器人为每个玩家显示不同的统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆