albertopoljak / Licensy

Discord bot that manages expiration of roles with subscriptions!
Other
58 stars 29 forks source link

Refactor database_handler Hacktoberfest #1

Closed albertopoljak closed 5 years ago

albertopoljak commented 5 years ago

Hacktoberfest , info below

albertopoljak commented 5 years ago

Lot of methods can be just querries that will call getter/setter methods.

Example:

async def change_default_license_expiration(self, guild_id: int, expiration_hours: int):
        query = "UPDATE GUILDS SET DEFAULT_LICENSE_DURATION_HOURS=? WHERE GUILD_ID=?"
        await self.connection.execute(query, (expiration_hours, guild_id))
        await self.connection.commit()

Last 2 lines can be separated into a universal method that does just those 2 lines and taking arguments query and query parameters. Example

async def update_database(self, query, *args):
    """
    Use for insert/update statements
    :param query: query to execute
    :param args: arguments, results in a tuple
    """
    await self.connection.execute(query, args)
    await self.connection.commit()

So then in the first method we would have something like this:

async def change_default_license_expiration(self, guild_id: int, expiration_hours: int):
        query = "UPDATE GUILDS SET DEFAULT_LICENSE_DURATION_HOURS=? WHERE GUILD_ID=?"
        await self.update_database(query, expiration_hours, guild_id)
grimmjow8 commented 5 years ago

I'm willing to give this a try.

albertopoljak commented 5 years ago

Sure go ahead and good luck :) I'll keep updating some misc code but won't touch database_handler or anything that uses it.