我一直在尝试为我的命令(在我的main.py文件中)创建一个错误处理程序,但我似乎找不到连接cog和main.py的方法,我已经输入了一个print命令,以查看何时连接了cog,但没有显示,也许我做错了,但我找不到连接它们的方法,这让我有点担心
这是我的cog代码
from discord.ext.commands import Cog
class brody(commands.Cog, name='brody'):
pass
def __init__(self, bot):
self.bot = bot
bot.load_extension('brody')
@commands.Cog.listener()
async def on_ready(self):
print("Brody's cog online")
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if hasattr(ctx.command, 'on_error'):
return
cog = ctx.cog
if cog:
if cog._get_overridden_method(cog.cog_command_error) is not None:
return
ignored = (commands.CommandNotFound, )
error = getattr(error, 'original', error)
if isinstance(error, ignored):
return
if isinstance(error, commands.DisabledCommand):
await ctx.send('you can\'t use that, that is disabled')
elif isinstance(error, commands.NoPrivateMessage):
try:
await ctx.author.send('you can\'t use my commands when dming me, try it in a server i\'m in.')
except discord.HTTPException:
pass
elif isinstance(error, commands.BadArgument):
if ctx.command.qualified_name == 'tag list':
await ctx.send('i cannot find that person, probably try someone else or try again')
else:
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
def setup(bot):
bot.add_cog(mycog(bot))
是的,我确实复制/粘贴了GitHub教程中的一些代码,但我来这里是想了解如何将cog连接到我的main.py文件,以用于一些崩溃处理程序或其他东西,因此我想我们将非常感谢您的帮助,谢谢
除了缩进、类开头的
pass
和cog内的load_extension
调用之外,它看起来很好。您应该在主文件内调用load_extension
,并传递load_extension
您试图加载的文件的相对路径。例如,如果您的cog(我们称之为brody.py
)与主文件位于同一文件夹中,您将使用bot.load_extension("brody")
。有关更多信息,请参见the docs for cogs相关问题 更多 >
编程相关推荐