Search code examples
pythondiscorddiscord.pyapscheduler

Is there a way to send messages to a guild's system channel, and use it in a scheduled task?


async def rules_reminder(self):
    channel = self.bot.guild.system_channel
    await channel.send("Remember to adhere to the rules!")
    
self.scheduler.add_job(self.rules_reminder, CronTrigger(second="0, 15, 30, 45"))

Solution

  • You could use discord.ext.tasks for this. discord.ext.tasks is a discord.py extension that allows users to easily create recurring tasks. For your code, this would be the discord.ext.tasks version:

    from discord.ext import tasks # Somewhere...
    # --------------------------------------------
    # Preferably somewhere in your cog's __init__...
    self.rules_reminder.start()
    
    @tasks.loop(seconds=15)
    async def rules_reminder(self):
        channel = self.bot.guild.system_channel
        await channel.send("Remember to adhere to the rules!")
    

    One last thing, just to make sure you know: neither commands.Bot or discord.Client don't have a guild attribute. I assume you set the guild yourself.