countdown-bot

A Discord bot that runs countdown games and generates analytics
git clone https://git.ashermorgan.net/countdown-bot/
Log | Files | Refs | README

commit ede67eb6d5db3af8128ffc68f71da75cdef4b019
parent 0d2b5230fb4fd7833783beb8628a2c9fe775a490
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Fri, 26 Apr 2024 14:21:56 -0700

Use dict row factory

Diffstat:
Mcountdown_bot/__main__.py | 2+-
Mcountdown_bot/botUtilities.py | 20++++++++++----------
Mcountdown_bot/utilitiesCog.py | 7+++----
3 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/countdown_bot/__main__.py b/countdown_bot/__main__.py @@ -22,7 +22,7 @@ logging.basicConfig( # Connect to database databaseSessionMaker = getSessionMaker(os.environ.get("DATABASE")) -db_connection = psycopg.connect(os.environ.get("DATABASE2")) +db_connection = psycopg.connect(os.environ.get("DATABASE2"), row_factory=psycopg.rows.dict_row) # Run bot bot = CountdownBot(databaseSessionMaker, [os.environ.get("PREFIX", "!")], db_connection) diff --git a/countdown_bot/botUtilities.py b/countdown_bot/botUtilities.py @@ -167,7 +167,7 @@ def isCountdown(cur, id): cur.execute("CALL isCountdown(%s, null);", (id,)) - return cur.fetchone()[0] + return cur.fetchone()["result"] @@ -230,13 +230,13 @@ def getContextCountdown2(cur, ctx): # Channel inside a server cur.execute("CALL getServerContextCountdown(%s, %s, %s, null);", (ctx.channel.guild.id, ctx.channel.id, ctx.prefix)) - return cur.fetchone()[0] + return cur.fetchone()["countdownid"] if (isinstance(ctx.channel, discord.channel.DMChannel)): # DM with a user cur.execute("CALL getUserContextCountdown(%s, null);", (ctx.author.id,)) - return cur.fetchone()[0] + return cur.fetchone()["countdownid"] return None @@ -260,7 +260,7 @@ def getPrefix(conn, ctx, default): cur.execute("SELECT * FROM getPrefixes(%s, %s);", (ctx.channel.guild.id, ctx.channel.id)) prefixes = cur.fetchall() - return [x[0] for x in prefixes] if prefixes else default + return [x["prefix"] for x in prefixes] if prefixes else default @@ -298,19 +298,19 @@ async def addMessage(cur, message): result = cur.fetchone() # Process result - if result[0] == 'badNumber': + if result["result"] == 'badNumber': await message.add_reaction("❌") - if result[0] == 'badUser': + if result["result"] == 'badUser': await message.add_reaction("⛔") - if result[1]: + if result["pin"]: await message.pin() - if result[2]: + if result["reactions"]: cur.execute("SELECT * FROM getReactions(%s, %s);", (message.channel.id, number)) for reaction in cur.fetchall(): - await message.add_reaction(reaction[0]) + await message.add_reaction(reaction["value"]) - return result[0] == 'good' + return result["result"] == 'good' diff --git a/countdown_bot/utilitiesCog.py b/countdown_bot/utilitiesCog.py @@ -80,7 +80,7 @@ class Utilities(commands.Cog): embed.description = f"**Countdown Channel:** <#{countdown}>\n" cur.execute("SELECT * FROM getPrefixes(NULL, %s);", (countdown,)) - prefixes = [x[0] for x in cur.fetchall()] + prefixes = [x["prefix"] for x in cur.fetchall()] embed.description += f"**Command Prefixes:** `{'`, `'.join(prefixes)}`\n" # embed.description += f"**Countdown Timezone:** {countdown.getTimezone()}\n" @@ -91,8 +91,8 @@ class Utilities(commands.Cog): embed.description += f"**Reactions:** none\n" else: embed.description += f"**Reactions:**\n" - for number in reversed(list(set([x[1] for x in reactions]))): - embed.description += f"**-** #{number}: {', '.join([x[0] for x in reactions if x[1] == number])}\n" + for number in reversed(list(set([x["number"] for x in reactions]))): + embed.description += f"**-** #{number}: {', '.join([x["value"] for x in reactions if x["number"] == number])}\n" embed.description += f"\nUse `{ctx.prefix}help config` to view more information about settings\n" embed.description += f"Use `{ctx.prefix}config <key> <value>` to modify settings\n" @@ -117,7 +117,6 @@ class Utilities(commands.Cog): raise CommandError(f"Invalid number: `{args[0]}`") if (number < 0): raise CommandError("Number must be greater than zero") - print(list(args[1:])) cur.execute("CALL setReactions(%s, %s, %s);", (countdown, number, list(args[1:]))) if (len(args) == 1):