小编Ben*_*jin的帖子

为多个机器人加载cog

使用discord.py,我可以从一段代码运行多个机器人,但我正在寻找一种方法将cog或扩展加载到多个机器人.对于一个测试用例,我有bot.py,它处理加载cog并启动bot,cog.py这是一个简单的cog,逐渐增加1到一个计数器

bot.py

from discord.ext import commands
import asyncio

client1 = commands.Bot(command_prefix='!')
client2 = commands.Bot(command_prefix='~')

client1.load_extension('cog')
client2.load_extension('cog')

@client1.event
async def on_ready():
    print('client1 ready')

@client1.command()
async def ping():
    await client1.say('Pong')

@client2.event
async def on_ready():
    print('client2 ready')

@client2.command()
async def ping():
    await client2.say('Pong')

loop = asyncio.get_event_loop()
loop.create_task(client1.start('TOKEN1'))
loop.create_task(client2.start('TOKEN2'))
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)

cog.py

from discord.ext import commands

class TestCog:

    def __init__(self, bot):
        self.bot = bot
        self.counter = 0

    @commands.command()
    async def add(self):
        self.counter += 1
        await self.bot.say('Counter is now %d' …
Run Code Online (Sandbox Code Playgroud)

python python-3.x discord discord.py

8
推荐指数
1
解决办法
919
查看次数

标签 统计

discord ×1

discord.py ×1

python ×1

python-3.x ×1