Update main.py
Browse files
main.py
CHANGED
|
@@ -4,6 +4,8 @@ import os
|
|
| 4 |
import asyncio
|
| 5 |
from datetime import datetime
|
| 6 |
import logging
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Setup logging
|
| 9 |
logging.basicConfig(
|
|
@@ -12,6 +14,20 @@ logging.basicConfig(
|
|
| 12 |
)
|
| 13 |
logger = logging.getLogger('discord_bot')
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Bot configuration
|
| 16 |
TOKEN = os.getenv('DISCORD_TOKEN')
|
| 17 |
if not TOKEN:
|
|
@@ -34,8 +50,8 @@ bot = commands.Bot(
|
|
| 34 |
# Event: Bot ready
|
| 35 |
@bot.event
|
| 36 |
async def on_ready():
|
| 37 |
-
logger.info(f'
|
| 38 |
-
logger.info(f'
|
| 39 |
|
| 40 |
# Set bot status
|
| 41 |
await bot.change_presence(
|
|
@@ -63,12 +79,12 @@ async def status_update():
|
|
| 63 |
# Event: When bot joins a new guild
|
| 64 |
@bot.event
|
| 65 |
async def on_guild_join(guild):
|
| 66 |
-
logger.info(f'
|
| 67 |
|
| 68 |
# Event: When bot leaves a guild
|
| 69 |
@bot.event
|
| 70 |
async def on_guild_remove(guild):
|
| 71 |
-
logger.info(f'
|
| 72 |
|
| 73 |
# Event: Handle messages (for non-command messages)
|
| 74 |
@bot.event
|
|
@@ -91,7 +107,7 @@ async def help_command(ctx, command_name: str = None):
|
|
| 91 |
command = bot.get_command(command_name)
|
| 92 |
if command:
|
| 93 |
embed = discord.Embed(
|
| 94 |
-
title=f"
|
| 95 |
description=command.help or "No description available",
|
| 96 |
color=discord.Color.blue()
|
| 97 |
)
|
|
@@ -108,11 +124,11 @@ async def help_command(ctx, command_name: str = None):
|
|
| 108 |
)
|
| 109 |
await ctx.send(embed=embed)
|
| 110 |
else:
|
| 111 |
-
await ctx.send(f"
|
| 112 |
else:
|
| 113 |
# Show all commands
|
| 114 |
embed = discord.Embed(
|
| 115 |
-
title="
|
| 116 |
description="Here are all available commands:",
|
| 117 |
color=discord.Color.blue(),
|
| 118 |
timestamp=datetime.now()
|
|
@@ -133,7 +149,7 @@ async def help_command(ctx, command_name: str = None):
|
|
| 133 |
valid_cmds.append(f"`!{cmd.name}`")
|
| 134 |
if valid_cmds:
|
| 135 |
embed.add_field(
|
| 136 |
-
name=f"
|
| 137 |
value=" | ".join(valid_cmds),
|
| 138 |
inline=False
|
| 139 |
)
|
|
@@ -148,7 +164,7 @@ async def ping_command(ctx):
|
|
| 148 |
latency = round(bot.latency * 1000)
|
| 149 |
|
| 150 |
embed = discord.Embed(
|
| 151 |
-
title="
|
| 152 |
description=f"**Latency:** `{latency}ms`\n**API Latency:** `{round(bot.latency * 1000)}ms`",
|
| 153 |
color=discord.Color.green() if latency < 200 else discord.Color.orange() if latency < 500 else discord.Color.red()
|
| 154 |
)
|
|
@@ -160,7 +176,7 @@ async def ping_command(ctx):
|
|
| 160 |
async def info_command(ctx):
|
| 161 |
"""Show bot information"""
|
| 162 |
embed = discord.Embed(
|
| 163 |
-
title="
|
| 164 |
description="A Discord bot powered by discord.py",
|
| 165 |
color=discord.Color.blue(),
|
| 166 |
timestamp=datetime.now()
|
|
@@ -176,7 +192,7 @@ async def info_command(ctx):
|
|
| 176 |
embed.add_field(name="Uptime", value=get_uptime(), inline=True)
|
| 177 |
|
| 178 |
embed.set_thumbnail(url=bot.user.avatar.url if bot.user.avatar else bot.user.default_avatar.url)
|
| 179 |
-
embed.set_footer(text="Made with
|
| 180 |
|
| 181 |
await ctx.send(embed=embed)
|
| 182 |
|
|
@@ -187,7 +203,7 @@ async def serverinfo_command(ctx):
|
|
| 187 |
guild = ctx.guild
|
| 188 |
|
| 189 |
embed = discord.Embed(
|
| 190 |
-
title=f"
|
| 191 |
color=discord.Color.blue(),
|
| 192 |
timestamp=datetime.now()
|
| 193 |
)
|
|
@@ -212,7 +228,7 @@ async def userinfo_command(ctx, member: discord.Member = None):
|
|
| 212 |
member = member or ctx.author
|
| 213 |
|
| 214 |
embed = discord.Embed(
|
| 215 |
-
title=f"
|
| 216 |
color=member.color if member.color != discord.Color.default() else discord.Color.blue(),
|
| 217 |
timestamp=datetime.now()
|
| 218 |
)
|
|
@@ -242,7 +258,7 @@ async def avatar_command(ctx, member: discord.Member = None):
|
|
| 242 |
member = member or ctx.author
|
| 243 |
|
| 244 |
embed = discord.Embed(
|
| 245 |
-
title=f"
|
| 246 |
color=discord.Color.blue()
|
| 247 |
)
|
| 248 |
|
|
@@ -276,7 +292,7 @@ async def embed_command(ctx, *, message: str):
|
|
| 276 |
async def poll_command(ctx, *, question: str):
|
| 277 |
"""Create a poll"""
|
| 278 |
embed = discord.Embed(
|
| 279 |
-
title="
|
| 280 |
description=question,
|
| 281 |
color=discord.Color.blue(),
|
| 282 |
timestamp=datetime.now()
|
|
@@ -284,9 +300,9 @@ async def poll_command(ctx, *, question: str):
|
|
| 284 |
embed.set_footer(text=f"Poll by {ctx.author.name}")
|
| 285 |
|
| 286 |
poll_message = await ctx.send(embed=embed)
|
| 287 |
-
await poll_message.add_reaction("
|
| 288 |
-
await poll_message.add_reaction("
|
| 289 |
-
await poll_message.add_reaction("
|
| 290 |
|
| 291 |
# 8Ball Command
|
| 292 |
@bot.command(name='8ball', aliases=['magic8ball', 'ask'])
|
|
@@ -295,30 +311,30 @@ async def eightball_command(ctx, *, question: str):
|
|
| 295 |
import random
|
| 296 |
|
| 297 |
responses = [
|
| 298 |
-
"
|
| 299 |
-
"
|
| 300 |
-
"
|
| 301 |
-
"
|
| 302 |
-
"
|
| 303 |
-
"
|
| 304 |
-
"
|
| 305 |
-
"
|
| 306 |
-
"
|
| 307 |
-
"
|
| 308 |
-
"
|
| 309 |
-
"
|
| 310 |
-
"
|
| 311 |
-
"
|
| 312 |
-
"
|
| 313 |
-
"
|
| 314 |
-
"
|
| 315 |
-
"
|
| 316 |
-
"
|
| 317 |
-
"
|
| 318 |
]
|
| 319 |
|
| 320 |
embed = discord.Embed(
|
| 321 |
-
title="
|
| 322 |
color=discord.Color.purple()
|
| 323 |
)
|
| 324 |
embed.add_field(name="Question", value=question, inline=False)
|
|
@@ -333,7 +349,7 @@ async def coinflip_command(ctx):
|
|
| 333 |
import random
|
| 334 |
|
| 335 |
result = random.choice(["Heads", "Tails"])
|
| 336 |
-
emoji = "
|
| 337 |
|
| 338 |
embed = discord.Embed(
|
| 339 |
title=f"{emoji} Coin Flip",
|
|
@@ -363,7 +379,7 @@ async def invite_command(ctx):
|
|
| 363 |
invite_url = discord.utils.oauth_url(bot.user.id, permissions=permissions)
|
| 364 |
|
| 365 |
embed = discord.Embed(
|
| 366 |
-
title="
|
| 367 |
description=f"[Click here to invite me!]({invite_url})",
|
| 368 |
color=discord.Color.green()
|
| 369 |
)
|
|
@@ -377,12 +393,12 @@ async def invite_command(ctx):
|
|
| 377 |
async def clear_command(ctx, amount: int = 5):
|
| 378 |
"""Clear messages (requires Manage Messages permission)"""
|
| 379 |
if amount < 1 or amount > 100:
|
| 380 |
-
await ctx.send("
|
| 381 |
return
|
| 382 |
|
| 383 |
deleted = await ctx.channel.purge(limit=amount + 1)
|
| 384 |
embed = discord.Embed(
|
| 385 |
-
description=f"
|
| 386 |
color=discord.Color.green()
|
| 387 |
)
|
| 388 |
msg = await ctx.send(embed=embed)
|
|
@@ -394,7 +410,7 @@ async def clear_command(ctx, amount: int = 5):
|
|
| 394 |
async def uptime_command(ctx):
|
| 395 |
"""Show bot uptime"""
|
| 396 |
embed = discord.Embed(
|
| 397 |
-
title="
|
| 398 |
description=get_uptime(),
|
| 399 |
color=discord.Color.blue()
|
| 400 |
)
|
|
@@ -408,7 +424,7 @@ async def on_command_error(ctx, error):
|
|
| 408 |
|
| 409 |
elif isinstance(error, commands.MissingRequiredArgument):
|
| 410 |
embed = discord.Embed(
|
| 411 |
-
title="
|
| 412 |
description=f"Missing required argument: `{error.param.name}`\nUse `!help {ctx.command.name}` for usage info.",
|
| 413 |
color=discord.Color.red()
|
| 414 |
)
|
|
@@ -416,7 +432,7 @@ async def on_command_error(ctx, error):
|
|
| 416 |
|
| 417 |
elif isinstance(error, commands.MissingPermissions):
|
| 418 |
embed = discord.Embed(
|
| 419 |
-
title="
|
| 420 |
description=f"You need `{', '.join(error.missing_permissions)}` permission to use this command.",
|
| 421 |
color=discord.Color.red()
|
| 422 |
)
|
|
@@ -424,7 +440,7 @@ async def on_command_error(ctx, error):
|
|
| 424 |
|
| 425 |
elif isinstance(error, commands.MemberNotFound):
|
| 426 |
embed = discord.Embed(
|
| 427 |
-
title="
|
| 428 |
description="The specified user was not found.",
|
| 429 |
color=discord.Color.red()
|
| 430 |
)
|
|
@@ -433,7 +449,7 @@ async def on_command_error(ctx, error):
|
|
| 433 |
else:
|
| 434 |
logger.error(f"Command error: {error}")
|
| 435 |
embed = discord.Embed(
|
| 436 |
-
title="
|
| 437 |
description="An unexpected error occurred. Please try again later.",
|
| 438 |
color=discord.Color.red()
|
| 439 |
)
|
|
@@ -465,4 +481,4 @@ if __name__ == "__main__":
|
|
| 465 |
bot.run(TOKEN)
|
| 466 |
except Exception as e:
|
| 467 |
logger.error(f"Failed to start bot: {e}")
|
| 468 |
-
raise
|
|
|
|
| 4 |
import asyncio
|
| 5 |
from datetime import datetime
|
| 6 |
import logging
|
| 7 |
+
from flask import Flask
|
| 8 |
+
from threading import Thread
|
| 9 |
|
| 10 |
# Setup logging
|
| 11 |
logging.basicConfig(
|
|
|
|
| 14 |
)
|
| 15 |
logger = logging.getLogger('discord_bot')
|
| 16 |
|
| 17 |
+
# Flask web server
|
| 18 |
+
app = Flask(__name__)
|
| 19 |
+
|
| 20 |
+
@app.route('/')
|
| 21 |
+
def home():
|
| 22 |
+
return "hey, bot started✅"
|
| 23 |
+
|
| 24 |
+
def run_web():
|
| 25 |
+
app.run(host='0.0.0.0', port=7860)
|
| 26 |
+
|
| 27 |
+
# Start web server in a thread
|
| 28 |
+
web_thread = Thread(target=run_web)
|
| 29 |
+
web_thread.start()
|
| 30 |
+
|
| 31 |
# Bot configuration
|
| 32 |
TOKEN = os.getenv('DISCORD_TOKEN')
|
| 33 |
if not TOKEN:
|
|
|
|
| 50 |
# Event: Bot ready
|
| 51 |
@bot.event
|
| 52 |
async def on_ready():
|
| 53 |
+
logger.info(f'✅ Bot logged in as {bot.user.name} ({bot.user.id})')
|
| 54 |
+
logger.info(f'🌐 Connected to {len(bot.guilds)} guilds')
|
| 55 |
|
| 56 |
# Set bot status
|
| 57 |
await bot.change_presence(
|
|
|
|
| 79 |
# Event: When bot joins a new guild
|
| 80 |
@bot.event
|
| 81 |
async def on_guild_join(guild):
|
| 82 |
+
logger.info(f'➕ Joined guild: {guild.name} ({guild.id})')
|
| 83 |
|
| 84 |
# Event: When bot leaves a guild
|
| 85 |
@bot.event
|
| 86 |
async def on_guild_remove(guild):
|
| 87 |
+
logger.info(f'➖ Left guild: {guild.name} ({guild.id})')
|
| 88 |
|
| 89 |
# Event: Handle messages (for non-command messages)
|
| 90 |
@bot.event
|
|
|
|
| 107 |
command = bot.get_command(command_name)
|
| 108 |
if command:
|
| 109 |
embed = discord.Embed(
|
| 110 |
+
title=f"📖 Help: !{command.name}",
|
| 111 |
description=command.help or "No description available",
|
| 112 |
color=discord.Color.blue()
|
| 113 |
)
|
|
|
|
| 124 |
)
|
| 125 |
await ctx.send(embed=embed)
|
| 126 |
else:
|
| 127 |
+
await ctx.send(f"❌ Command `!{command_name}` not found!")
|
| 128 |
else:
|
| 129 |
# Show all commands
|
| 130 |
embed = discord.Embed(
|
| 131 |
+
title="🤖 Bot Commands",
|
| 132 |
description="Here are all available commands:",
|
| 133 |
color=discord.Color.blue(),
|
| 134 |
timestamp=datetime.now()
|
|
|
|
| 149 |
valid_cmds.append(f"`!{cmd.name}`")
|
| 150 |
if valid_cmds:
|
| 151 |
embed.add_field(
|
| 152 |
+
name=f"📁 {category}",
|
| 153 |
value=" | ".join(valid_cmds),
|
| 154 |
inline=False
|
| 155 |
)
|
|
|
|
| 164 |
latency = round(bot.latency * 1000)
|
| 165 |
|
| 166 |
embed = discord.Embed(
|
| 167 |
+
title="🏓 Pong!",
|
| 168 |
description=f"**Latency:** `{latency}ms`\n**API Latency:** `{round(bot.latency * 1000)}ms`",
|
| 169 |
color=discord.Color.green() if latency < 200 else discord.Color.orange() if latency < 500 else discord.Color.red()
|
| 170 |
)
|
|
|
|
| 176 |
async def info_command(ctx):
|
| 177 |
"""Show bot information"""
|
| 178 |
embed = discord.Embed(
|
| 179 |
+
title="🤖 Bot Information",
|
| 180 |
description="A Discord bot powered by discord.py",
|
| 181 |
color=discord.Color.blue(),
|
| 182 |
timestamp=datetime.now()
|
|
|
|
| 192 |
embed.add_field(name="Uptime", value=get_uptime(), inline=True)
|
| 193 |
|
| 194 |
embed.set_thumbnail(url=bot.user.avatar.url if bot.user.avatar else bot.user.default_avatar.url)
|
| 195 |
+
embed.set_footer(text="Made with ❤️ using discord.py")
|
| 196 |
|
| 197 |
await ctx.send(embed=embed)
|
| 198 |
|
|
|
|
| 203 |
guild = ctx.guild
|
| 204 |
|
| 205 |
embed = discord.Embed(
|
| 206 |
+
title=f"📊 {guild.name} Server Info",
|
| 207 |
color=discord.Color.blue(),
|
| 208 |
timestamp=datetime.now()
|
| 209 |
)
|
|
|
|
| 228 |
member = member or ctx.author
|
| 229 |
|
| 230 |
embed = discord.Embed(
|
| 231 |
+
title=f"👤 {member.name}'s Profile",
|
| 232 |
color=member.color if member.color != discord.Color.default() else discord.Color.blue(),
|
| 233 |
timestamp=datetime.now()
|
| 234 |
)
|
|
|
|
| 258 |
member = member or ctx.author
|
| 259 |
|
| 260 |
embed = discord.Embed(
|
| 261 |
+
title=f"🖼️ {member.name}'s Avatar",
|
| 262 |
color=discord.Color.blue()
|
| 263 |
)
|
| 264 |
|
|
|
|
| 292 |
async def poll_command(ctx, *, question: str):
|
| 293 |
"""Create a poll"""
|
| 294 |
embed = discord.Embed(
|
| 295 |
+
title="📊 Poll",
|
| 296 |
description=question,
|
| 297 |
color=discord.Color.blue(),
|
| 298 |
timestamp=datetime.now()
|
|
|
|
| 300 |
embed.set_footer(text=f"Poll by {ctx.author.name}")
|
| 301 |
|
| 302 |
poll_message = await ctx.send(embed=embed)
|
| 303 |
+
await poll_message.add_reaction("👍")
|
| 304 |
+
await poll_message.add_reaction("👎")
|
| 305 |
+
await poll_message.add_reaction("🤷")
|
| 306 |
|
| 307 |
# 8Ball Command
|
| 308 |
@bot.command(name='8ball', aliases=['magic8ball', 'ask'])
|
|
|
|
| 311 |
import random
|
| 312 |
|
| 313 |
responses = [
|
| 314 |
+
"🎱 It is certain.",
|
| 315 |
+
"🎱 It is decidedly so.",
|
| 316 |
+
"🎱 Without a doubt.",
|
| 317 |
+
"🎱 Yes definitely.",
|
| 318 |
+
"🎱 You may rely on it.",
|
| 319 |
+
"🎱 As I see it, yes.",
|
| 320 |
+
"🎱 Most likely.",
|
| 321 |
+
"🎱 Outlook good.",
|
| 322 |
+
"🎱 Yes.",
|
| 323 |
+
"🎱 Signs point to yes.",
|
| 324 |
+
"🎱 Reply hazy, try again.",
|
| 325 |
+
"🎱 Ask again later.",
|
| 326 |
+
"🎱 Better not tell you now.",
|
| 327 |
+
"🎱 Cannot predict now.",
|
| 328 |
+
"🎱 Concentrate and ask again.",
|
| 329 |
+
"🎱 Don't count on it.",
|
| 330 |
+
"🎱 My reply is no.",
|
| 331 |
+
"🎱 My sources say no.",
|
| 332 |
+
"🎱 Outlook not so good.",
|
| 333 |
+
"🎱 Very doubtful."
|
| 334 |
]
|
| 335 |
|
| 336 |
embed = discord.Embed(
|
| 337 |
+
title="🎱 Magic 8-Ball",
|
| 338 |
color=discord.Color.purple()
|
| 339 |
)
|
| 340 |
embed.add_field(name="Question", value=question, inline=False)
|
|
|
|
| 349 |
import random
|
| 350 |
|
| 351 |
result = random.choice(["Heads", "Tails"])
|
| 352 |
+
emoji = "🪙" if result == "Heads" else "🪙"
|
| 353 |
|
| 354 |
embed = discord.Embed(
|
| 355 |
title=f"{emoji} Coin Flip",
|
|
|
|
| 379 |
invite_url = discord.utils.oauth_url(bot.user.id, permissions=permissions)
|
| 380 |
|
| 381 |
embed = discord.Embed(
|
| 382 |
+
title="🔗 Invite Bot",
|
| 383 |
description=f"[Click here to invite me!]({invite_url})",
|
| 384 |
color=discord.Color.green()
|
| 385 |
)
|
|
|
|
| 393 |
async def clear_command(ctx, amount: int = 5):
|
| 394 |
"""Clear messages (requires Manage Messages permission)"""
|
| 395 |
if amount < 1 or amount > 100:
|
| 396 |
+
await ctx.send("❌ Please specify a number between 1 and 100!")
|
| 397 |
return
|
| 398 |
|
| 399 |
deleted = await ctx.channel.purge(limit=amount + 1)
|
| 400 |
embed = discord.Embed(
|
| 401 |
+
description=f"🗑️ Deleted {len(deleted) - 1} messages!",
|
| 402 |
color=discord.Color.green()
|
| 403 |
)
|
| 404 |
msg = await ctx.send(embed=embed)
|
|
|
|
| 410 |
async def uptime_command(ctx):
|
| 411 |
"""Show bot uptime"""
|
| 412 |
embed = discord.Embed(
|
| 413 |
+
title="⏱️ Bot Uptime",
|
| 414 |
description=get_uptime(),
|
| 415 |
color=discord.Color.blue()
|
| 416 |
)
|
|
|
|
| 424 |
|
| 425 |
elif isinstance(error, commands.MissingRequiredArgument):
|
| 426 |
embed = discord.Embed(
|
| 427 |
+
title="❌ Missing Argument",
|
| 428 |
description=f"Missing required argument: `{error.param.name}`\nUse `!help {ctx.command.name}` for usage info.",
|
| 429 |
color=discord.Color.red()
|
| 430 |
)
|
|
|
|
| 432 |
|
| 433 |
elif isinstance(error, commands.MissingPermissions):
|
| 434 |
embed = discord.Embed(
|
| 435 |
+
title="❌ Missing Permissions",
|
| 436 |
description=f"You need `{', '.join(error.missing_permissions)}` permission to use this command.",
|
| 437 |
color=discord.Color.red()
|
| 438 |
)
|
|
|
|
| 440 |
|
| 441 |
elif isinstance(error, commands.MemberNotFound):
|
| 442 |
embed = discord.Embed(
|
| 443 |
+
title="❌ User Not Found",
|
| 444 |
description="The specified user was not found.",
|
| 445 |
color=discord.Color.red()
|
| 446 |
)
|
|
|
|
| 449 |
else:
|
| 450 |
logger.error(f"Command error: {error}")
|
| 451 |
embed = discord.Embed(
|
| 452 |
+
title="❌ Error",
|
| 453 |
description="An unexpected error occurred. Please try again later.",
|
| 454 |
color=discord.Color.red()
|
| 455 |
)
|
|
|
|
| 481 |
bot.run(TOKEN)
|
| 482 |
except Exception as e:
|
| 483 |
logger.error(f"Failed to start bot: {e}")
|
| 484 |
+
raise
|