Enable commands globally if not sandboxing, add support for test guilds in env, monitor slash command & context menu usage, create error handler for slash commands, log slash commands in terminal

This commit is contained in:
Stijn De Clercq 2021-09-03 20:40:03 +02:00
parent ef547a7090
commit a28bd116f0
12 changed files with 117 additions and 62 deletions

View file

@ -1,23 +1,20 @@
from enum import IntEnum
from functions.database import utils
from functions.stringFormatters import leading_zero as lz
import time
def invoked():
class InvocationType(IntEnum):
TextCommand = 0
SlashCommand = 1
ContextMenu = 2
def invoked(inv: InvocationType):
t = time.localtime()
day_string: str = f"{t.tm_year}-{_lz(t.tm_mon)}-{_lz(t.tm_mday)}"
_update(day_string)
def _lz(arg: int) -> str:
"""
Add leading zeroes if necessary (YYYY-MM-DD)
"""
arg = str(arg)
if len(arg) == 1:
return f"0{arg}"
return arg
day_string: str = f"{t.tm_year}-{lz(t.tm_mon)}-{lz(t.tm_mday)}"
_update(day_string, inv)
def _is_present(date: str) -> bool:
@ -43,25 +40,27 @@ def _add_date(date: str):
connection = utils.connect()
cursor = connection.cursor()
cursor.execute("INSERT INTO command_stats(day, amount) VALUES (%s, 1)", (date,))
cursor.execute("INSERT INTO command_stats(day, commands, slash_commands, context_menus) VALUES (%s, 0, 0, 0)", (date,))
connection.commit()
def _update(date: str):
def _update(date: str, inv: InvocationType):
"""
Increase the counter for a given day
"""
# Date wasn't present yet, add it with a value of 1
# Date wasn't present yet, add it
if not _is_present(date):
_add_date(date)
return
connection = utils.connect()
cursor = connection.cursor()
cursor.execute("""
column_name = ["commands", "slash_commands", "context_menus"][inv.value]
# String formatting is safe here because the input comes from above ^
cursor.execute(f"""
UPDATE command_stats
SET amount = amount + 1
SET {column_name} = {column_name} + 1
WHERE day = %s
""", (date,))
connection.commit()

View file

@ -1,3 +1,9 @@
import traceback
from discord.ext.commands import Context
from dislash import SlashInteraction
def title_case(string):
return " ".join(capitalize(word) for word in string.split(" "))
@ -13,3 +19,35 @@ def leading_zero(string, size=2):
while len(string) < size:
string = "0" + string
return string
def format_error_tb(err: Exception) -> str:
# Remove the InvokeCommandError because it's useless information
x = traceback.format_exception(type(err), err, err.__traceback__)
error_string = ""
for line in x:
if "direct cause of the following" in line:
break
error_string += line.replace("*", "") + "\n" if line.strip() != "" else ""
return error_string
def _format_error_location(src) -> str:
DM = src.guild is None
return "DM" if DM else f"{src.channel.name} ({src.guild.name})"
def format_command_usage(ctx: Context) -> str:
return f"{ctx.author.display_name} in {_format_error_location(ctx)}: {ctx.message.content}"
def format_slash_command_usage(interaction: SlashInteraction) -> str:
# Create a string with the options used
options = " ".join(list(map(
lambda option: f"{option.name}: \"{option.value}\"",
interaction.data.options.values()
)))
command = f"{interaction.slash_command.name} {options or ''}"
return f"{interaction.author.display_name} in {_format_error_location(interaction)}: /{command}"