title: Find Guild ID in on_ready() - Discord.py
body: <p>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:</p>
  <pre class="lang-py prettyprint-override"><code>    async def func(self):
          await bot.wait_until_ready()
          with open('guild_settings.json', 'r') as file:
              guild_settings = json.loads(file.read())
  ------&gt; guild_id = NEED
          channel = bot.get_channel(guild_settings[guild_id][&quot;random_facts_channel_id&quot;])
  
          random_messages = guild_settings[guild_id][&quot;random_facts&quot;].value()
          if random_messages is None:
              await channel.send(f&quot;You need to add a fact first.&quot;)
          else:
              random_messages = random.choice(random_messages)
              await channel.send(random_messages)
  
      @commands.Cog.listener()
      async def on_ready(self):
          print(&quot;Bot is ready.&quot;)
  
          with open('guild_settings.json', 'r') as file:
              guild_settings = json.loads(file.read())
          # Initializing scheduler
          scheduler = AsyncIOScheduler()
          hour = 12
          minute = 0
          for guild in bot.guilds:
              guild_id = str(guild.id)
              try:
                  hour = guild_settings[str(guild_id)][&quot;random_facts_send_time&quot;][&quot;hour&quot;]
                  minute = guild_settings[str(guild_id)][&quot;random_facts_send_time&quot;][&quot;minute&quot;]
              except KeyError:
                  print(f&quot;{guild_id} has a KeyError with the random fact feature.&quot;)
                  continue
  
          # Sends &quot;Your Message&quot; at 12PM and 18PM (Local Time)
          scheduler.add_job(self.func, CronTrigger(hour=hour, minute=minute, second=&quot;0&quot;))
  
          # Starting the scheduler
          scheduler.start()
  </code></pre>
  <p>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.</p>
  <p>EDIT: This is what I have working so far</p>
  <pre class="lang-py prettyprint-override"><code>    async def func(self):
          await bot.wait_until_ready()
          with open('guild_settings.json', 'r') as file:
              guild_settings = json.loads(file.read())
          for guild_id in guild_settings:
              channel = bot.get_channel(guild_settings[guild_id][&quot;random_facts_channel_id&quot;])
              if guild_settings[guild_id][&quot;random_facts&quot;] is not None:
                  random_facts = []
                  for values in guild_settings[guild_id][&quot;random_facts&quot;].values():
                      random_fact = values.split(&quot;\n&quot;)
                      random_facts += random_fact
                  try:
                      random_choice = random.choice(random_facts)
                  except IndexError:
                      continue
                  await channel.send(random_choice)
     @commands.Cog.listener()
      async def on_ready(self):
          print(&quot;Bot is ready.&quot;)
  
          with open('guild_settings.json', 'r') as file:
              guild_settings = json.loads(file.read())
          # Initializing scheduler
          scheduler = AsyncIOScheduler()
          hour = 12
          minute = 0
          for guild in bot.guilds:
              guild_id = str(guild.id)
              try:
                  hour = guild_settings[str(guild_id)][&quot;random_facts_send_time&quot;][&quot;hour&quot;]
                  minute = guild_settings[str(guild_id)][&quot;random_facts_send_time&quot;][&quot;minute&quot;]
              except KeyError:
                  print(f&quot;{guild_id} has a KeyError with the random fact feature.&quot;)
                  continue
  
          # Sends &quot;Your Message&quot; at 12PM and 18PM (Local Time)
          scheduler.add_job(self.func, CronTrigger(hour=hour, minute=minute, second=&quot;0&quot;))
  
          # Starting the scheduler
          scheduler.start()
  </code></pre>