Remove Flask server and health-check endpoint from bot implementation and update requirements

This commit is contained in:
2025-03-06 21:00:32 +07:00
parent af68d6182f
commit d0287e7b9f
10 changed files with 71 additions and 42 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

23
.idea/ChatGPT-Discord-Bot.iml generated Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="Flask">
<option name="enabled" value="true" />
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (ChatGPT-Discord-Bot)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="py.test" />
</component>
</module>

13
.idea/dataSources.xml generated Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="@cluster0.akbzf5r.mongodb.net" uuid="1fd5036f-4eb6-4575-b3b7-1a641f728a88">
<driver-ref>documentdb</driver-ref>
<synchronize>true</synchronize>
<configured-by-url>true</configured-by-url>
<jdbc-driver>com.dbschema.MongoJdbcDriver</jdbc-driver>
<jdbc-url>mongodb+srv://chatgpt:Anhtt2021@cluster0.akbzf5r.mongodb.net/?retryWrites=true&amp;w=majority&amp;appName=Cluster0</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (ChatGPT-Discord-Bot)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (ChatGPT-Discord-Bot)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ChatGPT-Discord-Bot.iml" filepath="$PROJECT_DIR$/.idea/ChatGPT-Discord-Bot.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@@ -24,13 +24,6 @@ COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --no-cache-dir -r requirements.txt
# Expose port (optional, only if needed for a web server)
EXPOSE 5000
# Add health check (update endpoint if needed)
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD curl --fail http://localhost:5000/health || exit 1
# Copy the rest of the application source code
COPY . .

34
bot.py
View File

@@ -1,7 +1,6 @@
import os
import discord
import io
import threading
import tiktoken
import asyncio
import requests
@@ -9,8 +8,6 @@ import logging
import sys
import json
import aiohttp
import tempfile
import subprocess
import re
from discord.ext import commands, tasks
from discord import app_commands
@@ -22,7 +19,6 @@ from collections import defaultdict
from dotenv import load_dotenv
from flask import Flask, jsonify
from PyPDF2 import PdfReader
import io
# Load environment variables
load_dotenv()
@@ -30,31 +26,6 @@ load_dotenv()
# Supported file types for text processing
supported_file_types = ['.txt', '.py', '.js', '.html', '.css', '.json', '.md', '.log', '.csv', '.xml', '.yml', '.yaml', '.ini', '.cfg', '.conf']
# Flask app for health-check
app = Flask(__name__)
# Health-check endpoint
@app.route('/health', methods=['GET'])
def health():
"""
Checks if the bot is ready and connected to Discord.
"""
if bot.is_closed(): # Bot is disconnected
return jsonify(status="unhealthy", error="Bot is disconnected"), 500
elif not bot.is_ready(): # Bot is not ready yet
return jsonify(status="unhealthy", error="Bot is not ready"), 500
elif bot.latency > 151: # Bot heartbeat is blocked for more than 151 seconds
return jsonify(status="unhealthy", error=f"Heartbeat to websocket blocked for {bot.latency:.2f} seconds"), 500
else:
return jsonify(status="healthy"), 200
# Run Flask server in a separate thread
def run_flask():
"""
Starts the Flask server.
"""
app.run(host="0.0.0.0", port=5000)
# OpenAI client initialization
client = OpenAI(
base_url=str(os.getenv("OPENAI_BASE_URL")),
@@ -2058,11 +2029,6 @@ def extract_code_blocks(content):
return matches
# Start Flask in a separate thread
flask_thread = threading.Thread(target=run_flask)
flask_thread.daemon = True # Ensure it closes when the main program exits
flask_thread.start()
# Main bot startup
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

View File

@@ -7,7 +7,6 @@ runware
Pillow
discord.py
pymongo
flask
tiktoken
motor
PyPDF2