diff --git a/notsobot/notsobot.py b/notsobot/notsobot.py index 16a36ab19b..cedc0eda19 100644 --- a/notsobot/notsobot.py +++ b/notsobot/notsobot.py @@ -219,11 +219,9 @@ async def truncate(self, channel, msg): async def safe_send(self, ctx, text, file, file_size): if not ctx.channel.permissions_for(ctx.me).send_messages: - file.close() return if not ctx.channel.permissions_for(ctx.me).attach_files: await ctx.send("I don't have permission to attach files.") - file.close() return BASE_FILESIZE_LIMIT = 8388608 if ctx.guild and file_size < ctx.guild.filesize_limit: @@ -232,7 +230,6 @@ async def safe_send(self, ctx, text, file, file_size): await ctx.send(content=text, file=file) else: await ctx.send("The contents of this command is too large to upload!") - file.close() @staticmethod async def determine_mime_type(image_header: str): @@ -300,7 +297,6 @@ def do_magik(self, scale, img): list_imgs = [] count = 0 i = wand.image.Image(file=img) - img.close() i.transform_colorspace("cmyk") i.format = "png" if i.size >= (3000, 3000): @@ -326,9 +322,6 @@ def do_magik(self, scale, img): list_imgs.append(magikd) filename = self.random_filename(True, "png") file = discord.File(list_imgs[0], filename=filename) - i.close() - for image in list_imgs: - image.close() return file, file_size except Exception: log.error("Error processing magik", exc_info=True) @@ -366,7 +359,6 @@ async def magik(self, ctx, urls: ImageFinder = None, scale: int = 2, scale_msg: return await self.safe_send(ctx, scale_msg, file, file_size) - b.close() def do_gmagik(self, image, frame_delay): final = BytesIO() @@ -428,10 +420,6 @@ def do_gmagik(self, image, frame_delay): final.seek(0) filename = self.random_filename(True, "gif") file = discord.File(final, filename=filename) - img.close() - image.close() - new_image.close() - final.close() return file, file_size @commands.command() @@ -470,7 +458,6 @@ async def gmagik(self, ctx, urls: ImageFinder = None, frame_delay: int = 1): await msg.delete() except discord.errors.NotFound: pass - b.close() @commands.command() @commands.bot_has_permissions(attach_files=True) @@ -541,12 +528,10 @@ def make_caption_image(b, text, color, font, x, y, is_gif): new_image.sequence.append(frame) new_image.save(file=final) i.save(file=final) - i.close() file_size = final.tell() final.seek(0) filename = f"caption.{'png' if not is_gif else 'gif'}" file = discord.File(final, filename=filename) - final.close() return file, file_size loop = asyncio.get_running_loop() @@ -557,9 +542,7 @@ def make_caption_image(b, text, color, font, x, y, is_gif): file, file_size = await asyncio.wait_for(task, timeout=60) except asyncio.TimeoutError: return await ctx.send("That image is too large.") - b.close() await ctx.send(file=file) - file.close() def trigger_image(self, path: BytesIO, t_path: BytesIO) -> Tuple[discord.File, int]: final = BytesIO() @@ -599,12 +582,10 @@ def trigger_image(self, path: BytesIO, t_path: BytesIO) -> Tuple[discord.File, i for frame in img.sequence: frame.delay = 2 img.save(file=final) - img.close() file_size = final.tell() final.seek(0) filename = self.random_filename(True, "gif") file = discord.File(final, filename=filename) - final.close() return file, file_size @commands.command() @@ -643,7 +624,6 @@ async def triggered(self, ctx, urls: ImageFinder = None): except asyncio.TimeoutError: return await ctx.send("Error creating trigger image") await self.safe_send(ctx, None, file, file_size) - img.close() @commands.command(aliases=["aes"]) @commands.max_concurrency(1, commands.BucketType.guild) @@ -678,8 +658,6 @@ def do_ascii(self, text): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - final.close() - imgs.close() return file, txt, file_size except Exception: log.exception("Error making ascii text") @@ -739,8 +717,6 @@ def generate_ascii(self, image): file_size = final.tell() final.seek(0) # file = discord.File(final, filename="iascii.png") - # final.close() - img.close() return final, file_size async def check_font_file(self): @@ -787,7 +763,6 @@ async def iascii(self, ctx, urls: ImageFinder = None): ) filename = self.random_filename(True, "png") file = discord.File(temp, filename) - temp.close() await self.safe_send(ctx, None, file, file_size) def do_gascii(self, b): @@ -809,8 +784,6 @@ def do_gascii(self, b): temp.seek(0) filename = self.random_filename(True, "gif") file = discord.File(temp, filename=filename) - temp.close() - image.close() return file, file_size except Exception: raise @@ -904,8 +877,6 @@ def make_rip(image, text): final.seek(0) filename = self.random_filename(True, "jpg") file = discord.File(final, filename=filename) - final.close() - img.close() return file, file_size loop = asyncio.get_running_loop() @@ -965,10 +936,6 @@ def make_merge(list_im): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - final.close() - for i in imgs: - i.close() - imgs_comb.close() return file, file_size if len(list_im) < 2: @@ -982,7 +949,6 @@ def make_merge(list_im): "That image is either too large or image filetype is unsupported." ) await self.safe_send(ctx, None, file, file_size) - b.close() @commands.command() async def emojify(self, ctx, *, txt: str): @@ -1037,8 +1003,6 @@ def make_jpeg(b): final.seek(0) filename = self.random_filename(True, "jpg") file = discord.File(final, filename=filename) - final.close() - img.close() return file, file_size loop = asyncio.get_running_loop() @@ -1050,7 +1014,6 @@ def make_jpeg(b): "That image is either too large or image filetype is unsupported." ) await self.safe_send(ctx, None, file, file_size) - b.close() def do_vw(self, b, txt): im = Image.open(b) @@ -1062,8 +1025,6 @@ def do_vw(self, b, txt): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - final.close() - im.close() return file, file_size @commands.command(aliases=["vaporwave", "vape", "vapewave"]) @@ -1127,8 +1088,6 @@ def make_mc(b, txt): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - final.close() - image.close() return file, file_size try: @@ -1233,8 +1192,6 @@ def add_watermark(b, wmm, x, y, transparency, wm_gif=False): image=wm, left=final_x, top=final_y, transparency=transparency ) new_img.save(file=final) - new_img.close() - wm.close() elif is_gif and not wm_gif: log.debug("The base image is a gif") @@ -1253,9 +1210,6 @@ def add_watermark(b, wmm, x, y, transparency, wm_gif=False): ) new_image.sequence.append(frame) new_image.save(file=final) - new_image.close() - new_img.close() - wm.close() else: log.debug("The mark is a gif") with wand.image.Image() as new_image: @@ -1283,15 +1237,11 @@ def add_watermark(b, wmm, x, y, transparency, wm_gif=False): new_frame.delay = frame.delay new_image.save(file=final) - new_image.close() - new_img.close() size = final.tell() final.seek(0) filename = f"watermark.{'gif' if is_gif or wm_gif else 'png'}" file = discord.File(final, filename=filename) - final.close() - img.close() return file, size try: @@ -1303,8 +1253,6 @@ def add_watermark(b, wmm, x, y, transparency, wm_gif=False): except asyncio.TimeoutError: return await ctx.send("That image is too large.") await self.safe_send(ctx, None, file, file_size) - b.close() - wmm.close() def do_glitch(self, b, amount, seed, iterations): img = Image.open(b) @@ -1315,8 +1263,6 @@ def do_glitch(self, b, amount, seed, iterations): image.save(img_bytes, format="JPEG") img_bytes.seek(0) image = jpglitch.Jpeg(bytearray(img_bytes.getvalue()), amount, seed, iterations) - img_bytes.close() - final = BytesIO() final.name = self.random_filename(True, "jpg") image.save_image(final) file_size = final.tell() @@ -1338,9 +1284,6 @@ def do_glitch(self, b, amount, seed, iterations): file_size = final.tell() filename = self.random_filename(True, "gif") file = discord.File(final, filename=filename) - b.close() - final.close() - img.close() return file, file_size @commands.command(aliases=["jpglitch"]) @@ -1382,7 +1325,6 @@ async def glitch( msg = f"Iterations: `{iterations}` | Amount: `{amount}` | Seed: `{seed}`" await self.safe_send(ctx, msg, file, file_size) - b.close() @commands.command(aliases=["pixel"]) @commands.bot_has_permissions(attach_files=True) @@ -1409,7 +1351,6 @@ async def pixelate(self, ctx, urls: ImageFinder = None, pixels: int = 9): except asyncio.TimeoutError: return await ctx.send("The image is too large.") await self.safe_send(ctx, None, file, file_size) - b.close() def make_pixel(self, b: BytesIO, pixels: int) -> Tuple[discord.File, int]: bg = (0, 0, 0) @@ -1428,16 +1369,12 @@ def make_pixel(self, b: BytesIO, pixels: int) -> Tuple[discord.File, int]: final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - b.close() - final.close() - img.close() return file, file_size def make_pixel_gif(self, b, pixels): try: image = Image.open(b) gif_list = [frame.copy() for frame in ImageSequence.Iterator(image)] - image.close() except IOError: return ":warning: Cannot load gif." bg = (0, 0, 0) @@ -1460,11 +1397,6 @@ def make_pixel_gif(self, b, pixels): final.seek(0) filename = self.random_filename(True, "gif") file = discord.File(final, filename=filename) - b.close() - final.close() - img.close() - for image_file in img_list: - image_file.close() return file, file_size def do_waaw(self, b): @@ -1479,9 +1411,6 @@ def do_waaw(self, b): h2.flop() h1.save(file=f) h2.save(file=f2) - h1.close() - h2.close() - b.close() f.seek(0) f2.seek(0) list_im = [f, f2] @@ -1495,11 +1424,6 @@ def do_waaw(self, b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - f.close() - f2.close() - final.close() - for image_file in list_im: - image_file.close() return file, file_size # Thanks to Iguniisu#9746 for the idea @@ -1525,7 +1449,6 @@ async def waaw(self, ctx, urls: ImageFinder = None): "The image is either too large or you're missing delegates for this image format." ) await self.safe_send(ctx, None, file, file_size) - b.close() def do_haah(self, b): f = BytesIO() @@ -1539,9 +1462,6 @@ def do_haah(self, b): h2.flop() h1.save(file=f) h2.save(file=f2) - h1.close() - h2.close() - b.close() f.seek(0) f2.seek(0) list_im = [f2, f] @@ -1555,11 +1475,6 @@ def do_haah(self, b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - f.close() - f2.close() - final.close() - for image_file in list_im: - image_file.close() return file, file_size @commands.command(aliases=["magik4", "mirror2"]) @@ -1584,7 +1499,6 @@ async def haah(self, ctx, urls: ImageFinder = None): "The image is either too large or you're missing delegates for this image format." ) await self.safe_send(ctx, None, file, file_size) - b.close() def do_woow(self, b): f = BytesIO() @@ -1599,9 +1513,6 @@ def do_woow(self, b): h2.rotate(degree=180) h1.save(file=f) h2.save(file=f2) - h1.close() - h2.close() - b.close() f.seek(0) f2.seek(0) list_im = [f, f2] @@ -1615,11 +1526,6 @@ def do_woow(self, b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - f.close() - f2.close() - final.close() - for image_file in list_im: - image_file.close() return file, file_size @commands.command(aliases=["magik5", "mirror3"]) @@ -1644,7 +1550,6 @@ async def woow(self, ctx, urls: ImageFinder = None): "The image is either too large or you're missing delegates for this image format." ) await self.safe_send(ctx, None, file, file_size) - b.close() def do_hooh(self, b): f = BytesIO() @@ -1660,9 +1565,6 @@ def do_hooh(self, b): h2.flop() h1.save(file=f) h2.save(file=f2) - h1.close() - h2.close() - b.close() f.seek(0) f2.seek(0) list_im = [f, f2] @@ -1676,11 +1578,6 @@ def do_hooh(self, b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - f.close() - f2.close() - final.close() - for image_file in list_im: - image_file.close() return file, file_size @commands.command(aliases=["magik6", "mirror4"]) @@ -1705,7 +1602,6 @@ async def hooh(self, ctx, urls: ImageFinder = None): "The image is either too large or you're missing delegates for this image format." ) await self.safe_send(ctx, None, file, file_size) - b.close() @commands.command() @commands.bot_has_permissions(attach_files=True) @@ -1724,14 +1620,12 @@ async def flipimg(self, ctx, urls: ImageFinder = None): def flip_img(b): with Image.open(b) as img: image = ImageOps.flip(img) - img.close() with BytesIO() as final: image.save(final, "png") file_size = final.tell() final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - final.close() return file, file_size loop = asyncio.get_running_loop() @@ -1743,7 +1637,6 @@ def flip_img(b): "The image is either too large or image filetype is unsupported." ) await self.safe_send(ctx, None, file, file_size) - b.close() @commands.command() @commands.bot_has_permissions(attach_files=True) @@ -1768,7 +1661,6 @@ def flop_img(b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - image.close() return file, file_size loop = asyncio.get_running_loop() @@ -1778,7 +1670,6 @@ def flop_img(b): except asyncio.TimeoutError: return await ctx.send("That image is too large.") await self.safe_send(ctx, None, file, file_size) - b.close() @commands.command(aliases=["inverse", "negate"]) @commands.bot_has_permissions(attach_files=True) @@ -1803,7 +1694,6 @@ def invert_img(b): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - image.close() return file, file_size loop = asyncio.get_running_loop() @@ -1815,7 +1705,6 @@ def invert_img(b): "That image is either too large or image filetype is unsupported." ) await self.safe_send(ctx, None, file, file_size) - b.close() @commands.command() @commands.bot_has_permissions(attach_files=True) @@ -1839,7 +1728,6 @@ def rotate_img(b, degrees): final.seek(0) filename = self.random_filename(True, "png") file = discord.File(final, filename=filename) - image.close() return file, file_size loop = asyncio.get_running_loop() @@ -1851,4 +1739,3 @@ def rotate_img(b, degrees): "That image is either too large or image filetype is unsupported." ) await self.safe_send(ctx, f"Rotated: `{degrees}°`", file, file_size) - b.close()