Issue with Dynamic Status Updates
I’m working on a Discord bot that should monitor a game server and update its presence status accordingly. The bot connects successfully but the status information gets stuck and doesn’t refresh with new data.
import discord
from discord.ext import commands, tasks
import gamestat
import asyncio
def fetch_server_info():
server_checker = gamestat.ServerStat('my-server.example.com', 25565)
if server_checker.online:
status_text = f'Online! v{server_checker.version} - {server_checker.active_users}/{server_checker.max_users} players'
return status_text
else:
return 'Server Offline'
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f'{bot.user} has started')
update_status.start()
@tasks.loop(minutes=2)
async def update_status():
current_info = fetch_server_info()
game_activity = discord.Game(name=current_info)
await bot.change_presence(activity=game_activity, status=discord.Status.online)
bot.run('YOUR_BOT_TOKEN')
Problem: The presence shows the initial status but never updates even when server conditions change. I tried using sleep functions in the ready event but got heartbeat timeout errors. What’s the proper way to create a continuously updating status for Discord bots?
had this exact same problem! turns out discord.py tasks can sometimes get stuck if theres any error in the loop. add @update_status.before_loop
decorator with await bot.wait_until_ready()
to make sure bot is fully initialized first. also try adding a print statement inside your task loop to see if its actually running every 2 mins - mine wasnt executing at all after first error.
The issue is likely that your fetch_server_info()
function isn’t properly handling exceptions when the server query fails. I had a similar problem where my bot would silently fail on network timeouts and keep displaying stale data. Try wrapping your server status check in a try-except block and add some logging to see what’s actually happening during each loop iteration. Also, make sure you’re calling update_status.start()
only after the bot is fully ready - I’ve seen cases where starting the task too early causes it to run once then stop. Consider adding a small delay before the first status update and verify that your gamestat library isn’t caching results internally. The heartbeat timeout you mentioned suggests there might be blocking operations in your status fetch that need to be made async.
I encountered something similar when building my server monitoring bot last year. The main culprit was that the gamestat library operations were blocking the event loop, which prevents the task from completing properly. Your fetch_server_info function needs to be async and you should use an async-compatible library for server queries. I switched to using aiohttp for my server pings and wrapped everything in async/await. Another thing that caught me was Discord’s rate limiting on presence updates - they throttle frequent changes even if your code runs correctly. Try increasing your loop interval to 5 minutes initially to test if the updates work at all. Also check if your gamestat.ServerStat is maintaining persistent connections that might be timing out between calls. Converting the server check to async resolved my freezing issues completely.