Files
cogs
utils
config.py
main.py
requirements.txt
SETUP_GUIDE.txt
Code
9 784
main.py
import discord
from discord.ext import commands
import asyncio
import logging
from config import DISCORD_TOKEN, COMMAND_PREFIX
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('discord_music_bot')
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
intents.guilds = True
bot = commands.Bot(
command_prefix=COMMAND_PREFIX,
intents=intents,
help_command=commands.DefaultHelpCommand()
)
@bot.event
async def on_ready():
logger.info(f'Logged in as {bot.user.name} (ID: {bot.user.id})')
logger.info(f'Connected to {len(bot.guilds)} guilds')
logger.info('Bot is ready!')
try:
synced = await bot.tree.sync()
logger.info(f'Synced {len(synced)} command(s)')
except Exception as e:
logger.error(f'Failed to sync commands: {e}')
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name=f"{COMMAND_PREFIX}play | {len(bot.guilds)} servers"
)
)
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(
title="❌ Missing Argument",
description=f"Missing required argument: `{error.param.name}`\n\nUse `{COMMAND_PREFIX}help {ctx.command}` for more info.",
color=discord.Color.red()
)
await ctx.send(embed=embed)
elif isinstance(error, commands.BadArgument):
embed = discord.Embed(
title="❌ Invalid Argument",
description=f"Invalid argument provided.\n\nUse `{COMMAND_PREFIX}help {ctx.command}` for more info.",
color=discord.Color.red()
)
await ctx.send(embed=embed)
else:
logger.error(f'Error in command {ctx.command}: {error}')
embed = discord.Embed(
title="❌ Error",
description=f"An unexpected error occurred:\n```{str(error)}```",
color=discord.Color.red()
)
await ctx.send(embed=embed)
@bot.event
async def on_guild_join(guild: discord.Guild):
logger.info(f'Joined guild: {guild.name} (ID: {guild.id})')
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name=f"{COMMAND_PREFIX}play | {len(bot.guilds)} servers"
)
)
@bot.event
async def on_guild_remove(guild: discord.Guild):
logger.info(f'Left guild: {guild.name} (ID: {guild.id})')
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name=f"{COMMAND_PREFIX}play | {len(bot.guilds)} servers"
)
)
async def load_extensions():
try:
await bot.load_extension('cogs.music')
logger.info('Loaded music cog')
except Exception as e:
logger.error(f'Failed to load music cog: {e}')
async def main():
async with bot:
await load_extensions()
await bot.start(DISCORD_TOKEN)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info('Bot shutdown requested')
except Exception as e:
logger.error(f'Fatal error: {e}')