Remotely connect to any MySQL Database via Discord and execute any query!
The MySQL Discord Bot is a powerful tool that allows you to remotely connect to any MySQL database and execute queries directly from a Discord server. This bot is designed to facilitate database management and querying without needing direct access to the database server, making it ideal for developers, database administrators, and technical teams who collaborate on Discord.
The MySQL Discord Bot operates by connecting to a MySQL database and executing queries based on user commands within a Discord server. Here’s a step-by-step breakdown of how it functions:
-
Database Connection: The bot reads the database connection variables from the vars.py file, which includes the database credentials and connection details.
-
Command Listening: The bot continuously listens for specific command syntax in the Discord server. Commands must follow the predefined syntax set in the configuration.
-
User Authentication: Upon receiving a command, the bot checks if the user is whitelisted in the vars.py file. Only authorized users are allowed to execute queries.
-
Query Execution: If the user is authorized, the bot executes the query on the connected MySQL database.
-
Response Handling: The bot sends the query results back to the same Discord channel where the command was issued. If the user is not authorized, the bot sends an error message instead.
To use the MySQL Discord Bot, follow these steps:
Command Syntax: All commands must start with the predefined syntax specified in the configuration (e.g., mysql>). This syntax is case-sensitive. Query Execution: Type your SQL query after the syntax to execute it. Example:
mysql> SELECT * FROM `users` WHERE id=2
Detailed Steps:
- Whitelisted Users Only: The bot checks if the user issuing the command is whitelisted. If not, an error message will be sent.
- Help Command: Type mysql> help to get a link to MySQL documentation.
- Query Execution: The bot will process the query and return the results. If the query returns an empty list, a message indicating this will be sent. If there is an error in the query, *
the error message will be displayed.
Responses:
- Successful Query: The bot returns the query results in a tabular format.
- Empty Result: A message indicating that the query returned an empty list.
- Unauthorized User: An error message if the user is not whitelisted.
- Query Error: An error message if there is an issue with the query.
This setup ensures secure and efficient query execution directly from your Discord server.
To set up and run the MySQL Discord Bot, follow these steps:
- Clone the Repository Clone the repository to your local machine using the following command:
git clone https://github.com/YonLiud/MySQL-Discord-Bot/
-
Obtain a Discord Bot Token After cloning the repository, you'll need to obtain a token for your Discord bot. Follow this guide to get your bot token.
-
Install Prerequisites Before using the bot, install the required Python modules. Navigate to the root directory of the cloned repository and run:
pip3 install -r requirements.txt
- Configure the Bot Open the vars.py file located in the root directory of the cloned repository. Fill in the required information in the vars.py file:
USERNAME = 'your_database_username'
PASSWORD = 'your_database_password'
HOSTNAME = 'your_database_host'
DATABASE = 'your_database_name'
WHITELIST_IDS = [list_of_whitelisted_user_ids]
SYNTAX = 'your_command_syntax'
TOKEN = 'your_discord_bot_token'
Replace placeholders (your_database_username, your_database_password, etc.) with your actual database credentials, Discord bot token, and command syntax.
- Deploying Your Own Bot To run the bot, execute the following command in your terminal within the root directory of the cloned repository:
python3 app.py
This command starts the MySQL Discord Bot and connects it to your configured Discord server and MySQL database.