Files
minecraft-hosting-scripts/discord-bot/frankstein-bot.py
2025-10-02 00:51:42 +02:00

53 lines
1.2 KiB
Python

import discord
from discord.ext import tasks, commands
from mcstatus import JavaServer
import os
TOKEN = os.environ['TOKEN']
MC_HOST = os.environ.get('MC_HOST', 'localhost')
MC_PORT = int(os.environ.get('MC_PORT', 7272))
CHANNEL_ID = int(os.environ['CHANNEL_ID'])
intents = discord.Intents.default()
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Přihlášen jako {bot.user}')
update_status.start()
@tasks.loop(seconds=5)
async def update_status():
channel = bot.get_channel(CHANNEL_ID)
if channel is None:
print("Kanál nenalezen.")
return
try:
server = JavaServer(MC_HOST, MC_PORT)
status = server.status()
player_count = status.players.online
name = f'🟢|online|👤:{player_count}'
except Exception as e:
print("Server offline nebo nelze připojit:", e)
name = '🔴|server down!!!'
try:
if name != channel.name:
await channel.edit(name=name)
except discord.Forbidden:
print("Bot nemá oprávnění měnit název kanálu.")
except discord.HTTPException as e:
print("HTTP chyba:", e)
bot.run(TOKEN)