Spaces:
Paused
Paused
Update bot.py
Browse files
bot.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import discord
|
|
|
2 |
import mysql.connector
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
mysql = mysql.connector.connect(
|
6 |
host=os.environ.get('DB_HOST'),
|
@@ -10,6 +14,24 @@ mysql = mysql.connector.connect(
|
|
10 |
port=3306
|
11 |
)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
async def send_message(message, server_name):
|
15 |
try:
|
@@ -18,6 +40,12 @@ async def send_message(message, server_name):
|
|
18 |
print(e)
|
19 |
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def run_discord_bot():
|
22 |
cursor = mysql.cursor(dictionary=True)
|
23 |
cursor.execute("SELECT `token` FROM `auth_tokens` WHERE bot_id=%s", ((os.environ.get('BOT_ID')),))
|
@@ -25,26 +53,16 @@ def run_discord_bot():
|
|
25 |
cursor.close()
|
26 |
|
27 |
TOKEN = bot_token['token']
|
28 |
-
intents = discord.Intents.default()
|
29 |
-
intents.message_content = True
|
30 |
-
client = discord.Client(intents=intents)
|
31 |
-
|
32 |
-
@client.event
|
33 |
-
async def on_ready():
|
34 |
-
print(f'{client.user} is now running!')
|
35 |
|
36 |
-
|
37 |
-
async def on_message(message):
|
38 |
-
if message.author == client.user:
|
39 |
-
return
|
40 |
|
41 |
-
server_name = message.author.guild.name
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
client.run(TOKEN)
|
47 |
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
run_discord_bot()
|
|
|
1 |
import discord
|
2 |
+
from discord.ext import commands
|
3 |
import mysql.connector
|
4 |
import os
|
5 |
+
from flask import Flask
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
|
9 |
mysql = mysql.connector.connect(
|
10 |
host=os.environ.get('DB_HOST'),
|
|
|
14 |
port=3306
|
15 |
)
|
16 |
|
17 |
+
bot = commands.Bot(command_prefix='!')
|
18 |
+
|
19 |
+
|
20 |
+
@bot.event
|
21 |
+
async def on_ready():
|
22 |
+
print(f'{bot.user} is now running!')
|
23 |
+
|
24 |
+
|
25 |
+
@bot.event
|
26 |
+
async def on_message(message):
|
27 |
+
if message.author == bot.user:
|
28 |
+
return
|
29 |
+
|
30 |
+
server_name = message.author.guild.name
|
31 |
+
|
32 |
+
if bot.user.mentioned_in(message):
|
33 |
+
await send_message(message, server_name)
|
34 |
+
|
35 |
|
36 |
async def send_message(message, server_name):
|
37 |
try:
|
|
|
40 |
print(e)
|
41 |
|
42 |
|
43 |
+
# Flask route for health check or any other purposes
|
44 |
+
@app.route('/')
|
45 |
+
def home():
|
46 |
+
return "Discord Bot is running!"
|
47 |
+
|
48 |
+
|
49 |
def run_discord_bot():
|
50 |
cursor = mysql.cursor(dictionary=True)
|
51 |
cursor.execute("SELECT `token` FROM `auth_tokens` WHERE bot_id=%s", ((os.environ.get('BOT_ID')),))
|
|
|
53 |
cursor.close()
|
54 |
|
55 |
TOKEN = bot_token['token']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
bot.run(TOKEN, bot=False)
|
|
|
|
|
|
|
58 |
|
|
|
59 |
|
60 |
+
if __name__ == '__main__':
|
61 |
+
# Run the Flask application alongside the Discord bot
|
62 |
+
from gevent.pywsgi import WSGIServer
|
|
|
63 |
|
64 |
+
server = WSGIServer(('0.0.0.0', 5000), app)
|
65 |
+
server.start()
|
66 |
|
67 |
+
# Run the Discord bot
|
68 |
+
run_discord_bot()
|