Search code examples
pythondiscord.pyinvoke

Invoking command inside command discord.py


Okay, so i need two commands to duplicate, and the default code is written like this (I know it hard-to-reed one, cause it's full of references, but it's not really important, it's here just for context):

@bot.command(name='Bot', help="Dane nt. zakażenia w danym miejscu")
async def cv_local(ctx, country=""):
    translator = Translator()
    translation = translator.translate(country, 'en')
    country = translation.text
    now = datetime.datetime.now()
    startTime = time.time()
    if country == "":
        country = "world"
        print("At " + str(now.hour) + ":" + str(now.minute) + " user " + str(ctx.message.author.name) + "(Id: " + str(
            ctx.message.author.id) + ")" +
              " didn't mention any country, sent data for world")
    else:
        print("At " + str(now.hour) + ":" + str(now.minute) + " user " + str(ctx.message.author.name) + "(Id: " + str(
            ctx.message.author.id) + ")" + " searched for: " +
              str(Library.exceptionCheck(country)[1]))

    if str(country).lower() == "world" or str(country).lower() == "kw" or str(country).lower() == "za":
        url = 'https://www.worldometers.info/coronavirus/'
        code = Library.HttpsRead(url, "świata", translation.src)
    else:
        temp1 = Library.exceptionCheck(country)[0]
        url = 'https://www.worldometers.info/coronavirus/country/' + temp1
        code = Library.HttpsRead(url, country, config[ctx.guild.name]['country'])

    await ctx.send(embed=code)
    print("execution took %s seconds \n" % (time.time() - startTime))

And I have read a lot about it, so I tried many methods f.e.:


@bot.command(name='Cv', help="Dane nt. zakażenia w danym miejscu")
async def cv_local2(ctx, leng=""):
    print("Invoking...")
    await ctx.invoke(bot.get_command('cv_local'), country='Polska')
@bot.command(name='Cv', help="Dane nt. zakażenia w danym miejscu")
async def cv_local2(ctx, leng=""):
    print("Invoking...")
    await bot.get_command('cv_local').callback(ctx, leng)
@bot.command(name='Cv', help="Dane nt. zakażenia w danym miejscu")
async def cv_local2(self, ctx, leng=""):
    print("Invoking...")
    await ctx.invoke(self.bot.get_command(cv_local), country=leng)
@bot.command(name='Cv', help="Dane nt. zakażenia w danym miejscu")
async def cv_local2(ctx, leng=""):
    print("Invoking...")
    await cv_local.invoke(ctx, country='Poland')

and no error traces that I could post to you, It's Frustrating

Edit: So I've rewieved my code, and realised that I have an exception handler, so I won't get error traces, I deleted it, and now I'm using the first code

@bot.command(name='Cv', help="Dane nt. zakażenia w danym miejscu")
async def cv_local2(ctx, leng=""):
    print("Invoking...")
    await ctx.invoke(bot.get_command('cv_local'), country='Polska')

I get error like this:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\CoronaBot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/user/PycharmProjects/CoronaBot/bot.py", line 62, in cv_local2
    await ctx.invoke(bot.get_command('cv_local'), country='Polska')
  File "C:\Users\user\PycharmProjects\CoronaBot\venv\lib\site-packages\discord\ext\commands\context.py", line 129, in invoke
    if command.cog is not None:
AttributeError: 'NoneType' object has no attribute 'cog'

So it means that I have to make this whole code into a cog, right? I would really like to keep is as simple as possible, so I don't want to add more cogs, than I already have, is there anyway to do it without them?


Solution

  • Finally I made one more version of the command and it's working:

    @bot.command(name='cv', help="Dane nt. zakażenia w danym miejscu")
    async def cv_local2(ctx, leng=""):
        print("Invoking...")
        temp1 = bot.commands
        temp = bot.get_command(name='Bot')
        await temp.callback(ctx, leng)