Files
bot.py
config.py
monitor.py
ptero-status-bot.service
ptero.py
requirements.txt
Code
6 475
bot.py
import discord
from discord.ext import commands
import os
import asyncio
import logging
from dotenv import load_dotenv
from monitor import StatusMonitor
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger("ptero-bot")
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
monitor: StatusMonitor | None = None
@bot.event
async def on_ready():
global monitor
log.info(f"Logged in as {bot.user} (ID: {bot.user.id})")
channel_id = int(os.getenv("DISCORD_CHANNEL_ID", "0"))
if channel_id == 0:
log.error("DISCORD_CHANNEL_ID is not set in .env โ exiting.")
await bot.close()
return
channel = bot.get_channel(channel_id)
if channel is None:
log.error(f"Cannot find channel with ID {channel_id}. Check bot permissions.")
await bot.close()
return
monitor = StatusMonitor(bot, channel)
asyncio.create_task(monitor.start())
log.info(f"Monitor started in #{channel.name}")
@bot.event
async def on_disconnect():
log.warning("Bot disconnected from Discord.")
@bot.event
async def on_resumed():
log.info("Bot resumed connection.")
def main():
token = os.getenv("DISCORD_TOKEN")
if not token:
log.error("DISCORD_TOKEN is not set in .env โ exiting.")
return
bot.run(token, log_handler=None)
if __name__ == "__main__":
main()