2024-08-29 13:24:26 +00:00
|
|
|
import mysql.connector
|
|
|
|
from discord.ext import commands
|
|
|
|
|
2024-09-02 08:16:43 +00:00
|
|
|
import lib.format
|
2024-08-29 13:24:26 +00:00
|
|
|
from db import database
|
2024-09-03 07:47:46 +00:00
|
|
|
from lib.client import Luminara
|
2024-08-29 13:24:26 +00:00
|
|
|
from lib.const import CONST
|
|
|
|
from lib.format import shorten
|
|
|
|
from ui.embeds import Builder
|
|
|
|
|
|
|
|
|
|
|
|
class Sql(commands.Cog):
|
2024-09-03 07:47:46 +00:00
|
|
|
def __init__(self, bot: Luminara):
|
2024-08-29 13:24:26 +00:00
|
|
|
self.bot = bot
|
2024-09-02 08:16:43 +00:00
|
|
|
self.select_cmd.usage = lib.format.generate_usage(self.select_cmd)
|
|
|
|
self.inject_cmd.usage = lib.format.generate_usage(self.inject_cmd)
|
2024-08-29 13:24:26 +00:00
|
|
|
|
|
|
|
@commands.command(name="sqlselect", aliases=["sqls"])
|
|
|
|
@commands.is_owner()
|
2024-09-01 12:14:43 +00:00
|
|
|
async def select_cmd(
|
|
|
|
self,
|
2024-09-03 07:47:46 +00:00
|
|
|
ctx: commands.Context[Luminara],
|
2024-09-01 12:14:43 +00:00
|
|
|
*,
|
|
|
|
query: str,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Execute a SQL SELECT query.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2024-09-03 07:47:46 +00:00
|
|
|
ctx : commands.Context[Luminara]
|
2024-09-01 12:14:43 +00:00
|
|
|
The context of the command.
|
|
|
|
query : str
|
|
|
|
The SQL query to execute.
|
|
|
|
"""
|
2024-08-29 13:24:26 +00:00
|
|
|
if query.lower().startswith("select "):
|
|
|
|
query = query[7:]
|
|
|
|
|
|
|
|
try:
|
|
|
|
results = database.select_query(f"SELECT {query}")
|
|
|
|
embed = Builder.create_embed(
|
2024-09-03 10:16:21 +00:00
|
|
|
Builder.SUCCESS,
|
2024-08-29 13:24:26 +00:00
|
|
|
user_name=ctx.author.name,
|
|
|
|
author_text=CONST.STRINGS["admin_sql_select_title"],
|
|
|
|
description=CONST.STRINGS["admin_sql_select_description"].format(
|
|
|
|
shorten(query, 200),
|
|
|
|
shorten(str(results), 200),
|
|
|
|
),
|
|
|
|
hide_name_in_description=True,
|
|
|
|
)
|
|
|
|
except mysql.connector.Error as error:
|
|
|
|
embed = Builder.create_embed(
|
2024-09-03 10:16:21 +00:00
|
|
|
Builder.ERROR,
|
2024-08-29 13:24:26 +00:00
|
|
|
user_name=ctx.author.name,
|
|
|
|
author_text=CONST.STRINGS["admin_sql_select_error_title"],
|
|
|
|
description=CONST.STRINGS["admin_sql_select_error_description"].format(
|
|
|
|
shorten(query, 200),
|
|
|
|
shorten(str(error), 200),
|
|
|
|
),
|
|
|
|
hide_name_in_description=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await ctx.send(embed=embed, ephemeral=True)
|
|
|
|
|
|
|
|
@commands.command(name="sqlinject", aliases=["sqli"])
|
|
|
|
@commands.is_owner()
|
2024-09-01 12:14:43 +00:00
|
|
|
async def inject_cmd(
|
|
|
|
self,
|
2024-09-03 07:47:46 +00:00
|
|
|
ctx: commands.Context[Luminara],
|
2024-09-01 12:14:43 +00:00
|
|
|
*,
|
|
|
|
query: str,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Execute a SQL INJECT query.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2024-09-03 07:47:46 +00:00
|
|
|
ctx : commands.Context[Luminara]
|
2024-09-01 12:14:43 +00:00
|
|
|
The context of the command.
|
|
|
|
query : str
|
|
|
|
The SQL query to execute.
|
|
|
|
"""
|
2024-08-29 13:24:26 +00:00
|
|
|
try:
|
|
|
|
database.execute_query(query)
|
|
|
|
embed = Builder.create_embed(
|
2024-09-03 10:16:21 +00:00
|
|
|
Builder.SUCCESS,
|
2024-08-29 13:24:26 +00:00
|
|
|
user_name=ctx.author.name,
|
|
|
|
author_text=CONST.STRINGS["admin_sql_inject_title"],
|
|
|
|
description=CONST.STRINGS["admin_sql_inject_description"].format(
|
|
|
|
shorten(query, 200),
|
|
|
|
),
|
|
|
|
hide_name_in_description=True,
|
|
|
|
)
|
|
|
|
except mysql.connector.Error as error:
|
|
|
|
embed = Builder.create_embed(
|
2024-09-03 10:16:21 +00:00
|
|
|
Builder.ERROR,
|
2024-08-29 13:24:26 +00:00
|
|
|
user_name=ctx.author.name,
|
|
|
|
author_text=CONST.STRINGS["admin_sql_inject_error_title"],
|
|
|
|
description=CONST.STRINGS["admin_sql_inject_error_description"].format(
|
|
|
|
shorten(query, 200),
|
|
|
|
shorten(str(error), 200),
|
|
|
|
),
|
|
|
|
hide_name_in_description=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
await ctx.send(embed=embed, ephemeral=True)
|
|
|
|
|
|
|
|
|
2024-09-03 07:47:46 +00:00
|
|
|
async def setup(bot: Luminara) -> None:
|
2024-08-29 13:24:26 +00:00
|
|
|
await bot.add_cog(Sql(bot))
|