[英]TypeError: '_asyncio.Future' object is not subscriptable, using async motor driver for mongodb
I have a code where I try to update a user's data whenever he posts a new message:我有一个代码,每当他发布新消息时,我都会尝试更新用户的数据:
@bot.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel):
return
collection = db[f"{message.guild.id}"]
coll = db.guilds
if message.author.bot:
return
else:
if message.channel.id != 796145301013397544:
if not await coll.count_documents({"_id": message.guild.id}):
await coll.insert_one({"_id": message.guild.id, "suggest_channel": "None", "message_coins": 0.5, "log_channel": "None", 'prefix': '.', 'ignore_channel': 'None', 'likes': 0})
message_coins = await coll.find_one({"_id": message.guild.id})["message_coins"]
if not await collection.count_documents({"_id": message.author.id}):
await collection.insert_one({"_id": message.author.id, "msg": 0, "happy": 0, "coins": 0, "badge": "Нет", "cookie": 0, "minvoice": 0, "color": 0xFFFFFF, "osebe": "Отсутствует", "age": "Неизвестно"})
await collection.update_one({"_id": message.author.id}, {"$inc": {"msg": 1}})
await collection.update_one({"_id": message.author.id}, {"$inc": {"coins": message_coins}})
else:
pass
await bot.process_commands(message)
asyncio.get_event_loop().run_until_complete(on_message(message))
but, when it is activated, I get an TypeError:但是,当它被激活时,我收到一个 TypeError:
Ignoring exception in on_message
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/app/bot.py", line 187, in on_message
message_coins = await coll.find_one({"_id": message.guild.id})["message_coins"]
TypeError: '_asyncio.Future' object is not subscriptable
how can this TypeError be fixed?如何修复这个 TypeError?
Your issue here is in the nature that python will interpret this line of code您的问题在于 python 将解释这行代码的性质
message_coins = await coll.find_one({"_id": message.guild.id})["message_coins"]
It will see as call the find_one method on the coll object, use subscript to get the "message_coins" element, then pass that object to await
.它将看到调用 coll 对象上的 find_one 方法,使用下标获取“message_coins”元素,然后将该对象传递给
await
。 However this is not what you actually want.然而,这并不是你真正想要的。
message_coins = (await coll.find_one({"_id": message.guild.id}))["message_coins"]
You need to use perenthesis to force the order of operations here.您需要使用括号来强制此处的操作顺序。 So by wrapping the await and the method call it tells python to pass that to
await
then once you get the final result back from the await call you then use subscript to locate the "message_coins" element.因此,通过包装 await 和方法调用,它告诉 python 将它传递给
await
然后一旦您从 await 调用中获得最终结果,您就可以使用下标来定位“message_coins”元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.