Files
.gitignore
bot.py
requirements.txt
Code
3 423
bot.py
import discord
from discord.ext import commands, tasks
from discord import app_commands
import os
from dotenv import load_dotenv
import asyncio
import json
from datetime import datetime, timedelta
import random
from mcstatus import JavaServer, BedrockServer
load_dotenv()
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='-', intents=intents)
config_file = 'config.json'
active_status_messages = {}
active_giveaways = {}
def load_config():
if os.path.exists(config_file):
with open(config_file, 'r') as f:
return json.load(f)
return {"welcome_channels": {}}
def save_config(config):
with open(config_file, 'w') as f:
json.dump(config, f, indent=4)
config = load_config()
@bot.event
async def on_ready():
print(f'Bot is ready! Logged in as {bot.user}')
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
except Exception as e:
print(f"Failed to sync commands: {e}")
update_status_messages.start()
@bot.event
async def on_member_join(member):
guild_id = str(member.guild.id)
if guild_id in config.get("welcome_channels", {}):
channel_id = config["welcome_channels"][guild_id]
channel = member.guild.get_channel(channel_id)
if channel:
embed = discord.Embed(
description=(
f"<:emoji_78:1446021531603046410> Welcome To Our Server\n\n"
f"<:emoji_78:1446021531603046410> <a:Boost:1303289427514364006> Welcome To Mineleaf Network <a:Boost:1303289427514364006>\n\n"
f"▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n\n"
f"<a:errow:1145379079877566484> Read And Follow <a:errow:1145379079877566484>\n\n"
f"<a:errow:1145379079877566484> Server Updates <a:errow:1145379079877566484>\n\n"
f"<a:errow:1145379079877566484> Chat Here <a:errow:1145379079877566484>\n\n"
f"▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬\n\n"
f":kenji_hypesquad: User <a:errow:1145379079877566484> {member.mention}\n\n"
f":kenji_hypesquad: Now Members <a:errow:1145379079877566484> {member.guild.member_count}\n\n"
f":kenji_giveaway: Thanks For Joining :kenji_giveaway:"
),
color=0x2ecc71
)
embed.set_thumbnail(url=member.display_avatar.url)
embed.set_footer(text=f"Member #{member.guild.member_count}")
await channel.send(embed=embed)
@bot.command(name='setwelcome')
@commands.has_permissions(administrator=True)
async def set_welcome(ctx, channel: discord.TextChannel):
guild_id = str(ctx.guild.id)
if "welcome_channels" not in config:
config["welcome_channels"] = {}
config["welcome_channels"][guild_id] = channel.id
save_config(config)
embed = discord.Embed(
title="✅ Welcome Channel Set",
description=f"Welcome messages will now be sent to {channel.mention}",
color=0x2ecc71
)
await ctx.send(embed=embed)
@bot.command(name='i')
async def user_info(ctx):
user = ctx.author
embed = discord.Embed(
title=f"{user.name}'s Information",
color=0x3498db
)
embed.set_thumbnail(url=user.display_avatar.url)
embed.add_field(name="Username", value=user.name, inline=True)
embed.add_field(name="Display Name", value=user.display_name, inline=True)
embed.add_field(name="User ID", value=user.id, inline=True)
embed.add_field(name="Account Created", value=user.created_at.strftime("%B %d, %Y"), inline=True)
embed.add_field(name="Joined Server", value=user.joined_at.strftime("%B %d, %Y") if user.joined_at else "Unknown", inline=True)
await ctx.send(embed=embed)
@bot.command(name='server')
async def server_info(ctx):
guild = ctx.guild
embed = discord.Embed(
title=f"{guild.name}",
description="Server Information",
color=0xe74c3c
)
if guild.icon:
embed.set_thumbnail(url=guild.icon.url)
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="Owner", value=guild.owner.mention if guild.owner else "Unknown", inline=True)
embed.add_field(name="Members", value=guild.member_count, inline=True)
embed.add_field(name="Created On", value=guild.created_at.strftime("%B %d, %Y"), inline=True)
embed.add_field(name="Roles", value=len(guild.roles), inline=True)
await ctx.send(embed=embed)
@bot.tree.command(name="server", description="Display live Minecraft server status")
@app_commands.describe(server_ip="The Minecraft server IP address")
@app_commands.checks.has_permissions(administrator=True)
async def server_status(interaction: discord.Interaction, server_ip: str):
await interaction.response.defer()
embed = await create_status_embed(server_ip)
message = await interaction.followup.send(embed=embed)
key = f"{interaction.guild_id}_{message.id}"
active_status_messages[key] = {
"message": message,
"server_ip": server_ip,
"last_update": datetime.now()
}
async def create_status_embed(server_ip: str):
try:
server = JavaServer.lookup(server_ip)
status = server.status()
online_players = status.players.online
max_players = status.players.max
motd = status.description if isinstance(status.description, str) else str(status.description)
embed = discord.Embed(
title="🎮 Minecraft Server Status",
color=0x2ecc71,
timestamp=datetime.now()
)
embed.add_field(name="🟢 State", value="Online", inline=False)
embed.add_field(name="🧟 Body Count", value=f"{online_players}/{max_players} players online", inline=False)
embed.add_field(name="📡 Java IP", value=server_ip, inline=True)
embed.add_field(name="📱 Bedrock Port", value="19132", inline=True)
embed.add_field(name="📋 MOTD", value=motd[:1024], inline=False)
if status.favicon:
icon_data = status.favicon.replace('data:image/png;base64,', '')
embed.set_thumbnail(url=f"data:image/png;base64,{icon_data}")
embed.set_footer(text="Updates every 30 seconds")
return embed
except Exception as e:
embed = discord.Embed(
title="🎮 Minecraft Server Status",
color=0xe74c3c,
timestamp=datetime.now()
)
embed.add_field(name="🔴 State", value="Offline", inline=False)
embed.add_field(name="🧟 Body Count", value="0/0 players online", inline=False)
embed.add_field(name="📡 Java IP", value=server_ip, inline=True)
embed.add_field(name="📱 Bedrock Port", value="19132", inline=True)
embed.add_field(name="❌ Error", value="Server is offline or unreachable", inline=False)
embed.set_footer(text="Updates every 30 seconds")
return embed
@tasks.loop(seconds=30)
async def update_status_messages():
current_time = datetime.now()
keys_to_remove = []
for key, data in active_status_messages.items():
try:
message = data["message"]
server_ip = data["server_ip"]
if (current_time - data["last_update"]).total_seconds() >= 60:
embed = await create_status_embed(server_ip)
await message.edit(embed=embed)
data["last_update"] = current_time
except discord.NotFound:
keys_to_remove.append(key)
except Exception as e:
print(f"Error updating status message: {e}")
for key in keys_to_remove:
del active_status_messages[key]