From 9a18af6e590d298325d103cc2b4554f657a16327 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 3 Sep 2020 12:53:09 +0100 Subject: [PATCH] Not manually releasing connections back to the pool anymore --- bot/__init__.py | 40 ---------------------------------- cogs/guild.py | 50 +++++++++---------------------------------- cogs/help.py | 29 +++++++++++++++++++++---- cogs/owner.py | 4 ---- cogs/relationship.py | 8 ------- cogs/starboard.py | 51 +++++++------------------------------------- 6 files changed, 43 insertions(+), 139 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index aafa3a13..87d50a69 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -243,10 +243,6 @@ class Bot(commands.Bot): return self.member_cache.cache[member_id, guild_id] - # Release connection back to pool - finally: - await pool.release(conn) - # --------------------------------------------!End Cache Section!--------------------------------------------------- # --------------------------------------------!Starboard Section!--------------------------------------------------- @@ -351,10 +347,6 @@ class Bot(commands.Bot): else: return None, 0 - # Release connection back to pool - finally: - await pool.release(conn) - # --------------------------------------------!EndStarboard Section!------------------------------------------------- # --------------------------------------------!Modmail Section!----------------------------------------------------- @@ -411,10 +403,6 @@ class Bot(commands.Bot): else: self.enso_cache[guild_id]["roles_persist"] = value - # Release connection back to pool - finally: - await pool.release(conn) - # --------------------------------------------!End RolePersist Section!--------------------------------------------- # --------------------------------------------!ModLogs Section!----------------------------------------------------- @@ -449,10 +437,6 @@ class Bot(commands.Bot): # Store in cache self.enso_cache[ctx.guild.id]["modlogs"] = channel_id - # Release connection back to pool - finally: - await pool.release(conn) - def remove_modlog_channel(self, guild_id): """Remove the value of modlog for the guild specified""" @@ -492,10 +476,6 @@ class Bot(commands.Bot): # Store in cache self.enso_cache[ctx.guild.id]["prefix"] = prefix - # Release connection back to pool - finally: - await pool.release(conn) - def get_prefix_for_guild(self, guild_id): """Get the prefix of the guild that the user is in""" @@ -547,10 +527,6 @@ class Bot(commands.Bot): result["muted_roles"] = role_ids print(rowcount, f"Roles Added For User {member} in {ctx.guild}") - # Release connection back to pool - finally: - await pool.release(conn) - async def clear_roles(self, member): """Clear the roles when the user has been unmuted""" @@ -575,10 +551,6 @@ class Bot(commands.Bot): result["muted_roles"] = None print(rowcount, f"Roles Cleared For User {member} in {member.guild.name}") - # Release connection back to pool - finally: - await pool.release(conn) - # --------------------------------------------!End Roles/Colour/Embed Section!-------------------------------------- # --------------------------------------------!Events Section!------------------------------------------------------ @@ -849,10 +821,6 @@ class Bot(commands.Bot): else: print(rowcount, f"{member} Left {member.guild.name}, Roles stored into Members") - # Release connection back to pool - finally: - await pool.release(conn) - async def on_guild_channel_delete(self, channel): """Deleting modlogs/modmail channel if it's deleted in the guild""" @@ -887,10 +855,6 @@ class Bot(commands.Bot): else: self.remove_modlog_channel(channel.guild.id) - # Release connection back to pool - finally: - await pool.release(conn) - # Delete all of the starboard information when the channel is deleted from the guild if channel.id == starboard: # Setup pool connection @@ -935,10 +899,6 @@ class Bot(commands.Bot): else: self.delete_modmail(channel.guild.id) - # Release connection back to pool - finally: - await pool.release(conn) - # --------------------------------------------!End Events Section!-------------------------------------------------- def execute(self): diff --git a/cogs/guild.py b/cogs/guild.py index 080b2f47..fe52464c 100644 --- a/cogs/guild.py +++ b/cogs/guild.py @@ -27,8 +27,9 @@ from discord.ext.commands import has_permissions, Cog, group, bot_has_permission command -# Method to ask the user if they want to be anonymous or not def anon_or_not(self, author): + """Method to ask the user if they want to be anonymous or not""" + # Set up embed to let the user how to start sending modmail AnonModMailEmbed = Embed(title="**Want to send it Anonymously?**", colour=self.bot.admin_colour, @@ -53,9 +54,9 @@ def anon_or_not(self, author): return AnonModMailEmbed -# Method to send an embed to to let the user know to type into chat def send_instructions(self, author): - # Set up embed to let the user know that they have aborted the modmail + """Method to send an embed to to let the user know to type into chat""" + SendModMailEmbed = Embed(title="**Please enter a message for it to be sent to the staff!**", colour=self.bot.admin_colour, timestamp=datetime.datetime.utcnow()) @@ -73,9 +74,9 @@ def send_instructions(self, author): return SendModMailEmbed -# Method to let the user know that the message must be above 50 characters def error_handling(self, author): - # Set up embed to let the user know that the message must be above 50 characters + """Method to let the user know that the message must be above 50 characters""" + ErrorHandlingEmbed = Embed( title="Uh Oh! Please make sure the message is above **50** and below **1024** characters!", colour=self.bot.admin_colour, @@ -94,9 +95,9 @@ def error_handling(self, author): return ErrorHandlingEmbed -# Method to send an embed into chat to let the user know that their mail has been sent successfully def message_sent_confirmation(self, author): - # Set up embed to let the user know that they have sent the mail + """Method to send an embed into chat to let the user know that their mail has been sent successfully""" + ConfirmationEmbed = Embed(title="**Message relayed to Staff!!**", colour=self.bot.admin_colour, timestamp=datetime.datetime.utcnow()) @@ -114,8 +115,9 @@ def message_sent_confirmation(self, author): return ConfirmationEmbed -# Method to actually allow the message to be sent to #mod-mail def send_modmail(self, msg, author): + """Method to actually allow the message to be sent to modmail logging channel""" + embed = Embed(title="Modmail", colour=self.bot.admin_colour, timestamp=datetime.datetime.utcnow()) @@ -288,10 +290,6 @@ class Guild(Cog): mod_log_setup = True await self.bot.storage_modlog_for_guild(ctx, user_channel.id, mod_log_setup) - # Release the connection back to the pool - finally: - await pool.release(conn) - @modlogs.command(name="update") async def mlupdate(self, ctx, user_channel: TextChannel): """Change the channel that your modlogs are sent to""" @@ -321,10 +319,6 @@ class Guild(Cog): mod_log_setup = False await self.bot.storage_modlog_for_guild(ctx, user_channel.id, mod_log_setup) - # Release the connection back to the pool - finally: - await pool.release(conn) - @modlogs.command("delete") async def mldelete(self, ctx): """Delete the existing modlogs channel""" @@ -408,10 +402,6 @@ class Guild(Cog): await self.bot.generate_embed(ctx, desc=text) return - # Release the connection back to the pool - finally: - await pool.release(conn) - # Set up embed to let the user how to start sending modmail desc = "React to this message if you want to send a message to the Staff Team!" \ "\n\n**React with ✅**" \ @@ -454,10 +444,6 @@ class Guild(Cog): # Store into cache self.bot.cache_store_modmail(ctx.guild.id, modmail.id, modmail_message.id, modmail_logging.id) - # Release connection back into pool - finally: - await pool.release(conn) - @mod_mail.command(name="update") async def mmupdate(self, ctx, modmail_logging_channel: TextChannel): """ @@ -486,10 +472,6 @@ class Guild(Cog): await self.bot.generate_embed(ctx, desc=text) return - # Release connection back to pool - finally: - await pool.release(conn) - # Setup up pool connection and cursor async with pool.acquire() as conn: @@ -511,10 +493,6 @@ class Guild(Cog): # Update cache self.bot.update_modmail(ctx.guild.id, modmail_logging_channel.id) - # Release connection back to pool - finally: - await pool.release(conn) - @mod_mail.command(name="delete") async def mmdelete(self, ctx): """Delete the entire modmail system from the guild""" @@ -540,10 +518,6 @@ class Guild(Cog): await self.bot.generate_embed(ctx, desc=text) return - # Release connection back to pool - finally: - await pool.release(conn) - # Setup up pool connection async with pool.acquire() as conn: @@ -565,10 +539,6 @@ class Guild(Cog): # Delete from cache self.bot.delete_modmail(ctx.guild.id) - # Release connection back to pool - finally: - await pool.release(conn) - @Cog.listener() async def on_raw_reaction_add(self, payload): """Listen for reactions for modmail channel""" diff --git a/cogs/help.py b/cogs/help.py index c375f87a..e4bcf7e5 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -26,7 +26,7 @@ from typing import Optional import discord from discord import Embed, DMChannel from discord.ext import commands -from discord.ext.commands import Cog, command, has_permissions, guild_only +from discord.ext.commands import Cog, command, has_permissions, guild_only, is_owner class CannotPaginate(Exception): @@ -63,7 +63,7 @@ class Pages: Our permissions for the channel. """ - def __init__(self, ctx, *, entries, per_page=6, show_entry_count=True): + def __init__(self, ctx, *, entries, per_page=8, show_entry_count=True): self.bot = ctx.bot self.prefix = ctx.prefix @@ -370,7 +370,7 @@ def _command_signature(cmd): class HelpPaginator(Pages): - def __init__(self, ctx, entries, *, per_page=8): + def __init__(self, ctx, entries, *, per_page=10): super().__init__(ctx, entries=entries, per_page=per_page) self.reaction_emojis.append(('\N{WHITE QUESTION MARK ORNAMENT}', self.show_bot_help)) self.total = len(entries) @@ -447,7 +447,7 @@ class HelpPaginator(Pages): entries = sorted(ctx.bot.commands, key=key) nested_pages = [] - per_page = 6 + per_page = 8 # 0: (cog, desc, commands) (max len == 9) # 1: (cog, desc, commands) (max len == 9) @@ -650,6 +650,27 @@ class Help(Cog): except Exception as ex: await ctx.send(f"**{ex}**") + @command(name="forceprefix") + @guild_only() + @is_owner() + async def override_prefix(self, ctx, new: Optional[str] = None): + """Override the prefix in any given guild (Owner only)""" + + # As long as a new prefix has been given and is less than 5 characters + if new and len(new) <= 5: + # Store the new prefix in the dictionary and update the database + await self.bot.storage_prefix_for_guild(ctx, new) + + # Making sure that errors are handled if prefix is above 5 characters + elif new and len(new) > 5: + await self.bot.generate_embed(ctx, desc="The guild prefix must be less than or equal to **5** characters!") + + # if no prefix was provided + elif not new: + # Grab the current prefix for the guild within the cached dictionary + await self.bot.generate_embed(ctx, + desc=f"**The current guild prefix is `{self.bot.get_prefix_for_guild(ctx.guild.id)}`**") + @command(name="prefix") @guild_only() @has_permissions(manage_guild=True) diff --git a/cogs/owner.py b/cogs/owner.py index ae7d9c05..2c095c37 100644 --- a/cogs/owner.py +++ b/cogs/owner.py @@ -90,10 +90,6 @@ class Owner(Cog): else: print(f"Record(s) Inserted Into Members") - # Release connection back to pool - finally: - await pool.release(conn) - @command(name="cache", hidden=True) @is_owner() async def set_cache(self, ctx, size: Optional[int]): diff --git a/cogs/relationship.py b/cogs/relationship.py index 4138f260..b57670ba 100644 --- a/cogs/relationship.py +++ b/cogs/relationship.py @@ -139,10 +139,6 @@ class Relationship(Cog): db_member["married"] = ctx.author.id db_member["married_date"] = message_time - # Release connection back to pool - finally: - await pool.release(conn) - # Congratulate them! desc = f"**Congratulations! 。゚( ゚^∀^゚)゚。 {ctx.author.mention} and {member.mention} are now married to each other!**" await self.bot.generate_embed(ctx, desc=desc) @@ -235,10 +231,6 @@ class Relationship(Cog): db_member["married"] = None db_member["married_date"] = None - # Release connection back to pool - finally: - await pool.release(conn) - # Congratulate them! desc = f"**૮( ´⁰▱๋⁰ )ა {ctx.author.mention} and {member.mention} are now divorced." \ f"\nI hope you two can find happiness in life with other people**" diff --git a/cogs/starboard.py b/cogs/starboard.py index 97dfecc0..51df3724 100644 --- a/cogs/starboard.py +++ b/cogs/starboard.py @@ -20,14 +20,6 @@ from discord import TextChannel, Embed, NotFound from discord.ext.commands import Cog, group, bot_has_permissions, has_permissions -def is_url_spoiler(self, text, url): - spoilers = self.spoilers.findall(text) - for spoiler in spoilers: - if url in spoiler: - return True - return False - - async def send_starboard_and_update_db(self, payload, action): """Send the starboard embed and update database/cache""" @@ -52,6 +44,7 @@ async def send_starboard_and_update_db(self, payload, action): value=f"**Channel:** {message.channel.mention}\n[Jump To Message]({message.jump_url})", inline=False) + # Send spoiler attachments as links if message.attachments: file = message.attachments[0] spoiler = file.is_spoiler() @@ -94,10 +87,6 @@ async def send_starboard_and_update_db(self, payload, action): else: self.bot.cache_store_starboard_message(message.id, payload.guild_id, None) - # Release connection back to pool - finally: - await pool.release(conn) - else: if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id) and not msg_id: star_message = await channel.send(embed=embed) @@ -137,10 +126,6 @@ async def send_starboard_and_update_db(self, payload, action): self.bot.cache_store_starboard_message(message.id, payload.guild_id, star_message.id) self.bot.update_starboard_message(message.id, payload.guild_id, new_stars) - # Release connection back to pool - finally: - await pool.release(conn) - async def get_starboard_from_db(self, ctx, action): """Get the starboard record from DB""" @@ -171,10 +156,6 @@ async def get_starboard_from_db(self, ctx, action): await self.bot.generate_embed(ctx, desc=text) return None - # Release the connection back to the pool - finally: - await pool.release(conn) - return not None @@ -223,10 +204,6 @@ class Starboard(Cog): # Update cache self.bot.update_starboard_min_stars(ctx.guild.id, stars) - # Release connection back to pool - finally: - await pool.release(conn) - elif stars <= 0: await self.bot.generate_embed(ctx, desc="Minimum Stars Must Be Over or Equal to 1!") @@ -255,16 +232,12 @@ class Starboard(Cog): # Send confirmation message else: text = "**Starboard** is successfully set up!" \ - f"\nRefer to **{ctx.prefix}help modmail** for more information" + f"\nRefer to **{ctx.prefix}help starboard** for more information" await self.bot.generate_embed(ctx, desc=text) # Store into cache self.bot.cache_store_starboard(ctx.guild.id, starboard_channel.id, 1) - # Release connection back into pool - finally: - await pool.release(conn) - @starboard.command(name="update") @bot_has_permissions(embed_links=True) async def sb_update(self, ctx, starboard_channel: TextChannel): @@ -296,10 +269,6 @@ class Starboard(Cog): # Update cache self.bot.update_starboard_channel(ctx.guild.id, starboard_channel.id) - # Release connection back to pool - finally: - await pool.release(conn) - @starboard.command(name="delete") @bot_has_permissions(embed_links=True) async def sb_delete(self, ctx): @@ -328,22 +297,18 @@ class Starboard(Cog): # Delete from cache self.bot.delete_starboard(ctx.guild.id) - # Release connection back to pool - finally: - await pool.release(conn) - - @Cog.listener() - async def on_raw_reaction_remove(self, payload): - """Removing reaction when a star is removed from the message""" - - await send_starboard_and_update_db(self, payload, action="removed") - @Cog.listener() async def on_raw_reaction_add(self, payload): """Listening for star reactions for any guilds that have starboard enabled""" await send_starboard_and_update_db(self, payload, action="added") + @Cog.listener() + async def on_raw_reaction_remove(self, payload): + """Editing the message if a star reaction was removed""" + + await send_starboard_and_update_db(self, payload, action="removed") + def setup(bot): bot.add_cog(Starboard(bot))