TypeError:“协同程序”对象不可下标

2024-05-19 08:11:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个discord机器人,使用python作为语言,mongodb作为数据库。 我创建此命令是为了更新兵力数量,但当我运行此命令时,它显示TypeError:“coroutine”对象不可订阅。我认为$set行中的函数有问题,但一旦它工作正常,但现在显示错误。我不知道怎么修理它?。如果有人知道如何解决此问题,请更正此代码并帮助我

这是错误消息

Ignoring exception in command train:
Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "d:\code\kingdom fight\main.py", line 261, in training
    't1': res['tier1'], 't2': res['tier2'], 't3': res['tier3'], 'potions': res['training_train_pot']}})
TypeError: 'coroutine' object is not subscriptable

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

Traceback (most recent call last):
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'coroutine' object is not subscriptable
C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\asyncio\events.py:80: RuntimeWarning: coroutine 'Command.__call__' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback   
 

错误在这一行中

 await update_data(filtr, {'$set': {
                't1': res['tier1'], 't2': res['tier2'], 't3': res['tier3'], 'potions': res['training_train_pot']}})
@bot.command(name="train")  # Training the soldiers
# tier -  troop tier to train, Amount - No. of troops to train
async def training(ctx: commands.Context, tier: int = None, amount: int = None):
    '' Used to train troops `?train <tier> <troops_amount>` Eg: ||?train 3 100||''
    if(tier == None or amount == None):
        await ctx.send("Give the argument correctly like `?train <tier> <troops_amount>`")
    elif(user_info.find_one({'id': ctx.author.id})):
        if user_info.find_one({'id': ctx.author.id}) == None:
            await ctx.send('Please start using `?start`!')
        get_data = user_info.find_one({'id': ctx.author.id})
        update_data = user_info.find_one_and_update
        filtr = {'id': ctx.author.id}
        # print(get_data)
        res = train(amount, get_data['potions'], tier,
                    get_data['t1'], get_data['t2'], get_data['t3'])
        if(res == "Not enough training potions"):
            await ctx.send(f'{ctx.author.mention}You dont have enough potions')
        elif(res == "No such tier troops exist"):
            await ctx.send(f'{ctx.author.mention}No such tier exists. Tiers available are 1-3')
        else:
            await update_data(filtr, {'$set': {
                't1': res['tier1'], 't2': res['tier2'], 't3': res['tier3'], 'potions': res['training_train_pot']}})
            await ctx.send(f"{ctx.author.mention}You have successfully trained {amount} tier {tier} troops")
    elif(user_info.find_one({'id': ctx.author.id}) == None):
        await ctx.reply('Please start using `?start` command')
    else:
        await ctx.send("Give the argument correctly like `?train <tier> <troops_amount>`")

这是train函数

p_t1 = 1
p_t2 = 8
p_t3 = 20  # training potions of t1,t2,t3
train_pot = 0
def train(troop_count, train_pot, tier, t1, t2, t3):
    if(tier == 1):
        if(troop_count*p_t1 > train_pot):  # checks whether we have enough training costs for training t1
            result = "Not enough training potions"
            return result
        else:
            train_pot = train_pot-troop_count*p_t1  # if yes train the tier 1 troops
            t1 += troop_count
            result = {
                'training_train_pot': train_pot,
                'tier1': t1,
                'tier2': t2,
                'tier3': t3
            }
            return result
    elif(tier == 2):
        if(troop_count*p_t2 > train_pot):  # checks whether we have enough training costs for training t2
            result = "Not enough training potions"
            return result
        else:  # if yes train the tier 2 troops
            train_pot = train_pot-troop_count*p_t2
            t2 += troop_count
            result = {
                'training_train_pot': train_pot,
                'tier1': t1,
                'tier2': t2,
                'tier3': t3
            }
            return result
    elif(tier == 3):  # checks whether we have enough training costs for training t3
        if(troop_count*p_t3 > train_pot):
            result = "Not enough training potions"
            return result
        else:  # if yes train the tier 3 troops
            train_pot = train_pot-troop_count*p_t3
            t3 += troop_count
            result = {
                'training_train_pot': train_pot,
                'tier1': t1,
                'tier2': t2,
                'tier3': t3
            }
            return result
    else:
        # if we gave invalid tier print no such tier exist
        result = "No such tier troops exist"
        return result

Tags: iftrainingtrainresresultawaittiert1

热门问题