Spaces:
Paused

wpms commited on
Commit
694996d
·
verified ·
1 Parent(s): 8984d7f

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +16 -0
  2. main.py +257 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy bot code
10
+ COPY . .
11
+
12
+ # Expose port for Hugging Face
13
+ EXPOSE 7860
14
+
15
+ # Run bot
16
+ CMD ["python", "main.py"]
main.py ADDED
@@ -0,0 +1,257 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ from discord.ext import commands
3
+ import os
4
+ import asyncio
5
+ from flask import Flask
6
+ from threading import Thread
7
+
8
+ # Bot setup
9
+ intents = discord.Intents.default()
10
+ intents.message_content = True
11
+ intents.members = True
12
+ intents.presences = True
13
+
14
+ bot = commands.Bot(command_prefix="!", intents=intents, help_command=None)
15
+
16
+ # Keep-alive server for Hugging Face
17
+ app = Flask(__name__)
18
+
19
+ @app.route('/')
20
+ def home():
21
+ return f"""
22
+ <html>
23
+ <head><title>{bot.user.name if bot.user else 'Discord Bot'} - Status</title></head>
24
+ <body style="font-family: Arial; padding: 40px; background: #1a1a2e; color: white;">
25
+ <h1>🤖 Discord Bot Status</h1>
26
+ <p><strong>Status:</strong> {'Online ✅' if bot.is_ready() else 'Connecting...'}</p>
27
+ <p><strong>Bot Name:</strong> {bot.user.name if bot.user else 'Loading...'}</p>
28
+ <p><strong>Latency:</strong> {round(bot.latency * 1000, 2) if bot.is_ready() else '...'} ms</p>
29
+ <p><strong>Servers:</strong> {len(bot.guilds) if bot.is_ready() else '...'}</p>
30
+ <hr>
31
+ <h3>Commands:</h3>
32
+ <ul>
33
+ <li><code>!ping</code> - Check bot latency</li>
34
+ <li><code>!server</code> - Server info</li>
35
+ <li><code>!user @mention</code> - User info</li>
36
+ <li><code>!clear [amount]</code> - Clear messages</li>
37
+ <li><code>!say [message]</code> - Bot says something</li>
38
+ <li><code>!embed [title] | [description]</code> - Create embed</li>
39
+ <li><code>!poll [question]</code> - Create poll</li>
40
+ <li><code>!avatar @mention</code> - Get user avatar</li>
41
+ <li><code>!help</code> - Show all commands</li>
42
+ </ul>
43
+ </body>
44
+ </html>
45
+ """
46
+
47
+ @app.route('/health')
48
+ def health():
49
+ return {"status": "healthy", "bot_ready": bot.is_ready()}, 200
50
+
51
+ def run_flask():
52
+ app.run(host='0.0.0.0', port=7860)
53
+
54
+ # Event: Bot ready
55
+ @bot.event
56
+ async def on_ready():
57
+ print(f'✅ Logged in as {bot.user.name} ({bot.user.id})')
58
+ print(f'📊 Connected to {len(bot.guilds)} servers')
59
+ await bot.change_presence(
60
+ activity=discord.Activity(
61
+ type=discord.ActivityType.watching,
62
+ name="!help for commands"
63
+ )
64
+ )
65
+
66
+ # Event: Member join
67
+ @bot.event
68
+ async def on_member_join(member):
69
+ channel = discord.utils.get(member.guild.text_channels, name='general')
70
+ if channel:
71
+ embed = discord.Embed(
72
+ title="Welcome! 🎉",
73
+ description=f"Hey {member.mention}, welcome to **{member.guild.name}**!",
74
+ color=discord.Color.green()
75
+ )
76
+ embed.set_thumbnail(url=member.avatar.url if member.avatar else member.default_avatar.url)
77
+ await channel.send(embed=embed)
78
+
79
+ # Event: Message
80
+ @bot.event
81
+ async def on_message(message):
82
+ if message.author.bot:
83
+ return
84
+
85
+ # Auto-react to mentions
86
+ if bot.user in message.mentions:
87
+ await message.add_reaction('👋')
88
+
89
+ await bot.process_commands(message)
90
+
91
+ # ============ COMMANDS ============
92
+
93
+ @bot.command()
94
+ async def ping(ctx):
95
+ """Check bot latency"""
96
+ latency = round(bot.latency * 1000, 2)
97
+ embed = discord.Embed(
98
+ title="🏓 Pong!",
99
+ description=f"Latency: **{latency}ms**",
100
+ color=discord.Color.blue()
101
+ )
102
+ await ctx.send(embed=embed)
103
+
104
+ @bot.command()
105
+ async def server(ctx):
106
+ """Show server information"""
107
+ guild = ctx.guild
108
+ embed = discord.Embed(
109
+ title=f"📊 {guild.name} Info",
110
+ color=discord.Color.purple()
111
+ )
112
+ embed.add_field(name="Owner", value=guild.owner.mention, inline=True)
113
+ embed.add_field(name="Members", value=guild.member_count, inline=True)
114
+ embed.add_field(name="Channels", value=len(guild.channels), inline=True)
115
+ embed.add_field(name="Roles", value=len(guild.roles), inline=True)
116
+ embed.add_field(name="Created", value=guild.created_at.strftime("%Y-%m-%d"), inline=True)
117
+ embed.add_field(name="Boosts", value=guild.premium_subscription_count or 0, inline=True)
118
+ if guild.icon:
119
+ embed.set_thumbnail(url=guild.icon.url)
120
+ await ctx.send(embed=embed)
121
+
122
+ @bot.command()
123
+ async def user(ctx, member: discord.Member = None):
124
+ """Show user information"""
125
+ member = member or ctx.author
126
+ embed = discord.Embed(
127
+ title=f"👤 {member.name} Info",
128
+ color=member.color if member.color != discord.Color.default() else discord.Color.blue()
129
+ )
130
+ embed.add_field(name="ID", value=member.id, inline=True)
131
+ embed.add_field(name="Nickname", value=member.nick or "None", inline=True)
132
+ embed.add_field(name="Status", value=str(member.status).title(), inline=True)
133
+ embed.add_field(name="Joined Server", value=member.joined_at.strftime("%Y-%m-%d") if member.joined_at else "Unknown", inline=True)
134
+ embed.add_field(name="Account Created", value=member.created_at.strftime("%Y-%m-%d"), inline=True)
135
+ embed.add_field(name="Top Role", value=member.top_role.mention, inline=True)
136
+ embed.set_thumbnail(url=member.avatar.url if member.avatar else member.default_avatar.url)
137
+ await ctx.send(embed=embed)
138
+
139
+ @bot.command()
140
+ @commands.has_permissions(manage_messages=True)
141
+ async def clear(ctx, amount: int = 5):
142
+ """Clear messages (default: 5)"""
143
+ if amount < 1 or amount > 100:
144
+ await ctx.send("❌ Please specify a number between 1 and 100!")
145
+ return
146
+
147
+ deleted = await ctx.channel.purge(limit=amount + 1)
148
+ embed = discord.Embed(
149
+ description=f"🗑️ Deleted **{len(deleted)-1}** messages!",
150
+ color=discord.Color.red()
151
+ )
152
+ msg = await ctx.send(embed=embed)
153
+ await asyncio.sleep(3)
154
+ await msg.delete()
155
+
156
+ @bot.command()
157
+ async def say(ctx, *, message):
158
+ """Make the bot say something"""
159
+ await ctx.message.delete()
160
+ await ctx.send(message)
161
+
162
+ @bot.command()
163
+ async def embed(ctx, *, content):
164
+ """Create a custom embed. Usage: !embed Title | Description"""
165
+ parts = content.split("|", 1)
166
+ title = parts[0].strip()
167
+ description = parts[1].strip() if len(parts) > 1 else ""
168
+
169
+ embed = discord.Embed(
170
+ title=title,
171
+ description=description,
172
+ color=discord.Color.random()
173
+ )
174
+ embed.set_footer(text=f"Created by {ctx.author.name}", icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
175
+ await ctx.send(embed=embed)
176
+
177
+ @bot.command()
178
+ async def poll(ctx, *, question):
179
+ """Create a poll"""
180
+ embed = discord.Embed(
181
+ title="📊 Poll",
182
+ description=f"**{question}**",
183
+ color=discord.Color.gold()
184
+ )
185
+ embed.set_footer(text=f"Poll by {ctx.author.name}")
186
+ msg = await ctx.send(embed=embed)
187
+ await msg.add_reaction('👍')
188
+ await msg.add_reaction('👎')
189
+ await msg.add_reaction('🤷')
190
+
191
+ @bot.command()
192
+ async def avatar(ctx, member: discord.Member = None):
193
+ """Get user avatar"""
194
+ member = member or ctx.author
195
+ embed = discord.Embed(
196
+ title=f"🖼️ {member.name}'s Avatar",
197
+ color=discord.Color.blue()
198
+ )
199
+ avatar_url = member.avatar.url if member.avatar else member.default_avatar.url
200
+ embed.set_image(url=avatar_url)
201
+ embed.add_field(name="Download", value=f"[Click Here]({avatar_url})", inline=False)
202
+ await ctx.send(embed=embed)
203
+
204
+ @bot.command()
205
+ async def help(ctx):
206
+ """Show all commands"""
207
+ embed = discord.Embed(
208
+ title="🤖 Bot Commands",
209
+ description="Here are all available commands:",
210
+ color=discord.Color.blue()
211
+ )
212
+
213
+ commands_list = [
214
+ ("!ping", "Check bot latency"),
215
+ ("!server", "Server information"),
216
+ ("!user @mention", "User information"),
217
+ ("!clear [amount]", "Clear messages (1-100)"),
218
+ ("!say [message]", "Bot says something"),
219
+ ("!embed Title | Desc", "Create custom embed"),
220
+ ("!poll [question]", "Create a poll"),
221
+ ("!avatar @mention", "Get user avatar"),
222
+ ("!help", "Show this message")
223
+ ]
224
+
225
+ for cmd, desc in commands_list:
226
+ embed.add_field(name=cmd, value=desc, inline=False)
227
+
228
+ embed.set_footer(text=f"Requested by {ctx.author.name}")
229
+ await ctx.send(embed=embed)
230
+
231
+ # Error handling
232
+ @bot.event
233
+ async def on_command_error(ctx, error):
234
+ if isinstance(error, commands.CommandNotFound):
235
+ return
236
+ elif isinstance(error, commands.MissingPermissions):
237
+ await ctx.send("❌ You don't have permission to use this command!")
238
+ elif isinstance(error, commands.MissingRequiredArgument):
239
+ await ctx.send(f"❌ Missing argument! Usage: `{ctx.command.signature}`")
240
+ elif isinstance(error, commands.BadArgument):
241
+ await ctx.send("❌ Invalid argument provided!")
242
+ else:
243
+ print(f"Error: {error}")
244
+
245
+ # Run bot
246
+ if __name__ == "__main__":
247
+ # Start Flask server in background
248
+ flask_thread = Thread(target=run_flask)
249
+ flask_thread.daemon = True
250
+ flask_thread.start()
251
+
252
+ # Run Discord bot
253
+ TOKEN = os.getenv("DISCORD_TOKEN")
254
+ if not TOKEN:
255
+ print("❌ DISCORD_TOKEN not found! Please set it in Hugging Face secrets.")
256
+ else:
257
+ bot.run(TOKEN)