Ever wished you could just ask a database a question instead of writing complex SQL queries? Well, thatβs exactly what this project does! The HiveSQL Discord Bot lets you interact with a SQL database using natural language, making querying easier and more intuitive. Letβs dive into the cool stuff this bot can do!
- π¬ Ask in Natural Language, Get SQL β Just type your question, and the bot will generate the SQL query for you.
- π₯οΈ Run SQL Directly β If youβre more hands-on, you can execute queries manually.
- π Explore Database Schema β Check out available tables and their structures.
- π§ AI Assistance β Stuck on an error? The bot can explain and guide you through it.
- β‘ Built-in Rate Limits β Keeps things smooth and prevents spam.
Handles everything on Discord, from user commands to admin controls. It also:
β
Supports command shortcuts
β
Includes a cooldown system
β
Manages errors and gives feedback
This is where all the SQL magic happens. It takes care of:
β
Connecting to the database (HiveSQL, in this case)
β
Running queries safely
β
Caching table metadata for quick access
Every bot command goes through here. It handles:
β
Natural language to SQL conversion
β
Running SQL queries
β
Showing table info
β
Formatting query results
Everything is neatly organized in one place, including:
β
Discord bot settings
β
Database connection details
β
AI model configuration, supporting OpenAI and Groq
β
Query limitations
This bot isnβt just running SQL, itβs thinking (well, kind of). Thanks to LangChain, it can:
π‘ Convert human language into SQL queries
π€ Explain errors and suggest fixes
π Help users understand database structures
π Check queries for complexity and safety
Talking to the bot is super easy! Just use these commands:
!aiquery - Convert plain English to SQL
!hivesql - Execute SQL queries
!tablelist - Show available tables
!tableinfo - Display table schema
!help - Get AI-powered assistance
Keeping things safe and efficient is a top priority! The bot includes:
β
Rate limiting (to prevent abuse)
β
Daily query limits
β
Error handling & sanitization
β
Secure credential management with environment variables
This is just the beginning! Future improvements could include:
- Query result caching for faster responses
- More AI models to choose from
- Smarter query validation
- Interactive query builder for a more hands-on experience
- Query history & analytics
- Plot Graphics why not?!
This bot is an example of how AI + SQL + Discord can come together to create something truly useful. Whether you're a SQL pro or just starting out, it makes database interaction way more intuitive!
- Python 3.8+
- Microsoft ODBC Driver 18 for SQL Server
- Discord Account
- OpenAi or Groq API Key
- discord.py
- python-dotenv
- pypyodbc
- langchain
- table2ascii
- langchain-groq
- Clone the Repository
git clone https://github.com/rferrari/hivesql_aibot.git
cd hivesql_aibot
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
- Add the Microsoft repository for Debian:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/debian/11/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
- Install the ODBC driver and tools:
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools18
sudo apt-get install -y unixodbc-dev
- Verify the installation:
odbcinst -j
Let's set up the ODBC driver configuration:
- Create the ODBC driver configuration:
sudo bash -c 'cat > /etc/odbcinst.ini << EOL
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.*.so.1.1
UsageCount=1
EOL'
- Verify the driver is properly registered:
odbcinst -q -d
This should list the available ODBC drivers. If you see [ODBC Driver 18 for SQL Server]
in the output, the driver is properly configured.
If you encounter any permission issues with the configuration files:
sudo chmod 644 /etc/odbcinst.ini
sudo chown root:root /etc/odbcinst.ini
- Copy sample.env to .env and configure file with the following:
DISCORD_TOKEN=your_discord_bot_token
DISCORD_ADMIN_ID=your_discord_admin_id
OPENAI_API_KEY=your_open_ai_api_key
HIVESQL_SERVER=your_sql_server_address
HIVESQL_DATABASE=your_database_name
HIVESQL_USER=your_sql_username
HIVESQL_PWD=your_sql_password
The bot supports the following commands:
Use the !hivesql
command to execute direct SQL queries:
!hivesql SELECT TOP 100 * FROM Table
Use the !aiquery
command to ask questions in natural language:
!aiquery Show me the top 5 users that posted more this month
Use the !tablelist
command to see all available tables:
!tablelist
This will display a list of all accessible tables in the database.
Use the !tableinfo
command followed by a table name to see its structure:
!tableinfo TableName
This will show the complete schema for the specified table, including all columns and their types.
- Queries are automatically limited to 100 rows
- Use proper table and column names as shown in
!tableinfo
- For complex queries, prefer
!hivesql
over!aiquery
- AI queries will automatically format and validate your request
!hivesql SELECT TOP 10 author, title FROM Comments WHERE author = 'username' ORDER BY created DESC
!aiquery What are the latest comments from user 'username'?
!tableinfo Comments
-Queries are limited to 100 rows -Only specific columns are queried -Prevents full table scans -Sanitizes input to reduce SQL injection risks
-Ensure all environment variables are correctly set -Verify ODBC driver installation -Check network connectivity to SQL Server
-Fork the repository -Create your feature branch -Commit your changes -Push to the branch -Create a Pull Request
This bot is provided as-is. Always be cautious when running SQL queries and ensure proper access controls.