在命令discord.py中调用命令

时间:2020-10-23 21:18:32

标签: python discord.py invoke

好吧,所以我需要两个命令来重复,并且默认代码是这样写的(我知道它很难理解,因为它充满了引用,但这并不是很重要,它只是出于上下文) :

@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))

我已经读了很多,所以我尝试了很多方法,例如:


@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')

没有我可以发布给您的错误痕迹,这很令人沮丧

编辑:所以我重新整理了代码,意识到我有一个异常处理程序,所以我不会得到错误跟踪,我删除了它,现在我正在使用第一个代码

@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')

我收到这样的错误:

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'

所以这意味着我必须将整个代码变成一个齿轮,对吗? 我真的想保持尽可能的简单,所以我不想添加比我已经拥有的更多的齿轮,如果没有它们,有没有办法做?

1 个答案:

答案 0 :(得分:0)

最后,我又做了一个版本的命令,它正在起作用:

@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)