有没有办法在 python 中为 discord 机器人导入带有装饰器的函数
Is there a way to import functions with decorators in python for discord bots
如标题所述,如果可能的话,我希望能够在机器人代码中导入包含装饰器的部分代码。
from discord.ext import commands
class Updates(commands.Cog):
def __init__(self, bot):
self.bot = bot
if __name__ == "__main__":
token = 'XXXXXXXXXXXXXXXXXXX'
prefix = '!'
bot = commands.Bot(command_prefix=commands.when_mentioned_or(
prefix), description='Useless - Updates')
bot.add_cog(Updates(bot))
bot.run(token)
我有 3 个或更多这样的 sub-modules(我有一个主文件来检查查询 class 的模块,这里 'Updates')并且我有一个共同点部分代码:
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
return
raise error
@commands.group(name='stop', hidden=True)
@commands.is_owner()
async def stop(self, ctx):
await ctx.message.add_reaction('\N{THUMBS UP SIGN}')
await self.bot.logout()
sys.exit()
@commands.group(name='reload', hidden=True)
@commands.is_owner()
async def reload(self, ctx):
await ctx.message.add_reaction('\N{THUMBS UP SIGN}')
await self.bot.logout()
sys.stdout.flush()
os.execv(sys.executable, [sys.executable, __file__])
有人吗?
最有效的方法是使用cogs。这些基本上是 类,其功能相当于 "groupings" 命令。然后可以通过 bot.add_cog(YourCogClass(bot))
添加这样的 类
可以找到更详尽的解释 。
如标题所述,如果可能的话,我希望能够在机器人代码中导入包含装饰器的部分代码。
from discord.ext import commands
class Updates(commands.Cog):
def __init__(self, bot):
self.bot = bot
if __name__ == "__main__":
token = 'XXXXXXXXXXXXXXXXXXX'
prefix = '!'
bot = commands.Bot(command_prefix=commands.when_mentioned_or(
prefix), description='Useless - Updates')
bot.add_cog(Updates(bot))
bot.run(token)
我有 3 个或更多这样的 sub-modules(我有一个主文件来检查查询 class 的模块,这里 'Updates')并且我有一个共同点部分代码:
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
return
raise error
@commands.group(name='stop', hidden=True)
@commands.is_owner()
async def stop(self, ctx):
await ctx.message.add_reaction('\N{THUMBS UP SIGN}')
await self.bot.logout()
sys.exit()
@commands.group(name='reload', hidden=True)
@commands.is_owner()
async def reload(self, ctx):
await ctx.message.add_reaction('\N{THUMBS UP SIGN}')
await self.bot.logout()
sys.stdout.flush()
os.execv(sys.executable, [sys.executable, __file__])
有人吗?
最有效的方法是使用cogs。这些基本上是 类,其功能相当于 "groupings" 命令。然后可以通过 bot.add_cog(YourCogClass(bot))
可以找到更详尽的解释