Wednesday, 30 December 2020

Find Guild ID in on_ready() - Discord.py

I'm trying to make a random fact send at certain times. It worked before I implemented a dict to hold the random facts and it should work now, the only problem I'm running into is I can't access the guild id inside the on_ready() event. Here is what I have:

    async def func(self):
        await bot.wait_until_ready()
        with open('guild_settings.json', 'r') as file:
            guild_settings = json.loads(file.read())
------> guild_id = NEED
        channel = bot.get_channel(guild_settings[guild_id]["random_facts_channel_id"])

        random_messages = guild_settings[guild_id]["random_facts"].value()
        if random_messages is None:
            await channel.send(f"You need to add a fact first.")
        else:
            random_messages = random.choice(random_messages)
            await channel.send(random_messages)

    @commands.Cog.listener()
    async def on_ready(self):
        print("Bot is ready.")

        # Initializing scheduler
        scheduler = AsyncIOScheduler()

        # Sends "Your Message" at given times
        scheduler.add_job(self.func, CronTrigger(hour="0, 19", minute="10", second="0"))

        # Starting the scheduler
        scheduler.start()

If I were able to somehow get the guild id then I'm sure it would work correctly, I just can't figure out how.



from Find Guild ID in on_ready() - Discord.py

No comments:

Post a Comment