Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.4 kB
1
Indexable
Never
import discord
import asyncio
import ping3

DISCORD_BOT_TOKEN = 'MTEzNDk1MjE0NDczOTUxNjUyNg.GEXLvu.JrHwkBeT6H4UQx5P5WU_Stv-A7hZeTG0b0iKLU'
CHANNEL_ID = 1134934695507939471
MINECRAFT_SERVER_IP = '23.241.209.111'
MINECRAFT_SERVER_PORT = 25565

client = discord.Client()

@client.event
async def on_ready():
    print(f'Logged in as {client.user.name} ({client.user.id})')
    print('Bot is ready to show Minecraft server status.')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # Listen for a specific command to stop the bot
    if message.content.lower() == '!stopbot':
        await message.channel.send('Stopping the bot.')
        await client.close()  # Gracefully close the bot

async def check_server_status():
    while True:
        status = "offline"
        try:
            response_time = ping3.ping(MINECRAFT_SERVER_IP, MINECRAFT_SERVER_PORT, timeout=1)
            if response_time is not None:
                status = "online"
        except ping3.exceptions.Timeout:
            pass

        channel = client.get_channel(CHANNEL_ID)
        await channel.send(f'Minecraft server is {status}.')

        await asyncio.sleep(60)

if __name__ == '__main__':
    client.loop.create_task(check_server_status())

    try:
        client.run(DISCORD_BOT_TOKEN)
    except KeyboardInterrupt:
        print('Bot stopped manually.')