Skip to content

Commit

Permalink
Allow for the searching of error refs rather than searching the whole…
Browse files Browse the repository at this point in the history
… thing
  • Loading branch information
parafoxia committed Nov 27, 2021
1 parent d269a3e commit 204b42a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions carberretta/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,19 @@ async def on_command_error(event: events.CommandErrorEvent) -> None:
await event.context.respond("You need to be an owner to do that.")
return

# Add more errors when needed.

try:
err_id = hashlib.md5(f"{time.time()}".encode()).hexdigest()
await bot.d.db.execute(
"INSERT INTO errors VALUES (?, ?, ?)",
"INSERT INTO errors (err_id, err_cmd, err_text) VALUES (?, ?, ?)",
err_id,
event.context.invoked_with,
"".join(traceback.format_exception(event.exception)), # type: ignore
)
await event.context.respond(
f"Something went wrong. An error report has been created (ID: {err_id})."
"Something went wrong. An error report has been created "
f"(ID: {err_id[:7]})."
)
finally:
raise event.exception
Expand Down
38 changes: 38 additions & 0 deletions carberretta/extensions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import io
import logging
import typing as t

import hikari
from lightbulb import checks, commands, context, decorators, plugins

if t.TYPE_CHECKING:
Expand All @@ -49,6 +51,42 @@ async def cmd_shutdown(ctx: context.base.Context) -> None:
await ctx.bot.close()


@plugin.command
@decorators.add_checks(checks.owner_only)
@decorators.option("id", "The error reference ID.")
@decorators.command("error", "View an error.")
@decorators.implements(commands.slash.SlashCommand)
async def cmd_error(ctx: context.base.Context) -> None:
if len(search_id := ctx.options.id) < 5:
await ctx.respond("Your search should be at least 5 characters long.")
return

if not plugin.bot:
# Remove on next Lightbulb release.
return

row = await plugin.bot.d.db.try_fetch_record(
"SELECT * FROM errors "
"WHERE instr(err_id, ?) > 0 "
"ORDER BY err_time DESC "
"LIMIT 1",
search_id,
)

if not row:
await ctx.respond("No errors matching that reference were found.")
return

message = await ctx.respond("Error found. Standby...")
b = io.BytesIO(
f"Command: /{row.err_cmd}\nAt: {row.err_time}\n\n{row.err_text}".encode()
)
b.seek(0)
await message.edit(
content=None, attachment=hikari.files.Bytes(b, f"err{row.err_id}.txt")
)


def load(bot: "BotApp") -> None:
bot.add_plugin(plugin)

Expand Down
1 change: 1 addition & 0 deletions data/static/build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

CREATE TABLE IF NOT EXISTS errors (
err_id TEXT PRIMARY KEY,
err_time NUMERIC DEFAULT CURRENT_TIMESTAMP,
err_cmd TEXT,
err_text TEXT
);

0 comments on commit 204b42a

Please sign in to comment.