Files
bot.py
config.json
database.py
pterodactyl.py
README.md
requirements.txt
Code
6 1,703
bot.py
import discord
from discord import app_commands
from discord.ext import commands
import os
from dotenv import load_dotenv
import asyncio
import random
import string
import json
from database import Database
from pterodactyl import PterodactylAPI
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.invites = True
bot = commands.Bot(command_prefix='!', intents=intents)
tree = bot.tree
db = Database()
ptero = PterodactylAPI(
os.getenv('PTERODACTYL_URL'),
os.getenv('PTERODACTYL_API_KEY')
)
OWNER_ID = int(os.getenv('OWNER_ID', 0))
USER_COMMAND_CHANNEL_ID = int(os.getenv('USER_COMMAND_CHANNEL_ID', 0))
ADMIN_COMMAND_CHANNEL_ID = int(os.getenv('ADMIN_COMMAND_CHANNEL_ID', 0))
ADMIN_LOG_CHANNEL_ID = int(os.getenv('ADMIN_LOG_CHANNEL_ID', 0))
minecraft_eggs_cache = []
with open('config.json', 'r') as f:
config = json.load(f)
def generate_unique_username(base_username: str) -> str:
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
return f"{base_username}_{random_suffix}"
async def log_action(title: str, description: str, color: discord.Color = discord.Color.blue(), fields: list = None):
if ADMIN_LOG_CHANNEL_ID == 0:
return
try:
channel = bot.get_channel(ADMIN_LOG_CHANNEL_ID)
if channel:
embed = discord.Embed(
title=title,
description=description,
color=color,
timestamp=discord.utils.utcnow()
)
if fields:
for field in fields:
embed.add_field(name=field['name'], value=field['value'], inline=field.get('inline', False))
await channel.send(embed=embed)
except Exception as e:
print(f"Failed to log action: {e}")
def is_owner_check(interaction: discord.Interaction) -> bool:
return interaction.user.id == OWNER_ID
def is_admin_check(interaction: discord.Interaction) -> bool:
if interaction.user.id == OWNER_ID:
return True
return db.is_admin(interaction.user.id)
def user_channel_check(interaction: discord.Interaction) -> bool:
if USER_COMMAND_CHANNEL_ID == 0:
return True
return interaction.channel_id == USER_COMMAND_CHANNEL_ID
def admin_channel_check(interaction: discord.Interaction) -> bool:
if ADMIN_COMMAND_CHANNEL_ID == 0:
return True
return interaction.channel_id == ADMIN_COMMAND_CHANNEL_ID
async def load_minecraft_eggs():
global minecraft_eggs_cache
nest_result = await ptero.get_minecraft_nest_id()
if nest_result['success']:
eggs_result = await ptero.get_eggs_from_nest(nest_result['nest_id'])
if eggs_result['success']:
minecraft_eggs_cache = eggs_result['eggs']
print(f"Loaded {len(minecraft_eggs_cache)} Minecraft eggs")
else:
print(f"Failed to load eggs: {eggs_result['error']}")
else:
print(f"Failed to find Minecraft nest: {nest_result['error']}")
async def egg_autocomplete(
interaction: discord.Interaction,
current: str,
) -> list[app_commands.Choice[str]]:
if not minecraft_eggs_cache:
await load_minecraft_eggs()
choices = []
for egg in minecraft_eggs_cache:
egg_name = egg['name']
if current.lower() in egg_name.lower():
choices.append(app_commands.Choice(name=egg_name, value=egg_name))
return choices[:25]
@bot.event
async def on_ready():
await load_minecraft_eggs()
try:
synced = await bot.tree.sync()
print(f'Synced {len(synced)} slash commands')
except Exception as e:
print(f'Failed to sync commands: {e}')
print(f'Bot is ready! Logged in as {bot.user}')
bot.loop.create_task(track_invites())
await log_action(
"๐ข Bot Started",
f"Bot {bot.user.name} is now online and ready",
discord.Color.green()
)
async def track_invites():
await bot.wait_until_ready()
for guild in bot.guilds:
try:
invites = await guild.invites()
for invite in invites:
if invite.inviter:
db.cache_invite(invite.code, invite.uses)
except:
pass
@bot.event
async def on_invite_create(invite):
db.cache_invite(invite.code, invite.uses)
@bot.event
async def on_member_join(member):
await asyncio.sleep(1)
try:
invites_after = await member.guild.invites()
for invite in invites_after:
cached_uses = db.get_cached_invite_uses(invite.code)
if invite.uses > cached_uses:
if invite.inviter and invite.inviter.id != member.id:
if not db.has_invited_user(invite.inviter.id, member.id):
db.add_invites(invite.inviter.id, 1)
db.add_coins(invite.inviter.id, 10)
db.record_invite(invite.inviter.id, member.id)
db.cache_invite(invite.code, invite.uses)
await log_action(
"๐จ Invite Used",
f"{member.mention} joined using {invite.inviter.mention}'s invite",
discord.Color.blue(),
[
{'name': 'Inviter', 'value': invite.inviter.mention, 'inline': True},
{'name': 'New Member', 'value': member.mention, 'inline': True},
{'name': 'Reward', 'value': '1 invite (10 coins)', 'inline': True}
]
)
try:
await invite.inviter.send(
f'โ
You received 1 invite (10 coins)! {member.mention} joined using your invite.\n'
f'Total invites: {db.get_invites(invite.inviter.id)} | Total coins: {db.get_coins(invite.inviter.id)}'
)
except:
pass
break
except Exception as e:
print(f"Error tracking invite: {e}")
@bot.tree.command(name="help", description="Show all available commands")
async def help_command(interaction: discord.Interaction):
is_owner = is_owner_check(interaction)
is_admin = is_admin_check(interaction)
embed = discord.Embed(
title="๐ Bot Commands Help",
description="All available commands organized by permission level",
color=discord.Color.blue()
)
user_commands = """
`/help` - Show this help message
`/invites` - Check your invite count
`/coins` - Check your coin balance
`/shop` - View available server upgrades
`/buy <upgrade>` - Purchase a server upgrade
`/myresources` - View your purchased resources
`/createserver <egg_name> <email> <password>` - Create a new Minecraft server