mirror of https://github.com/stijndcl/didier
Listing custom commands
parent
cedb284adc
commit
a08bfca4c7
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from discord.ext import ipc
|
from discord.ext import ipc
|
||||||
|
from functions.database import custom_commands
|
||||||
from quart import Quart, jsonify, request
|
from quart import Quart, jsonify, request
|
||||||
from quart_cors import cors
|
from quart_cors import cors
|
||||||
from time import time
|
from time import time
|
||||||
|
@ -42,5 +43,41 @@ async def send_dm():
|
||||||
return jsonify({"response": 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/<command_id>")
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
app.run()
|
app.run()
|
||||||
|
|
|
@ -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,))
|
cursor.execute("INSERT INTO custom_command_aliases(command, alias) VALUES(%s, %s)", (command_id, alias,))
|
||||||
connection.commit()
|
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
|
||||||
|
|
Loading…
Reference in New Issue