import discord from discord.ext import commands import os import asyncio import logging from dotenv import load_dotenv # Load environment variables load_dotenv() # Configure logging logging.basicConfig(level=logging.INFO) # Bot configuration intents = discord.Intents.default() intents.message_content = True intents.guilds = True intents.members = True bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): """Event triggered when the bot is ready""" print(f'{bot.user} is online and ready!') print(f'Bot ID: {bot.user.id}') print(f'Discord.py Version: {discord.__version__}') print('------') # Set bot status await bot.change_presence( activity=discord.Game(name="Hearts of Iron IV"), status=discord.Status.online ) @bot.event async def on_guild_join(guild): """Event triggered when the bot joins a server""" print(f'Bot joined server "{guild.name}" (ID: {guild.id})') @bot.event async def on_guild_remove(guild): """Event triggered when the bot leaves a server""" print(f'Bot left server "{guild.name}" (ID: {guild.id})') # Basic Commands @bot.command(name='ping') async def ping(ctx): """Shows the bot latency""" latency = round(bot.latency * 1000) await ctx.send(f'🏓 Pong! Latency: {latency}ms') @bot.command(name='info') async def info(ctx): """Shows bot information""" embed = discord.Embed( title="🤖 Bot Information", description="HOI4 ELO Bot - A Discord Bot for Hearts of Iron IV", color=discord.Color.blue() ) embed.add_field(name="Bot Name", value=bot.user.name, inline=True) embed.add_field(name="Bot ID", value=bot.user.id, inline=True) embed.add_field(name="Servers", value=len(bot.guilds), inline=True) embed.add_field(name="Discord.py Version", value=discord.__version__, inline=True) embed.set_thumbnail(url=bot.user.avatar.url if bot.user.avatar else None) embed.set_footer(text=f"Requested by {ctx.author}", icon_url=ctx.author.avatar.url if ctx.author.avatar else None) await ctx.send(embed=embed) @bot.command(name='help_hoi4') async def help_hoi4(ctx): """Shows available HOI4-related commands""" embed = discord.Embed( title="📋 HOI4 ELO Bot Commands", description="Here are the available commands:", color=discord.Color.green() ) commands_list = [ ("!ping", "Shows the bot latency"), ("!info", "Shows bot information"), ("!help_hoi4", "Shows this help message"), ("!server_info", "Shows server information"), ] for cmd, desc in commands_list: embed.add_field(name=cmd, value=desc, inline=False) embed.set_footer(text="More commands will be added in future updates!") await ctx.send(embed=embed) @bot.command(name='server_info') async def server_info(ctx): """Shows information about the current server""" guild = ctx.guild if not guild: await ctx.send("❌ This command can only be used on a server!") return embed = discord.Embed( title=f"🏰 Server Information - {guild.name}", color=discord.Color.orange() ) embed.add_field(name="Server Name", value=guild.name, inline=True) embed.add_field(name="Server ID", value=guild.id, inline=True) embed.add_field(name="Members", value=guild.member_count, inline=True) embed.add_field(name="Created", value=guild.created_at.strftime("%m/%d/%Y"), inline=True) embed.add_field(name="Boost Level", value=guild.premium_tier, inline=True) embed.add_field(name="Boost Count", value=guild.premium_subscription_count, inline=True) if guild.icon: embed.set_thumbnail(url=guild.icon.url) await ctx.send(embed=embed) @bot.event async def on_command_error(ctx, error): """Handles command errors""" if isinstance(error, commands.CommandNotFound): await ctx.send("❌ Command not found! Use `!help_hoi4` for a list of available commands.") elif isinstance(error, commands.MissingRequiredArgument): await ctx.send(f"❌ Missing arguments! Command: `{ctx.command}`") elif isinstance(error, commands.BadArgument): await ctx.send("❌ Invalid argument!") else: print(f"Unknown error: {error}") await ctx.send("❌ An unknown error occurred!") async def main(): """Main function to start the bot""" # Load Discord token from environment variables token = os.getenv('DISCORD_TOKEN') if not token: print("❌ DISCORD_TOKEN environment variable not found!") print("Please set the DISCORD_TOKEN variable in Coolify or create a .env file") return try: print("🚀 Starting bot...") await bot.start(token) except discord.LoginFailure: print("❌ Invalid Discord token!") except Exception as e: print(f"❌ Error starting bot: {e}") if __name__ == "__main__": asyncio.run(main())