简体   繁体   中英

Execute loop start on every channel for every GUILD discord.py

I have made a bot command that has a task which loops every hour, it gets executed on_ready and after that it will just loop. However if my bot is in multiple servers, this will only get executed in one. Is there a way for it to work on all servers?

I have tried the following, but doesn't seem to work (I think as soon as i call the allFloors.start() that starts the loop on the first server but never comes back into the for statement.)

@bot.event
async def on_ready():
    await bot.wait_until_ready()

    guilds = bot.guilds
    for guild in guilds:
        for channel in guild.channels:
            if channel.name == "bot-commands":
                allFloors.start(channel)

How do I get the allFloors.start() function to work as a loop on every server its part of?

For context my all floors is set up like this

@tasks.loop(minutes=60)
async def allFloors(ctx):
    #Logic
    await ctx.send("Test")

All the other commands (That are just bot.command() work fine on both servers btw) Would be ideal if this starts when the bot gets added to any server? Maybe im looking at the wrong method? Cant find anything within documentation

This will start one task that runs every hour when the bot comes online. The task will loop through all the channels with the name bot-commands in all the guilds that the bot is a member of and send a message that says Test .

@tasks.loop(minutes=60)
async def allFloors():
    for guild in bot.guilds:
        for channel in guild.channels:
            if channel.name == "bot-commands":
                await channel.send("Test")


@bot.event
async def on_ready():
    allFloors.start()

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