简体   繁体   English

TypeError: '_asyncio.Future' 对象不可下标,使用 mongodb 的异步电机驱动程序

[英]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.

相关问题 Python TypeError: '_asyncio.Future' object 不可订阅 - Python TypeError: '_asyncio.Future' object is not subscriptable 类型错误:需要 asyncio.Future、协程或 awaitable - TypeError: An asyncio.Future, a coroutine or an awaitable is required Pickle Telethon 消息抛出“无法腌制 '_asyncio.Future' 对象” - Pickle telethon message throws “cannot pickle '_asyncio.Future' object” asyncio: TypeError: 'coroutine' 对象不可下标 - asyncio: TypeError: 'coroutine' object is not subscriptable loop.run_until_complete 给出“TypeError: An asyncio.Future, a coroutine or an awaitable is required” - loop.run_until_complete gives “TypeError: An asyncio.Future, a coroutine or an awaitable is required” 使用 Motor AsyncIO 和 Pytest 测试 MongoDB 功能 - Testing for MongoDB Functionality using Motor AsyncIO and Pytest 为什么asyncio.Future与concurrent.futures.Future不兼容? - Why is asyncio.Future incompatible with concurrent.futures.Future? 为什么会出现由 concurrent.futures.Future 引发的 asyncio.Future 错误? - Why is there an asyncio.Future error raised by a concurrent.futures.Future? 带有回调的asyncio.future取消后导致异常 - asyncio.future with callback causes Exception after cancellation 在不同线程的回调中设置asyncio.Future的值 - Setting asyncio.Future's value in a callback from different thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM