I am trying to create a discord bot with python as language and mongodb as database. I am creating this command to update the troops amount but when I run this command it shows TypeError: 'coroutine' object is not subscriptable. I think there is a problem with the function in the $set line but once it works fine but now it shows error. I don't know how to fix it?. If someone knows how to fix this problem please rectify this code and help me.
This is the error message
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
The error is in this line
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>`")
This is the train
function
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
Instead of writing the function outside the command. Try running the command without function. Like do all the stuffs that was done inside the function, in inside the command.