简体   繁体   中英

discord.py running inside subprocess

Hi I want to discord bot that would be controlled by multiple cpu cores at once. So I would be able to use more computational power from my cpu (servers will have assigned different servers to respond to).

Code:

import asyncio
from aiomultiprocess import Pool
import discord
from discord.ext import commands


async def c(core):
    print('core assigned' + str(core))
    client = commands.Bot(command_prefix='!', intents=discord.Intents().all())
    client.remove_command('prefix')
    client.remove_command('help')

    @client.event
    async def on_ready():
        print("ready")

    @client.event
    async def on_message(ctx):
        if ctx.author == client.user:
            return
        await ctx.channel.send("something")

    client.run('token')


async def main():
    async with Pool() as pool:
        await pool.map(c, core)

if __name__ == "__main__":
    core = [1, 2...]
    asyncio.run(main())

Error is pretty long and I dont want to blank out every personal information. Is there any way to do that or I am trying to do impossible?

It would be impractical to run two discord bots from one program (in the way above) as they'd share the same IP address with the code above - because ratelimits are done by ip address, and since both the bots would be connecting from the same IP, you could surpass the ratelimits and get temporarily banned from the discord API.

Since you want the discord bots to be controlled by 1 program, you could theoretically proxy the traffic or use a vpn on one bot (although this might be impractical).

Alternatively, if you're happy with two programs on separate machines, you could build a mechanism to communicate between them, and get similar functionality to running both the discord bots in one script.

You could put the other bots in the event loop of the main one by doing something like this.

import asyncio
import discord
from discord.ext import commands
loop = asyncio.get_event_loop()
mainbot = commands.Bot(loop=loop, command_prefix="!")
bot_one = commands.Bot(loop=loop, command_prefix="1>")
bot_two = commands.Bot(loop=loop, command_prefix="2>")
@mainbot.command()
async def ping1(ctx):
  await ctx.send("Pong. I'm main bot.")
@bot_one.command()
async def ping2(ctx):
  await ctx.send("Pong. I'm bot one.")
@bot_two.command()
async def ping3(ctx):
  await ctx.send("Pong. I'm bot two.")
loop.create_task(bot_one.start(bot1token))
loop.create_task(bot_two.start(bot2token))
mainbot.run(mainbottoken)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM