• <tfoot id='PDvfu'></tfoot>
    <legend id='PDvfu'><style id='PDvfu'><dir id='PDvfu'><q id='PDvfu'></q></dir></style></legend>
    <i id='PDvfu'><tr id='PDvfu'><dt id='PDvfu'><q id='PDvfu'><span id='PDvfu'><b id='PDvfu'><form id='PDvfu'><ins id='PDvfu'></ins><ul id='PDvfu'></ul><sub id='PDvfu'></sub></form><legend id='PDvfu'></legend><bdo id='PDvfu'><pre id='PDvfu'><center id='PDvfu'></center></pre></bdo></b><th id='PDvfu'></th></span></q></dt></tr></i><div id='PDvfu'><tfoot id='PDvfu'></tfoot><dl id='PDvfu'><fieldset id='PDvfu'></fieldset></dl></div>

      <small id='PDvfu'></small><noframes id='PDvfu'>

        • <bdo id='PDvfu'></bdo><ul id='PDvfu'></ul>

        Discord money bot 将用户 ID 保存在 json 文件中.当 B

        时间:2023-10-11
          <legend id='ftzhI'><style id='ftzhI'><dir id='ftzhI'><q id='ftzhI'></q></dir></style></legend>
          1. <i id='ftzhI'><tr id='ftzhI'><dt id='ftzhI'><q id='ftzhI'><span id='ftzhI'><b id='ftzhI'><form id='ftzhI'><ins id='ftzhI'></ins><ul id='ftzhI'></ul><sub id='ftzhI'></sub></form><legend id='ftzhI'></legend><bdo id='ftzhI'><pre id='ftzhI'><center id='ftzhI'></center></pre></bdo></b><th id='ftzhI'></th></span></q></dt></tr></i><div id='ftzhI'><tfoot id='ftzhI'></tfoot><dl id='ftzhI'><fieldset id='ftzhI'></fieldset></dl></div>

                <small id='ftzhI'></small><noframes id='ftzhI'>

                • <bdo id='ftzhI'></bdo><ul id='ftzhI'></ul>
                  <tfoot id='ftzhI'></tfoot>

                    <tbody id='ftzhI'></tbody>
                • 本文介绍了Discord money bot 将用户 ID 保存在 json 文件中.当 Bot 重新启动时,它会为每个人创建一个新的(但相同的)ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当这段代码运行时,它可以从 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.

                  推荐答案

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

                  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 重新启动时,它会为每个人创建一个新的(但相同的)ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python - 将频道添加到类别 下一篇:Python message.content 不和谐机器人

                  相关文章

                  最新文章

                • <i id='VmG5f'><tr id='VmG5f'><dt id='VmG5f'><q id='VmG5f'><span id='VmG5f'><b id='VmG5f'><form id='VmG5f'><ins id='VmG5f'></ins><ul id='VmG5f'></ul><sub id='VmG5f'></sub></form><legend id='VmG5f'></legend><bdo id='VmG5f'><pre id='VmG5f'><center id='VmG5f'></center></pre></bdo></b><th id='VmG5f'></th></span></q></dt></tr></i><div id='VmG5f'><tfoot id='VmG5f'></tfoot><dl id='VmG5f'><fieldset id='VmG5f'></fieldset></dl></div>

                    <bdo id='VmG5f'></bdo><ul id='VmG5f'></ul>
                  <legend id='VmG5f'><style id='VmG5f'><dir id='VmG5f'><q id='VmG5f'></q></dir></style></legend>

                  1. <tfoot id='VmG5f'></tfoot>

                      <small id='VmG5f'></small><noframes id='VmG5f'>