diff --git a/app/routers/bot.py b/app/routers/bot.py index a6f8ef9..5ac3764 100644 --- a/app/routers/bot.py +++ b/app/routers/bot.py @@ -1,7 +1,7 @@ import json from quart import Blueprint, jsonify, request, abort, current_app from werkzeug.exceptions import HTTPException -from internal.types import ActiveClient +from internal.types import ActiveClient, DiscordId from internal.db_wrappers import DiscordIdDbController bot_bp = Blueprint('bot', __name__) @@ -67,6 +67,95 @@ async def get_all_discord_tokens(): abort(500, f"An internal error occurred: {e}") +@bot_bp.route('/token/', methods=['GET']) +async def get_discord_token_by_id(discord_id_param: str): + """ + API endpoint to get a single Discord ID by its _id. + """ + try: + query = {"_id": discord_id_param} + d_id = await current_app.d_id_db_h.find_discord_id(query) + if d_id: + return jsonify(d_id.to_dict()) + else: + abort(404, "Discord ID not found.") + except Exception as e: + print(f"Error in get_discord_token_by_id: {e}") + abort(500, f"An internal error occurred: {e}") + + +@bot_bp.route('/token', methods=["POST"]) +async def create_discord_token(): + """ + API Endpoint to create a discord token (adding a new bot) + """ + data = await request.get_json() + + temp_name = data.get("name") + temp_discord_id = data.get("discord_id") + temp_token = data.get("token") + temp_active = bool(data.get("active")) + temp_guilds = data.get("guilds") + + # Create the discord ID object + d_id = DiscordId(None, temp_discord_id, temp_name, temp_token, temp_active, temp_guilds) + + new_d_id = await current_app.d_id_db_h.create_discord_id(d_id.to_dict()) + + if new_d_id: + return jsonify(new_d_id.to_dict()), 201 + else: + abort(500, "Failed to create Discord ID.") + + +@bot_bp.route('/token/', methods=['PUT']) +async def update_discord_token(discord_id_param: str): + """ + API endpoint to update a Discord ID by its _id. + """ + try: + data = await request.get_json() + if not data: + abort(400, "No update data provided.") + + query = {"_id": discord_id_param} + update_result = await current_app.d_id_db_h.update_discord_id(query, {"$set": data}) + + if update_result is not None: + if update_result > 0: + # Optionally, fetch the updated document to return it + updated_d_id = await current_app.d_id_db_h.find_discord_id(query) + return jsonify(updated_d_id.to_dict()), 200 + else: + abort(404, "Discord ID not found or no changes applied.") + else: + abort(500, "Failed to update Discord ID.") + except Exception as e: + print(f"Error in update_discord_token: {e}") + abort(500, f"An internal error occurred: {e}") + + +@bot_bp.route('/token/', methods=['DELETE']) +async def delete_discord_token(discord_id_param: str): + """ + API endpoint to delete a Discord ID by its _id. + """ + try: + query = {"_id": discord_id_param} + delete_count = await current_app.d_id_db_h.delete_discord_id(query) + + if delete_count is not None: + if delete_count > 0: + return jsonify({"message": f"Successfully deleted {delete_count} Discord ID(s)."}), 200 + else: + abort(404, "Discord ID not found.") + else: + abort(500, "Failed to delete Discord ID.") + except Exception as e: + print(f"Error in delete_discord_token: {e}") + abort(500, f"An internal error occurred: {e}") + + # ------- Util Functions def find_token_in_active_clients(target_token: str) -> bool: @@ -86,15 +175,4 @@ def find_token_in_active_clients(target_token: str) -> bool: return True except Exception as e: pass - return False - - - - - - - - - # query_params = dict(request.args) - - # systems = await current_app.sys_db_h.find_systems(query_params) \ No newline at end of file + return False \ No newline at end of file