mirror of https://github.com/stijndcl/didier
				
				
				
			
						commit
						34fe8a0feb
					
				| 
						 | 
					@ -3,7 +3,7 @@ import datetime
 | 
				
			||||||
import discord
 | 
					import discord
 | 
				
			||||||
from discord.ext import commands
 | 
					from discord.ext import commands
 | 
				
			||||||
from functions import checks, easterEggResponses
 | 
					from functions import checks, easterEggResponses
 | 
				
			||||||
from functions.database import stats, muttn, custom_commands
 | 
					from functions.database import stats, muttn, custom_commands, commands as command_stats
 | 
				
			||||||
import pytz
 | 
					import pytz
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import traceback
 | 
					import traceback
 | 
				
			||||||
| 
						 | 
					@ -96,6 +96,8 @@ class Events(commands.Cog):
 | 
				
			||||||
                                    "DM" if DM else "{} ({})".format(ctx.channel.name, ctx.guild.name),
 | 
					                                    "DM" if DM else "{} ({})".format(ctx.channel.name, ctx.guild.name),
 | 
				
			||||||
                                    ctx.message.content))
 | 
					                                    ctx.message.content))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        command_stats.invoked()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @commands.Cog.listener()
 | 
					    @commands.Cog.listener()
 | 
				
			||||||
    async def on_command_error(self, ctx, err):
 | 
					    async def on_command_error(self, ctx, err):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,6 +24,13 @@ Used in stats.py - Stats.Channels & the daily task that updates the message coun
 | 
				
			||||||
    0 channel_id: bigint, unique, primary key           | The channel id of this channel
 | 
					    0 channel_id: bigint, unique, primary key           | The channel id of this channel
 | 
				
			||||||
    1 message_count: numeric                            | The amount of messages sent in this channel
 | 
					    1 message_count: numeric                            | The amount of messages sent in this channel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### command_stats
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Used in commands.py, tracks counters of how many commands are invoked per day.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    0 day: date, unique, primary key                    | The date that is being tracked
 | 
				
			||||||
 | 
					    1 amount: integer                                   | The total amount of commands that were invoked on this day
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### currencytable
 | 
					### currencytable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Used in all Currency commands (Dinks, Bank, Bitcoin, Gambling, ...)
 | 
					Used in all Currency commands (Dinks, Bank, Bitcoin, Gambling, ...)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,78 @@
 | 
				
			||||||
 | 
					from functions.database import utils
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def invoked():
 | 
				
			||||||
 | 
					    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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cursor.execute("INSERT INTO command_stats(day, amount) VALUES (%s, 1)", (date,))
 | 
				
			||||||
 | 
					    connection.commit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _update(date: str):
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Increase the counter for a given day
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # Date wasn't present yet, add it with a value of 1
 | 
				
			||||||
 | 
					    if not _is_present(date):
 | 
				
			||||||
 | 
					        _add_date(date)
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    connection = utils.connect()
 | 
				
			||||||
 | 
					    cursor = connection.cursor()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cursor.execute("""
 | 
				
			||||||
 | 
					                    UPDATE command_stats
 | 
				
			||||||
 | 
					                        SET amount = amount + 1
 | 
				
			||||||
 | 
					                    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()
 | 
				
			||||||
		Loading…
	
		Reference in New Issue