python - 命令引发异常 : AttributeError: 'str' object has no attribute 'id'

标签 python discord discord.py

我正在创建一个制裁系统,其中保存一个随机 ID,将用户的 ID 作为种子,并将其保存在 json 中,如下所示,但是在激活命令时,我收到此错误:命令生成异常:AttributeError: object “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)

最佳答案

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

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)

关于python - 命令引发异常 : AttributeError: 'str' object has no attribute 'id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67224091/

相关文章:

python - 使用 python 从列中获取数据,而不使用 pyexcel 或其他库

python - Foreach 循环使用 BeautifulSoup/Mechanize/Python 获取下一页链接

javascript - Discord 机器人编辑消息太慢

python - 为什么我会收到 discord.py AttributeError : 'str' object has no attribute 'trigger_typing'

python - 有没有办法让我们在discord.py中一次获取多个命令的剩余冷却时间?

python - 为什么用具有相同名称但不同符号名称的变量替换表达式中的变量不起作用

node.js - DiscordAPIError : Invalid Form Body embeds[0]. 描述 : This field is required in discord. js v13

python - 显示用户级别系统 Discord.py

python - discord.py:我想从消息中获取一组特定的数字,但我被卡住了

python - 如何在pyqt(QGIS)中捕捉QDockWidget的KeyPressedEvent