【问题标题】:Command raised an exception: AttributeError: 'str' object has no attribute 'id'命令引发异常:AttributeError: 'str' object has no attribute 'id'
【发布时间】:2021-07-17 07:34:32
【问题描述】:

我正在创建一个制裁系统,其中保存了一个随机 id,将用户的 id 作为种子,并将其保存在 json 中,如下所示,但是当激活命令时,我收到此错误:命令生成异常: AttributeError:对象“str”没有属性“id”

 @commands.command()
  async def warn(self, ctx, member: discord.Member, *reason):
    if reason == ():
        reason = "Sin razón"
        
        reason = ' '.join(reason)
        
    timestamp = datetime.datetime.now()
    member = str(member.id)
    desordenar = random.sample(member, 18)
    idsancion = ''.join(desordenar)

    user = {}
    user[f"{idsancion}"] = {"Tiempo":f"{timestamp}","Sanción":"Warn","Staff":ctx.author.id, "Razón":f"{reason}"}
          
    with open("json/sanciones/{user}.json".format(user=member.id), "w") as f:
      json.dump(user, f, indent=4)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您在这一行中将变量成员设置为字符串:

    member = str(member.id)
    
    

    所以你在这里得到错误:

    with open("json/sanciones/{user}.json".format(user=member.id), "w") as f:
          json.dump(user, f, indent=4)
    
    

    把上面改成

    with open("json/sanciones/{user}.json".format(user=member), "w") as f:
          json.dump(user, f, indent=4)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2020-04-26
      • 1970-01-01
      • 2021-09-29
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多