【发布时间】:2018-10-15 20:23:09
【问题描述】:
我需要在多个扩展(cogs)中使用同一个对象。如何编辑我的简单机器人示例来实现这一点?
我可以在我的 main.py 或 cog 文件中启动 SHARED_OBJECT。要点是在 COG1 和 COG2 扩展中使用相同的对象。重要的是要考虑到每个扩展都可以在程序启动后重新加载。
我正在使用discord.py rewrite lib
main.py
client = Bot(description="Bot", command_prefix="!")
client.run(__token__)
SHARED_OBJECT = SomeObject()
@client.event
async def on_ready():
client.dev = True
print('[Discord] Logged in as {} (ID:{}) | Connected to {} servers'.format(client.user.name,client.user.id,1))
client.load_extension('COG1')
client.load_extension('COG2')
COG1.PY 类
class COG1():
def __init__(self, bot):
self.bot = bot
@commands.command()
async def test(self):
print()
def setup(bot):
bot.add_cog(COG1(bot))
COG1.PY 类
class COG2():
def __init__(self, bot):
self.bot = bot
@commands.command()
async def test(self):
print("test")
def setup(bot):
bot.add_cog(COG2(bot))
【问题讨论】:
标签: python python-3.x discord.py discord.py-rewrite