From a08bfca4c7ab2dea8fdc9e7796a205570571b885 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sat, 19 Jun 2021 17:02:42 +0200 Subject: [PATCH] Listing custom commands --- backend/server.py | 37 +++++++++++++++++++ functions/database/custom_commands.py | 52 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/backend/server.py b/backend/server.py index 0982e25..67cb8e9 100644 --- a/backend/server.py +++ b/backend/server.py @@ -1,6 +1,7 @@ import json from discord.ext import ipc +from functions.database import custom_commands from quart import Quart, jsonify, request from quart_cors import cors from time import time @@ -42,5 +43,41 @@ async def send_dm(): return jsonify({"response": dm}) +@app.route("/custom", methods=["GET"]) +async def get_all_custom_commands(): + """ + Return a list of all custom commands in the bot + """ + commands = custom_commands.get_all() + + return jsonify(commands) + + +@app.route("/custom/") +async def get_custom_command(command_id): + try: + command_id = int(command_id) + except ValueError: + # Id is not an int + return unprocessable_entity("Parameter id was not a valid integer.") + + command = custom_commands.get_by_id(command_id) + + if command is None: + return page_not_found("") + + return jsonify(command) + + +@app.errorhandler(404) +def page_not_found(e): + return jsonify({"error": "No resource could be found matching the given URL."}), 404 + + +@app.errorhandler(422) +def unprocessable_entity(e): + return jsonify({"error": e}), 422 + + if __name__ == "__main__": app.run() diff --git a/functions/database/custom_commands.py b/functions/database/custom_commands.py index ea8f172..7f0147a 100644 --- a/functions/database/custom_commands.py +++ b/functions/database/custom_commands.py @@ -127,3 +127,55 @@ def add_alias(command: str, alias: str): cursor.execute("INSERT INTO custom_command_aliases(command, alias) VALUES(%s, %s)", (command_id, alias,)) connection.commit() + + +def get_all(): + """ + Return a list of all registered custom commands + """ + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT * FROM custom_commands") + commands = cursor.fetchall() + ret = [] + + # Create a list of all entries + for command in commands: + dic = {"id": command[0], "name": command[1], "response": command[2]} + + # Find and add aliases + cursor.execute("SELECT id, alias FROM custom_command_aliases WHERE command = %s", (command[0],)) + aliases = cursor.fetchall() + + if aliases: + dic["aliases"] = list(map(lambda x: {"id": x[0], "alias": x[1]}, aliases)) + + ret.append(dic) + + return ret + + +def get_by_id(command_id: int): + """ + Return a command that matches a given id + """ + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT * FROM custom_commands WHERE id = %s", (command_id,)) + command = cursor.fetchone() + + # Nothing found + if not command: + return None + + dic = {"id": command[0], "name": command[1], "response": command[2]} + + cursor.execute("SELECT id, alias FROM custom_command_aliases WHERE command = %s", (command_id,)) + aliases = cursor.fetchall() + + if aliases: + dic["aliases"] = list(map(lambda x: {"id": x[0], "alias": x[1]}, aliases)) + + return dic