Anthony
Anthony

Reputation: 83

Command raised an exception: AttributeError: 'str' object has no attribute 'id'

I am creating a sanctions system in which a random id is saved taking the user's id as a seed and that saves it in a json as you can see below but when activating the command I get this error: Command generated an exception: AttributeError: object 'str' has no attribute '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)

Upvotes: 4

Views: 2275

Answers (1)

JordanKobeWade
JordanKobeWade

Reputation: 348

You set variable member to a string in this line:

member = str(member.id)

so you are getting the error here:

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

change the above to

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

Upvotes: 3

Related Questions