2021-09-03 20:40:03 +02:00
|
|
|
from enum import IntEnum
|
|
|
|
|
2021-06-19 17:45:26 +02:00
|
|
|
from functions.database import utils
|
2021-09-03 20:40:03 +02:00
|
|
|
from functions.stringFormatters import leading_zero as lz
|
2021-06-19 17:45:26 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
2021-09-03 20:40:03 +02:00
|
|
|
class InvocationType(IntEnum):
|
|
|
|
TextCommand = 0
|
|
|
|
SlashCommand = 1
|
|
|
|
ContextMenu = 2
|
2021-06-19 17:45:26 +02:00
|
|
|
|
|
|
|
|
2021-09-03 20:40:03 +02:00
|
|
|
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, inv)
|
2021-06-19 17:45:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _is_present(date: str) -> bool:
|
|
|
|
"""
|
|
|
|
Check if a given date is present in the database
|
|
|
|
"""
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
cursor.execute("SELECT * FROM command_stats WHERE day = %s", (date,))
|
|
|
|
res = cursor.fetchone()
|
|
|
|
|
|
|
|
if res:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _add_date(date: str):
|
|
|
|
"""
|
|
|
|
Add a date into the db
|
|
|
|
"""
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
2021-09-03 20:40:03 +02:00
|
|
|
cursor.execute("INSERT INTO command_stats(day, commands, slash_commands, context_menus) VALUES (%s, 0, 0, 0)", (date,))
|
2021-06-19 17:45:26 +02:00
|
|
|
connection.commit()
|
|
|
|
|
|
|
|
|
2021-09-03 20:40:03 +02:00
|
|
|
def _update(date: str, inv: InvocationType):
|
2021-06-19 17:45:26 +02:00
|
|
|
"""
|
|
|
|
Increase the counter for a given day
|
|
|
|
"""
|
2021-09-03 20:40:03 +02:00
|
|
|
# Date wasn't present yet, add it
|
2021-06-19 17:45:26 +02:00
|
|
|
if not _is_present(date):
|
|
|
|
_add_date(date)
|
|
|
|
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
2021-09-03 20:40:03 +02:00
|
|
|
column_name = ["commands", "slash_commands", "context_menus"][inv.value]
|
|
|
|
|
|
|
|
# String formatting is safe here because the input comes from above ^
|
|
|
|
cursor.execute(f"""
|
2021-06-19 17:45:26 +02:00
|
|
|
UPDATE command_stats
|
2021-09-03 20:40:03 +02:00
|
|
|
SET {column_name} = {column_name} + 1
|
2021-06-19 17:45:26 +02:00
|
|
|
WHERE day = %s
|
|
|
|
""", (date,))
|
|
|
|
connection.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_all():
|
|
|
|
"""
|
|
|
|
Get all rows
|
|
|
|
"""
|
|
|
|
connection = utils.connect()
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
cursor.execute("SELECT * FROM command_stats")
|
|
|
|
return cursor.fetchall()
|