【发布时间】:2021-12-01 16:02:24
【问题描述】:
我正在尝试使用 python 作为语言和 mongodb 作为数据库来创建一个不和谐的机器人。 我正在创建这个命令来更新部队数量,但是当我运行这个命令时它显示 TypeError: 'coroutine' object is not subscriptable。我认为 $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
【问题讨论】:
-
它到底在抱怨哪一行?发布带有回溯的完整错误消息。
-
没有stacktrace很难看到,但是这个错误通常发生在你在await之后使用[]时,你应该使用
(await some_func())['some_key']而不是await some_func()['some_key'] -
我编辑了消息并将回溯错误行与我的代码一起粘贴。你能帮忙解决一下吗?
-
什么是
train...?它似乎返回了一个协程,所以res是一个协程…… -
@deceze 我也更新了火车功能。你能检查一下并帮助我如何防止返回一个couroutine
标签: python mongodb discord discord.py pymongo