Every single day at roughly around the same time, when I run the code for a chatbot I have been working on (integrated into discord). I get a weird error message with no description or error code:
2024-12-16 19:35:38 WARNING discord.gateway Shard ID None heartbeat blocked for more than 110 seconds.
Loop thread traceback (most recent call last):
File "c:\Users\edens\CoralAI\stupidassbot.py", line 211, in <module>
DiscClient.run('')
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 860, in run
asyncio.run(runner())
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 674, in run_until_complete
self.run_forever()
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_events.py", line 322, in run_forever
super().run_forever()
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 641, in run_forever
self._run_once()
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 1987, in _run_once
handle._run()
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\asyncio\events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 441, in _run_event
await coro(*args, **kwargs)
File "c:\Users\edens\CoralAI\stupidassbot.py", line 97, in on_ready
chatSession.send_message(personality)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\generativeai\generative_models.py", line 578, in send_message
response = self.model.generate_content(
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\generativeai\generative_models.py", line 331, in generate_content
response = self._client.generate_content(
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\client.py", line 830, in generate_content
response = rpc(
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\api_core\gapic_v1\method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\api_core\retry\retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\api_core\retry\retry_unary.py", line 144, in retry_target
result = target()
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\api_core\timeout.py", line 120, in func_with_timeout
return func(*args, **kwargs)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\api_core\grpc_helpers.py", line 76, in error_remapped_callable
return callable_(*args, **kwargs)
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\grpc\_channel.py", line 1178, in __call__
) = self._blocking(
File "C:\Users\edens\AppData\Local\Programs\Python\Python312\Lib\site-packages\grpc\_channel.py", line 1162, in _blocking
event = call.next_event()
This seems to be an API error as I am seeing several lines pointing out the generate content function. And also the fact that creating a new API key seems to resolve the issue (sometimes). Here is the start of my code for where the error seems to first appear:
startTime = time.time()
class Client(discord.Client):
async def on_ready(self):
global running, chatSession, priorityChannel
while running == False:
try:
print("ONLINE!!!")
chatSession.send_message(personality)
chatSession.send_message(description)
mainChannel = DiscClient.get_channel("channel id placeholder")
await mainChannel.send("Coral-Sama is now online :P")
chatSession.send_message("DO NOT USE EMOJIS IN ANY OF YOUR MESSAGES")
running = True
except Exception as e:
print(f"\033[0;31mError sending prompts: {e} \nTrying again in 15 seconds...")
time.sleep(15)
continue
behaviours.messageLoop.start()
If anyone could point out how this error is happening and how I could fix it that would be great thanks!
Solved! Go to Solution.
Hi @Stisra,
Welcome to Google Cloud Community!
The Error “discord.gateway Shard ID None heartbeat blocked for more than 110 seconds.” This means the Discord bot's main thread is blocked and can't send heartbeats to Discord.
Here is a workaround and breakdown using asynchronous programming:
await
to wait for them by creating a task with asyncio.create_task.
Please note that the following is an example code demonstration with explanation. For available code templates, check here the documentation.
import discord
import time
import asyncio # Import the asyncio library
class Client(discord.Client):
async def on_ready(self):
global running, chatSession, priorityChannel
while running == False:
try:
print("ONLINE!!!")
# Move the blocking calls to an async task
async def send_startup_messages():
await chatSession.send_message(personality)
await chatSession.send_message(description)
mainChannel = DiscClient.get_channel("channel id placeholder")
await mainChannel.send("Coral-Sama is now online :P")
await chatSession.send_message("DO NOT USE EMOJIS IN ANY OF YOUR MESSAGES")
# create a task for the startup messages
await asyncio.create_task(send_startup_messages())
running = True
except Exception as e:
print(f"\033[0;31mError sending prompts: {e} \nTrying again in 15 seconds...")
await asyncio.sleep(15) # Use asyncio.sleep instead of time.sleep
behaviours.messageLoop.start()
Explanation:
async def on_ready(self)
: The on_ready function is now declared as async. This allows you to use await within it.async def send_startup_messages():
: A new async method that contains all your blocking chatSession.send_message() and .send() callsawait asyncio.create_task(send_startup_messages())
: Create a task from your async method. This will run concurrently and allow the on_ready loop to continue running.await asyncio.sleep(15)
: Use asyncio.sleep instead of time.sleep in your except block, as time.sleep is blocking.For your reference, check these documentation:
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
Hi @Stisra,
Welcome to Google Cloud Community!
The Error “discord.gateway Shard ID None heartbeat blocked for more than 110 seconds.” This means the Discord bot's main thread is blocked and can't send heartbeats to Discord.
Here is a workaround and breakdown using asynchronous programming:
await
to wait for them by creating a task with asyncio.create_task.
Please note that the following is an example code demonstration with explanation. For available code templates, check here the documentation.
import discord
import time
import asyncio # Import the asyncio library
class Client(discord.Client):
async def on_ready(self):
global running, chatSession, priorityChannel
while running == False:
try:
print("ONLINE!!!")
# Move the blocking calls to an async task
async def send_startup_messages():
await chatSession.send_message(personality)
await chatSession.send_message(description)
mainChannel = DiscClient.get_channel("channel id placeholder")
await mainChannel.send("Coral-Sama is now online :P")
await chatSession.send_message("DO NOT USE EMOJIS IN ANY OF YOUR MESSAGES")
# create a task for the startup messages
await asyncio.create_task(send_startup_messages())
running = True
except Exception as e:
print(f"\033[0;31mError sending prompts: {e} \nTrying again in 15 seconds...")
await asyncio.sleep(15) # Use asyncio.sleep instead of time.sleep
behaviours.messageLoop.start()
Explanation:
async def on_ready(self)
: The on_ready function is now declared as async. This allows you to use await within it.async def send_startup_messages():
: A new async method that contains all your blocking chatSession.send_message() and .send() callsawait asyncio.create_task(send_startup_messages())
: Create a task from your async method. This will run concurrently and allow the on_ready loop to continue running.await asyncio.sleep(15)
: Use asyncio.sleep instead of time.sleep in your except block, as time.sleep is blocking.For your reference, check these documentation:
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.