'int' 对象在 discord.py 命令中没有属性 'id'
'int' object has no attribute 'id' in discord.py command
所以这个问题我已经有一个星期了,我真的不知道如何解决这个问题。
我正在创建一个调平系统,我可以通过命令 +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'
我还导入了所有需要的模块。
拜托,谁给我一个解决方案。谢谢
函数注解说你应该得到 user
类型的 User Object 参数。您获得的用户参数为 int
type id。
您的下一个错误可能在 user.mention
.
我建议您仔细检查传入的函数参数 user
以获得预期的 User Object
。
所以这个问题我已经有一个星期了,我真的不知道如何解决这个问题。 我正在创建一个调平系统,我可以通过命令 +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'
我还导入了所有需要的模块。
拜托,谁给我一个解决方案。谢谢
函数注解说你应该得到 user
类型的 User Object 参数。您获得的用户参数为 int
type id。
您的下一个错误可能在 user.mention
.
我建议您仔细检查传入的函数参数 user
以获得预期的 User Object
。