File size: 1,572 Bytes
b8f05dc
318911f
b8f05dc
 
318911f
 
 
b8f05dc
 
 
 
 
 
 
 
f6dc1b0
 
fdf1ead
318911f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8f05dc
 
 
 
 
 
 
 
318911f
 
 
 
 
 
b8f05dc
 
 
 
 
 
 
 
0a19f5a
b8f05dc
 
318911f
 
 
b8f05dc
318911f
 
b8f05dc
318911f
 
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
import discord
from discord.ext import commands
import mysql.connector
import os
from flask import Flask

app = Flask(__name__)

mysql = mysql.connector.connect(
    host=os.environ.get('DB_HOST'),
    user=os.environ.get('DB_USER'),
    password=os.environ.get('DB_PASSWORD'),
    database=os.environ.get('DB_DATABASE'),
    port=3306
)
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)


@bot.event
async def on_ready():
    print(f'{bot.user} is now running!')


@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    server_name = message.author.guild.name

    if bot.user.mentioned_in(message):
        await send_message(message, server_name)


async def send_message(message, server_name):
    try:
        await message.channel.send(f'Hello World {server_name}')
    except Exception as e:
        print(e)


# Flask route for health check or any other purposes
@app.route('/')
def home():
    return "Discord Bot is running!"


def run_discord_bot():
    cursor = mysql.cursor(dictionary=True)
    cursor.execute("SELECT `token` FROM `auth_tokens` WHERE bot_id=%s", ((os.environ.get('BOT_ID')),))
    bot_token = cursor.fetchone()
    cursor.close()

    TOKEN = bot_token['token']

    bot.run(TOKEN)


if __name__ == '__main__':
    # Run the Flask application alongside the Discord bot
    from gevent.pywsgi import WSGIServer

    server = WSGIServer(('0.0.0.0', 5000), app)
    server.start()

    # Run the Discord bot
    run_discord_bot()