Files
.gitignore
bot.py
README.md
requirements.txt
Code
4 124
bot.py
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
import logging
load_dotenv()
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('IzaacBot')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
logger.info(f'Bot is ready! Logged in as {bot.user.name} (ID: {bot.user.id})')
logger.info(f'Connected to {len(bot.guilds)} guilds')
await bot.change_presence(activity=discord.Game(name="!help for commands"))
@bot.event
async def on_message(message):
if message.author.bot:
return
await bot.process_commands(message)
@bot.command(name='hello')
async def hello(ctx):
"""Say hello to Izaac!"""
await ctx.send(f'Hello {ctx.author.mention}! 👋')
@bot.command(name='ping')
async def ping(ctx):
"""Check bot latency"""
latency = round(bot.latency * 1000)
await ctx.send(f'Pong! 🏓 Latency: {latency}ms')
@bot.command(name='info')
async def info(ctx):
"""Get information about Izaac"""
embed = discord.Embed(
title="About Izaac",
description="A friendly Discord bot here to help!",
color=discord.Color.pink()
)
embed.add_field(name="Servers", value=str(len(bot.guilds)), inline=True)
embed.add_field(name="Prefix", value="!", 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.name}", icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
await ctx.send(embed=embed)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send("Command not found! Use `!help` to see available commands.")
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to use this command.")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"Missing required argument: {error.param.name}")
else:
logger.error(f"Error in command {ctx.command}: {error}")
await ctx.send("An error occurred while processing the command.")
if __name__ == "__main__":
token = os.getenv('DISCORD_TOKEN')
if not token:
logger.error("DISCORD_TOKEN not found in environment variables!")
raise ValueError("DISCORD_TOKEN not found in .env file")
bot.run(token)