-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord.py
More file actions
423 lines (211 loc) · 10.1 KB
/
discord.py
File metadata and controls
423 lines (211 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# bot.py
import discord
from discord.ext import commands
from discord import app_commands
from pymongo import MongoClient
import requests
import re
from datetime import datetime, timezone
# ======================
# CONFIGURATION
# ======================
MONGO_URI = "PLACE MONGODB URI HERE"
DISCORD_TOKEN = "DISCORD BOT TOKEN"
# MongoDB
client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
db = client["mcscanner"]
servers_collection = db["servers"]
# Bot
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
# ======================
# UTILITIES
# ======================
def get_skin_url(username: str):
"""Get Minecraft player skin URL"""
try:
res = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}", timeout=5)
if res.status_code == 200:
uuid = res.json().get("id")
return f"https://crafatar.com/avatars/{uuid}?size=160&overlay"
except Exception:
pass
return None
def extract_online_players(players_str: str) -> int:
"""Extract current player count from 'X/Y' format"""
if not players_str or not isinstance(players_str, str):
return 0
match = re.match(r"(\d+)/\d+", players_str)
return int(match.group(1)) if match else 0
def create_server_embed(title: str, servers: list, color: discord.Color) -> discord.Embed:
"""Create formatted embed for server list"""
embed = discord.Embed(title=title, color=color, timestamp=datetime.now(timezone.utc))
if not servers:
embed.description = "🔭 No servers found."
embed.set_footer(text="Live server data • Updated in real-time")
return embed
for s in servers[:10]:
ip = s.get("ip", "Unknown")
port = s.get("port", 25565)
version = s.get("version", "Unknown")
players = s.get("players", "0/0")
desc = (s.get("description") or s.get("motd") or "No description").strip()
if len(desc) > 100:
desc = desc[:97] + "..."
embed.add_field(
name=f"🌐 `{ip}:{port}`",
value=f"🧩 **Version:** `{version}`\n"
f"👥 **Players:** `{players}`\n"
f"📜 **MOTD:** `{desc}`",
inline=False
)
embed.set_footer(text="Live server data • Updated in real-time")
return embed
# ======================
# EVENTS
# ======================
@bot.event
async def on_ready():
"""Bot startup event"""
await bot.tree.sync()
print(f"✅ {bot.user} is online and ready!")
print(f"📊 Connected to {len(bot.guilds)} guilds")
print(f"🗄️ Database has {servers_collection.estimated_document_count():,} servers")
# ======================
# SLASH COMMANDS
# ======================
@bot.tree.command(name="help", description="📘 View all available commands")
async def help_command(interaction: discord.Interaction):
"""Display help menu with all commands"""
await interaction.response.defer(thinking=True)
embed = discord.Embed(
title="✨ Minecraft Server Explorer",
description="Discover thousands of live Minecraft servers!",
color=discord.Color.teal()
)
embed.add_field(name="🎲 `/random`", value="Get 10 random servers", inline=False)
embed.add_field(name="🔍 `/find`", value="Search by version or MOTD keyword", inline=False)
embed.add_field(name="🏆 `/top`", value="Top 10 servers by player count", inline=False)
embed.add_field(name="📊 `/count`", value="Total number of servers", inline=False)
embed.add_field(name="🧑 `/user`", value="Get player skin & info", inline=False)
embed.add_field(name="🏓 `/ping`", value="Check bot latency", inline=False)
embed.add_field(name="🤖 `/botinfo`", value="Bot statistics", inline=False)
embed.set_thumbnail(url=bot.user.display_avatar.url)
embed.set_footer(text="Made with ❤️ for Minecraft enthusiasts")
await interaction.followup.send(embed=embed)
@bot.tree.command(name="random", description="🎲 Show 10 random Minecraft servers")
async def random_servers(interaction: discord.Interaction):
"""Get random servers from database"""
await interaction.response.defer(thinking=True)
try:
servers = list(servers_collection.aggregate([{"$sample": {"size": 10}}]))
except Exception:
servers = list(servers_collection.find().limit(10))
embed = create_server_embed("🎲 Random Minecraft Servers", servers, discord.Color.green())
await interaction.followup.send(embed=embed)
@bot.tree.command(name="find", description="🔍 Find servers by version or MOTD")
@app_commands.describe(
version="Minecraft version (e.g., '1.20.1') — use '*' for any",
motd_keyword="Keyword in server description (case-insensitive)"
)
async def find_servers(interaction: discord.Interaction, version: str = "*", motd_keyword: str = ""):
"""Search for servers matching criteria"""
await interaction.response.defer(thinking=True)
query = {}
if version != "*":
query["version"] = {"$regex": f"^{re.escape(version)}", "$options": "i"}
if motd_keyword.strip():
query["$or"] = [
{"description": {"$regex": re.escape(motd_keyword), "$options": "i"}},
{"motd": {"$regex": re.escape(motd_keyword), "$options": "i"}}
]
try:
servers = list(servers_collection.aggregate([
{"$match": query},
{"$sample": {"size": 10}}
]))
except Exception:
servers = list(servers_collection.find(query).limit(10))
title = f"🔍 Results — Version: `{version}`, MOTD: `{motd_keyword or 'Any'}`"
embed = create_server_embed(title, servers, discord.Color.blue())
await interaction.followup.send(embed=embed)
@bot.tree.command(name="count", description="📊 Show total number of servers")
async def count_servers(interaction: discord.Interaction):
"""Display total server count"""
await interaction.response.defer(thinking=True)
count = servers_collection.estimated_document_count()
embed = discord.Embed(
title="📊 Server Database Stats",
description=f"**Total Servers:** `{count:,}`",
color=discord.Color.purple(),
timestamp=datetime.now(timezone.utc)
)
embed.set_footer(text="Updated in real-time")
await interaction.followup.send(embed=embed)
@bot.tree.command(name="top", description="🏆 Top 10 servers by player count")
async def top_servers(interaction: discord.Interaction):
"""Get most populated servers"""
await interaction.response.defer(thinking=True)
servers = list(servers_collection.find({"players": {"$exists": True}}).limit(200))
servers.sort(key=lambda s: extract_online_players(s.get("players", "0/0")), reverse=True)
top_10 = servers[:10]
embed = create_server_embed("🏆 Top Servers by Player Count", top_10, discord.Color.gold())
await interaction.followup.send(embed=embed)
@bot.tree.command(name="ping", description="🏓 Check bot latency")
async def ping_command(interaction: discord.Interaction):
"""Display bot response time"""
await interaction.response.defer(thinking=True)
latency = round(bot.latency * 1000)
status = "🟢 Excellent" if latency < 100 else "🟡 Good" if latency < 200 else "🔴 High"
await interaction.followup.send(f"🏓 **Latency:** `{latency}ms` — {status}")
@bot.tree.command(name="botinfo", description="🤖 Get bot information")
async def botinfo_command(interaction: discord.Interaction):
"""Display bot statistics"""
await interaction.response.defer(thinking=True)
embed = discord.Embed(
title="🤖 Bot Information",
color=discord.Color.blurple(),
timestamp=datetime.now(timezone.utc)
)
embed.set_thumbnail(url=bot.user.display_avatar.url)
embed.add_field(name="Name", value=bot.user.name, inline=True)
embed.add_field(name="ID", value=f"`{bot.user.id}`", inline=True)
embed.add_field(name="Servers", value=f"`{len(bot.guilds)}`", inline=True)
total_members = sum(g.member_count for g in bot.guilds)
embed.add_field(name="Users", value=f"`{total_members:,}`", inline=True)
embed.add_field(name="Database", value=f"`{servers_collection.estimated_document_count():,}` servers", inline=False)
embed.set_footer(text="Powered by MongoDB • Python + Discord.py")
await interaction.followup.send(embed=embed)
@bot.tree.command(name="user", description="🧑 Get Minecraft player skin & info")
@app_commands.describe(username="Minecraft username")
async def user_command(interaction: discord.Interaction, username: str):
"""Fetch player profile and skin"""
await interaction.response.defer(thinking=True)
skin_url = get_skin_url(username)
if not skin_url:
await interaction.followup.send("❌ Player not found on Mojang servers.", ephemeral=True)
return
embed = discord.Embed(
title=f"🧑 Minecraft Profile: `{username}`",
color=discord.Color.orange(),
timestamp=datetime.now(timezone.utc)
)
embed.set_thumbnail(url=skin_url)
embed.add_field(name="Skin", value=f"[View Full Skin](https://crafatar.com/skins/{username})", inline=True)
embed.add_field(name="Head", value=f"[Download Head](https://crafatar.com/renders/head/{username})", inline=True)
embed.add_field(name="Recent Servers", value="🔭 Not tracked (requires player list storage)", inline=False)
embed.set_footer(text="Data from Mojang API • Skin by Crafatar")
await interaction.followup.send(embed=embed)
# ======================
# RUN BOT
# ======================
if __name__ == "__main__":
if DISCORD_TOKEN == "YOUR_DISCORD_BOT_TOKEN_HERE":
print("❌ ERROR: Please replace 'YOUR_DISCORD_BOT_TOKEN_HERE' with your actual Discord bot token!")
exit(1)
try:
bot.run(DISCORD_TOKEN)
except discord.LoginFailure:
print("❌ ERROR: Invalid Discord token. Please check your token and try again.")
except Exception as e:
print(f"❌ ERROR: An unexpected error occurred: {e}")