I’m having trouble with my Discord bot that monitors server status. The problem is that it keeps reporting the server as offline even though I know it’s actually online.
Here’s what I’ve built:
import os
import discord
import socket
import asyncio
API_KEY = os.environ['API_KEY']
NOTIFY_CHANNEL = 1234567890123456789
HOST_ADDRESS = '192.168.1.100'
HOST_PORT = 25565
bot = discord.Client(intents=discord.Intents.all())
async def monitor_host():
last_state = None
while True:
try:
await bot.wait_until_ready()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3)
sock.connect((HOST_ADDRESS, HOST_PORT))
sock.close()
status = "up"
except Exception:
status = "down"
if status != last_state:
last_state = status
target_channel = bot.get_channel(NOTIFY_CHANNEL)
await target_channel.send(f"Host status changed to {status}")
await asyncio.sleep(120)
@bot.event
async def on_ready():
print(f'Bot ready: {bot.user}')
bot.loop.create_task(monitor_host())
bot.run(API_KEY)
The issue: It always says the server is down but I can connect to it fine using other tools. I tried different IPs and ports but same result. Could this be because I’m running it on Replit? Maybe there’s some network restriction or I’m missing something in my code?