I’m working on a Discord bot that should monitor if a game server is up or down. The problem is it keeps saying the server is offline when I know for sure its actually online and working.
I’ve tested with different IP addresses and ports but they all return offline status. The bot runs without errors but the status detection isn’t working right. I can connect to the server through other tools so I know its not actually down.
One time I managed to get it showing online status but then when the server actually went down it didn’t send any notification. I have no idea what I changed to make that work.
import os
import discord
import socket
import asyncio
BOT_TOKEN = os.environ['BOT_TOKEN']
NOTIFY_CHANNEL = 1234567890123456789
HOST_ADDRESS = '192.168.1.100'
HOST_PORT = 25565
bot = discord.Client(intents=discord.Intents.all())
async def monitor_host_status():
last_state = None
while True:
try:
await bot.wait_until_ready()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect((HOST_ADDRESS, HOST_PORT))
sock.close()
current_state = "up"
except Exception:
current_state = "down"
if current_state != last_state:
last_state = current_state
notify_channel = bot.get_channel(NOTIFY_CHANNEL)
await notify_channel.send(f"Host status changed: {current_state}")
await asyncio.sleep(120)
@bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
bot.loop.create_task(monitor_host_status())
bot.run(BOT_TOKEN)
I’m running this on Replit so maybe that’s causing issues with the socket connections? Has anyone else had problems with server monitoring bots giving wrong status updates?