当前位置: 首页 > 面试题库 >

Discord money机器人将用户ID保留在json文件中。Bot重新启动时,会为每个人创建一个新的(但相同的)ID

凌俊材
2023-03-14
问题内容

运行此代码时,它可以从不一致的用户ID中获取用户ID,并将其放入json中,其中有100美元。但是,一旦您重新启动bot,就必须重新注册,并在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")


 类似资料:
  • 我有50人,而这50个人在5个团队中(10人可能不在一个团队中)。我想在这50个人中生成7个小组,这样每个小组中没有两个人在同一个团队中。 这些组不必具有相同数量的人。 这些组可以有两个人在同一团队中,但必须积极地最小化 这些组的大小应大致相同 理想情况下,我想在python中解决这个问题,但我并不特别关心我用哪种语言实现它。

  • 问题内容: 历史上一直存在“ 如何在没有主要方法的情况下启动应用程序 ”的问题,大多数都沿用“ 您不能 ”的方式陈述。我注意到现在如何可以通过扩展来启动JavaFX应用程序。我一直在使用与相似的方法,但是在这种情况下,我始终将主要方法视为样板代码。因此,我再次开始寻找答案。 我找到了这个答案,将我们链接到可以学习如何创建自己的JVM启动器的地方,但不幸的是,该文章不再存在。我花了很长时间寻找可能暗

  • Activity1还实现了我正在调用的Activity1的PaymentResponseListener和onResume PaymentResponse.ValidatePaymentResponse(getIntent(),Activity1.this);//此函数来自他们的sdk,并且是只读的。 现在,当Activity1启动ATH Movil应用程序并获得响应时。它再次创建Activity

  • 所以,我最近被告知,仅仅将不和谐机器人令牌存储在顶部的变量中是不好的做法,一个. env文件会更好。有人能向我解释一下如何创建带有令牌的. env文件,并将其导入到我的bot.py文件中吗?

  • 我通常通过安装pycharm,但我犯的错误是在下载文件夹中包含pycharm目录时执行。 我后来将目录移动到。这导致曾经运行的unity启动器(最初安装PyCharm时添加的)每次尝试使用时都会失败(如预期的那样)。 如何从Unity启动器中重新添加作为一个可使用的应用程序?我知道文件的位置。我试图将其添加到,但这并不能改变任何事情。 我仍然可以通过启动pycharm。但这很乏味。 我已尝试使用以

  • 问题内容: 我需要能够使用读取同一键的多个值。配置文件示例: 使用“标准”将有一个带有值的键。但是我需要解析器读取两个值。 在重复键输入之后,我创建了以下示例代码: 第一部分(带有)读入我们的“常规”配置文件,仅保留其值(覆盖/删除其他值),然后得到以下预期输出: 第二部分()使用我的方法将多个值附加到列表中,但输出是 如何摆脱重复的价值观?我期望的输出如下: 要么 (我不在乎是否每个值都在列表中