【问题标题】:'int' object has no attribute 'id' in discord.py command“int”对象在 discord.py 命令中没有属性“id”
【发布时间】:2019-12-28 14:13:52
【问题描述】:

所以我有一个星期的这个问题,我真的不知道如何解决这个问题。 我正在创建一个调平系统,我可以通过命令 +addpoint 向我的成员添加点(级别),然后在不和谐中提到一个成员,然后是我想给他们的点数(正如你在代码中看到的那样)和我还想将会员的观点保存在 JSON 文件中。 我多次分享我的代码并请求解决方案,但没有人为此提供解决方案。

这是我的代码

os.chdir(r'C:\Users\USER\Desktop\Python\mclearn')

@client.event
async def on_member_join(member):
    with open('users.json', 'r') as f:
        users = json.load(f)

        #code
        await update_data(users, member)


    with open('user.json', 'w') as f:
        json.dump(users, f)

@client.command()
async def addpoint(ctx, user: discord.User, amountt):
    with open('users.json', 'r') as f:
        users = json.load(f)
        amountt = amountt.replace(" ", "")
        amountt = int(amountt)
        #code
        await update_data(users, user.id)
        await add_point(users, user.id, amountt)
        await addedpointmsg(users, user.id, message.channel)

    await ctx.send(f"{user.mention}, {str(amountt)} point daryaft kard.")

    with open('user.json', 'w') as f:
        json.dump(users, f)


async def update_data(Users, user: discord.User):
    if not user.id in users:
        users[user.id] = {}
        users[user.id]['point'] = 0

async def add_point(Users, user, point):
    users[user.id]['point'] += point

这是我无法修复的错误(我是初学者)

Ignoring exception in command addpoint:
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\USER\Desktop\Python\mclearn\bot.py", line 165, in addpoint
    await update_data(users, user.id)
  File "C:\Users\USER\Desktop\Python\mclearn\bot.py", line 176, in update_data
    if not user.id in users:
AttributeError: 'int' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'int' object has no attribute 'id'

我还导入了每个需要的模块。

请有人给我一个解决方案。谢谢

【问题讨论】:

  • 我建议修正你的语言。如果您想了解为什么仅仅分享您的代码并不总是足以获得好的答案,请仔细阅读stackoverflow.com/help/how-to-ask
  • 你的电话是await update_data(users, user.id)而不是await update_data(users, user)
  • 我猜 await update_data(users, user.id) 是对的,因为用户本身不提供任何东西,我希望用户的 id 以 JSON 格式保存他们的观点。
  • 根据函数注释完全错误

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

函数注释说你应该得到User Object类型的user参数。您将用户参数设置为 int 类型 id。 您的下一个错误可能是user.mention

我建议你仔细检查传入的函数参数user,以获得预期的User Object

【讨论】:

    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2020-12-17
    • 2021-07-27
    • 2021-01-05
    • 2021-03-06
    • 2019-08-13
    相关资源
    最近更新 更多