PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
791 stars 163 forks source link

[QUESTION] Multiple instances. #357

Closed Nucito closed 1 year ago

Nucito commented 1 year ago

It's possible to run multiple bots? i try create tasks but don't work propely:

from twitchio.ext import commands
import json
import asyncio

STATUS = {"Count": 0, "ADMINs": ["ADM1", "ADM2"]}

class Bot(commands.Bot):
    def __init__(self, token):
        super().__init__(
            token=token,
            prefix="!",
            initial_channels=["channeltest"],
        )

        STATUS["Count"] += 1

    async def event_ready(self):
        print(f"Logged in as [{self.nick}]")

    @commands.command()
    async def botcount(self, ctx: commands.Context):
        if self.nick != "BotMain":
            return

        await ctx.send(f"BOTS: {STATUS['Count']}")

async def start_bots():
    with open("tokens.json", "r") as fl:
        CONFIG = json.load(fl)

    tasks = []

    for user in CONFIG["users"]:
        bot = Bot(user["token"])
        tasks.append(asyncio.ensure_future(bot.start()))

    await asyncio.gather(*tasks)

asyncio.run(start_bots())

All the bot start without any problem but all of them have the same name if i call self.nick.

github-actions[bot] commented 1 year ago

Hello! Thanks for the issue. If this is a general help question, for a faster response consider joining the official Discord Server

Else if you have an issue with the library please wait for someone to help you here.

Nucito commented 1 year ago

Dup of #173