WaspInACup commited on
Commit
cdb00d7
·
verified ·
1 Parent(s): 6677722

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import threading
3
+ from flask import Flask
4
+ import discord
5
+
6
+ # 1. Setup a tiny web server on port 7860 so Hugging Face keeps the Space awake
7
+ app = Flask(__name__)
8
+
9
+ @app.route("/")
10
+ def home():
11
+ return "🤖 Discord Bot is alive and running 24/7!"
12
+
13
+ def run_web():
14
+ app.run(host="0.0.0.0", port=7860)
15
+
16
+ # 2. Setup your Discord Bot
17
+ intents = discord.Intents.default()
18
+ intents.message_content = True
19
+ client = discord.Client(intents=intents)
20
+
21
+ @client.event
22
+ async def on_ready():
23
+ print(f"Logged in successfully as {client.user}!")
24
+
25
+ @client.event
26
+ async def on_message(message):
27
+ if message.author == client.user:
28
+ return
29
+ # Simple test command
30
+ if message.content.startswith("!hello"):
31
+ await message.channel.send(f"Hello {message.author.mention}! I am running for free in the cloud on Hugging Face!")
32
+
33
+ # 3. Run both the web server and the Discord bot simultaneously
34
+ if __name__ == "__main__":
35
+ # Start the web server in a background thread
36
+ web_thread = threading.Thread(target=run_web)
37
+ web_thread.start()
38
+
39
+ # Grab your token securely from Hugging Face Secrets
40
+ TOKEN = os.getenv("DISCORD_TOKEN")
41
+ if TOKEN:
42
+ client.run(TOKEN)
43
+ else:
44
+ print("ERROR: DISCORD_TOKEN secret was not found!")