Small hotfixes to fix the entire starboard system

pull/9/head
sgoudham 4 years ago
parent dcbfddcb95
commit 7d7b957ebd

@ -323,10 +323,9 @@ class Bot(commands.Bot):
# Store it in cache # Store it in cache
else: else:
if result: if result:
self.starboard_messages_cache[root_message_id, guild_id] = {"channel_id": result["channel_id"], self.starboard_messages_cache[root_message_id, guild_id] = {
"star_message_id": "star_message_id": result["star_message_id"],
result["star_message_id"], "stars": result["stars"]}
"stars": result["stars"]}
return self.starboard_messages_cache[root_message_id, guild_id]["star_message_id"], \ return self.starboard_messages_cache[root_message_id, guild_id]["star_message_id"], \
self.starboard_messages_cache[root_message_id, guild_id]["stars"] self.starboard_messages_cache[root_message_id, guild_id]["stars"]

@ -22,11 +22,17 @@ from discord.ext.commands import Cog, group, bot_has_permissions, has_permission
async def send_starboard_and_update_db(self, payload, action): async def send_starboard_and_update_db(self, payload, action):
"""Send the starboard embed and update database/cache""" """Send the starboard embed and update database/cache"""
deletion = False
if (starboard := self.bot.get_starboard(payload.guild_id)) and payload.emoji.name == "": if (starboard := self.bot.get_starboard(payload.guild_id)) and payload.emoji.name == "":
message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id) message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
if not message.author.bot and payload.member.id != message.author.id: if action == "added":
check = not message.author.bot and payload.member.id != message.author.id
else:
check = not message.author.bot
if check:
channel = self.bot.get_channel(starboard) channel = self.bot.get_channel(starboard)
msg_id, stars = await self.bot.check_starboard_messages_cache(message.id, payload.guild_id) msg_id, stars = await self.bot.check_starboard_messages_cache(message.id, payload.guild_id)
new_stars = stars + 1 if action == "added" else stars - 1 new_stars = stars + 1 if action == "added" else stars - 1
@ -46,34 +52,36 @@ async def send_starboard_and_update_db(self, payload, action):
if new_stars <= 0: if new_stars <= 0:
try: try:
deletion = True
star_message = await channel.fetch_message(msg_id) star_message = await channel.fetch_message(msg_id)
await star_message.delete() await star_message.delete()
except NotFound: except Exception:
pass pass
# Setup up pool connection if deletion:
pool = self.bot.db # Setup up pool connection
async with pool.acquire() as conn: pool = self.bot.db
async with pool.acquire() as conn:
# Insert the starboard message in the database # Insert the starboard message in the database
try: try:
update_query = """DELETE FROM starboard_messages WHERE root_message_id = $1 AND guild_id = $2""" update_query = """DELETE FROM starboard_messages WHERE root_message_id = $1 AND guild_id = $2"""
await conn.execute(update_query, message.id, payload.guild_id) await conn.execute(update_query, message.id, payload.guild_id)
# Catch errors # Catch errors
except asyncpg.PostgresError as e: except asyncpg.PostgresError as e:
print( print(
f"PostGres Error: Starboard_Message Record Could Not Be Deleted For Guild {payload.guild_id}", f"PostGres Error: Starboard_Message Record Could Not Be Deleted For Guild {payload.guild_id}",
e) e)
# Update cache # Update cache
else: else:
self.bot.delete_starboard_messages(payload.guild_id) self.bot.delete_starboard_messages(payload.guild_id)
# Release connection back to pool # Release connection back to pool
finally: finally:
await pool.release(conn) await pool.release(conn)
return return
if not stars: if not stars:
star_message = await channel.send(embed=embed) star_message = await channel.send(embed=embed)
@ -182,7 +190,7 @@ class Starboard(Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@group(name="starboard", case_insensitive=True, usage="`<setup|update|delete>`") @group(name="starboarde", case_insensitive=True, usage="`<setup|update|delete>`")
@bot_has_permissions(embed_links=True) @bot_has_permissions(embed_links=True)
@has_permissions(manage_guild=True) @has_permissions(manage_guild=True)
async def starboard(self, ctx): async def starboard(self, ctx):

Loading…
Cancel
Save