在命令 discord.py 中调用命令
Invoking command inside command discord.py
好的,所以我需要复制两个命令,默认代码是这样写的(我知道很难读懂一个,因为它充满了参考,但这并不重要,它只是为了上下文):
@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))
而且我也看了很多,所以尝试了很多方法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')
并且没有错误痕迹,我可以 post 给你,这令人沮丧
编辑:所以我修改了我的代码,并意识到我有一个异常处理程序,所以我不会得到错误跟踪,我删除了它,现在我正在使用第一个代码
@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'
所以这意味着我必须将整个代码变成一个齿轮,对吗?
我真的很想保持尽可能简单,所以我不想添加比我已经拥有的更多的齿轮,如果没有它们,无论如何都可以做到吗?
最后我又制作了一个版本的命令并且它正在运行:
@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)
好的,所以我需要复制两个命令,默认代码是这样写的(我知道很难读懂一个,因为它充满了参考,但这并不重要,它只是为了上下文):
@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))
而且我也看了很多,所以尝试了很多方法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')
并且没有错误痕迹,我可以 post 给你,这令人沮丧
编辑:所以我修改了我的代码,并意识到我有一个异常处理程序,所以我不会得到错误跟踪,我删除了它,现在我正在使用第一个代码
@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'
所以这意味着我必须将整个代码变成一个齿轮,对吗? 我真的很想保持尽可能简单,所以我不想添加比我已经拥有的更多的齿轮,如果没有它们,无论如何都可以做到吗?
最后我又制作了一个版本的命令并且它正在运行:
@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)