From 23c3ce4a10a439d316ceb99d9e55b13ccc2f179f Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:11:31 +0530 Subject: [PATCH 01/12] Make EmbedEngine in NKA Framework --- framework/isobot/embedengine.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 framework/isobot/embedengine.py diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py new file mode 100644 index 00000000..1430e492 --- /dev/null +++ b/framework/isobot/embedengine.py @@ -0,0 +1,33 @@ +"""The library used for designing and outputting Discord embeds.""" +import discord +from discord import Color + + +def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, color=None, footer: dict = None): + """Designs a Discord embed. + ----------------------------------------------------------- + The color argument is completely optional. + - If a color is set, it will return the embed in that color only. + - If the color is set as "rand", then it will return the embed with a random color. + - If a color is not set, it will appear as the Discord client's default embed color. + + Footer must be in a json format ONLY, otherwise it cannot be parsed. + - Correct format: {"text": "something", "img" "an image url"} + """ + if color == "rand": + color = Color.random() + local_embed = discord.Embed() + local_embed.title(title) + local_embed.description(desc) + if image is not None: + local_embed.set_image(url=image) + if thumbnail is not None: + local_embed.set_thumbnail(url=thumbnail) + if color is not None: + local_embed.colour(color) + if footer is not None: + try: + local_embed.set_footer(text=footer["text"], icon_url=footer["img"]) + except KeyError: + return "Unable to create embed: Failed to parse \"footer\" argument. Expected format: {\"text\": \"something\", \"img\" \"an image url\"}" + return local_embed From f751b03f2e387eaca7f97080c103067101b62c54 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:22:55 +0530 Subject: [PATCH 02/12] Add `/embedbuilder` command --- main.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/main.py b/main.py index 35af0252..136c1312 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import framework.isobot.colors import framework.isobank.authorize import framework.isobank.manager +import framework.isobot.embedengine import discord from discord.ext import commands, tasks from discord.ext.commands import * @@ -993,6 +994,23 @@ async def repo(ctx:SlashContext): localembed = discord.Embed(title="Source-code Repositories", description="See and contribute to **isobot lazer's [GitHub repository](https://github.com/PyBotDevs/isobot-lazer)**\nSee our **[GitHub organization](https://github.com/PyBotDevs)**", color=discord.Color.random()) await ctx.send(embed=localembed) +@slash.slash( + name="embedbuilder", + description="Builds a custom embed however you want", + options=[ + create_option(name="title", description="The title of the embed", option_type=3, required=True), + create_option(name="description", description="The body of the embed", option_type=3, required=True), + create_option(name="image_url", description="The main image you want to show for the embed (URL ONLY)", option_type=3, required=False), + create_option(name="thumbnail_url", description="The thumbnail image you want to show for the embed (URL ONLY)", option_type=3, required=False), + create_option(name="color", description="The embed's accent color", option_type=3, required=False), + create_option(name="footer", description="The footer of the embed | Syntax: {\"text\": text, \"img\": url}", option_type=3, required=False), + ] +) +async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str, thumbnail_url: str, color: str, footer: str): + footer = dict(footer) + await ctx.send("Embed Built!", hidden=True) + await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer=footer)) + # Initialization utils.ping.host() client.run(api.auth.get_token()) From eda72501bae7c74ef838300ac1fc7d53f739574a Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:28:29 +0530 Subject: [PATCH 03/12] Return optional arguments as `None` if not inputted by user --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 136c1312..95a13297 100644 --- a/main.py +++ b/main.py @@ -1006,7 +1006,7 @@ async def repo(ctx:SlashContext): create_option(name="footer", description="The footer of the embed | Syntax: {\"text\": text, \"img\": url}", option_type=3, required=False), ] ) -async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str, thumbnail_url: str, color: str, footer: str): +async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: str = None, footer: str = None): footer = dict(footer) await ctx.send("Embed Built!", hidden=True) await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer=footer)) From 61bb70e026e8d995638413d96d0f7e16c88623d6 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:32:18 +0530 Subject: [PATCH 04/12] Skip footer's dict conversion if argument not inputted by user --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 95a13297..0ac177c5 100644 --- a/main.py +++ b/main.py @@ -1007,7 +1007,7 @@ async def repo(ctx:SlashContext): ] ) async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: str = None, footer: str = None): - footer = dict(footer) + if footer is not None: footer = dict(footer) await ctx.send("Embed Built!", hidden=True) await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer=footer)) From 015c13aa5d87da25ec3af4ef7259145d9ce7c170 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:37:25 +0530 Subject: [PATCH 05/12] Move title, description and colour configs directly to `discord.Embed()` --- framework/isobot/embedengine.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index 1430e492..86dab3b4 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -16,15 +16,15 @@ def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, co """ if color == "rand": color = Color.random() - local_embed = discord.Embed() - local_embed.title(title) - local_embed.description(desc) + local_embed = discord.Embed( + title=title, + description=desc, + colour=color + ) if image is not None: local_embed.set_image(url=image) if thumbnail is not None: local_embed.set_thumbnail(url=thumbnail) - if color is not None: - local_embed.colour(color) if footer is not None: try: local_embed.set_footer(text=footer["text"], icon_url=footer["img"]) From 95081f5c8161b509080a057cf551c92c954d0917 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 10:41:37 +0530 Subject: [PATCH 06/12] Replace colour argument firing as `None` with `discord.Color.blurple()` --- framework/isobot/embedengine.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index 86dab3b4..413f4a9a 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -9,13 +9,15 @@ def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, co The color argument is completely optional. - If a color is set, it will return the embed in that color only. - If the color is set as "rand", then it will return the embed with a random color. - - If a color is not set, it will appear as the Discord client's default embed color. + - If a color is not set, it will appear as Discord's blurple embed color. Footer must be in a json format ONLY, otherwise it cannot be parsed. - Correct format: {"text": "something", "img" "an image url"} """ if color == "rand": color = Color.random() + elif color == None: + color = Color.blurple() local_embed = discord.Embed( title=title, description=desc, From d6b531dcfc53a9b2b4e801751be6d10337d3520a Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:26:25 +0530 Subject: [PATCH 07/12] Define `color` as int type --- framework/isobot/embedengine.py | 2 +- main.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index 413f4a9a..c662cc71 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -3,7 +3,7 @@ from discord import Color -def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, color=None, footer: dict = None): +def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, color:int=None, footer: dict = None): """Designs a Discord embed. ----------------------------------------------------------- The color argument is completely optional. diff --git a/main.py b/main.py index 0ac177c5..e6ce28e0 100644 --- a/main.py +++ b/main.py @@ -1002,11 +1002,11 @@ async def repo(ctx:SlashContext): create_option(name="description", description="The body of the embed", option_type=3, required=True), create_option(name="image_url", description="The main image you want to show for the embed (URL ONLY)", option_type=3, required=False), create_option(name="thumbnail_url", description="The thumbnail image you want to show for the embed (URL ONLY)", option_type=3, required=False), - create_option(name="color", description="The embed's accent color", option_type=3, required=False), + create_option(name="color", description="The embed's accent color", option_type=2, required=False), create_option(name="footer", description="The footer of the embed | Syntax: {\"text\": text, \"img\": url}", option_type=3, required=False), ] ) -async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: str = None, footer: str = None): +async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: int = None, footer: str = None): if footer is not None: footer = dict(footer) await ctx.send("Embed Built!", hidden=True) await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer=footer)) From feb9e034e87908d33f0fc116a54af2b59dfb7350 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:29:32 +0530 Subject: [PATCH 08/12] Fix incorrectly defined option type --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index e6ce28e0..f188b918 100644 --- a/main.py +++ b/main.py @@ -1002,7 +1002,7 @@ async def repo(ctx:SlashContext): create_option(name="description", description="The body of the embed", option_type=3, required=True), create_option(name="image_url", description="The main image you want to show for the embed (URL ONLY)", option_type=3, required=False), create_option(name="thumbnail_url", description="The thumbnail image you want to show for the embed (URL ONLY)", option_type=3, required=False), - create_option(name="color", description="The embed's accent color", option_type=2, required=False), + create_option(name="color", description="The embed's accent color", option_type=4, required=False), create_option(name="footer", description="The footer of the embed | Syntax: {\"text\": text, \"img\": url}", option_type=3, required=False), ] ) From 44979790b7c254f75aebd1b29e5d468cec94f30f Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:48:20 +0530 Subject: [PATCH 09/12] Drop complex json format for `footer` argument --- framework/isobot/embedengine.py | 22 ++++++++-------------- main.py | 8 ++++---- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index c662cc71..76ccbb32 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -3,7 +3,7 @@ from discord import Color -def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, color:int=None, footer: dict = None): +def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, color:int=None, footer_text: str = None, footer_img: str = None): """Designs a Discord embed. ----------------------------------------------------------- The color argument is completely optional. @@ -14,22 +14,16 @@ def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, co Footer must be in a json format ONLY, otherwise it cannot be parsed. - Correct format: {"text": "something", "img" "an image url"} """ - if color == "rand": - color = Color.random() - elif color == None: - color = Color.blurple() + if color == "rand": color = Color.random() + elif color == None: color = Color.blurple() local_embed = discord.Embed( title=title, description=desc, colour=color ) - if image is not None: - local_embed.set_image(url=image) - if thumbnail is not None: - local_embed.set_thumbnail(url=thumbnail) - if footer is not None: - try: - local_embed.set_footer(text=footer["text"], icon_url=footer["img"]) - except KeyError: - return "Unable to create embed: Failed to parse \"footer\" argument. Expected format: {\"text\": \"something\", \"img\" \"an image url\"}" + if image is not None: local_embed.set_image(url=image) + if thumbnail is not None: local_embed.set_thumbnail(url=thumbnail) + if footer_text is not None and footer_img is not None: local_embed.set_footer(text=footer_text, icon_url=footer_img) + elif footer_text is not None: local_embed.set_footer(text=footer_text) + elif footer_img is not None: local_embed.set_footer(icon_url=footer_img) return local_embed diff --git a/main.py b/main.py index f188b918..db5a8739 100644 --- a/main.py +++ b/main.py @@ -1003,13 +1003,13 @@ async def repo(ctx:SlashContext): create_option(name="image_url", description="The main image you want to show for the embed (URL ONLY)", option_type=3, required=False), create_option(name="thumbnail_url", description="The thumbnail image you want to show for the embed (URL ONLY)", option_type=3, required=False), create_option(name="color", description="The embed's accent color", option_type=4, required=False), - create_option(name="footer", description="The footer of the embed | Syntax: {\"text\": text, \"img\": url}", option_type=3, required=False), + create_option(name="footer_text", description="The text at the footer of the embed", option_type=3, required=False), + create_option(name="footer_icon_url", description="The icon you want to show in the embed (URL ONLY)", option_type=3, required=False) ] ) -async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: int = None, footer: str = None): - if footer is not None: footer = dict(footer) +async def embedbuilder(ctx:SlashContext, title: str, description: str, image_url: str = None, thumbnail_url: str = None, color: int = None, footer_text: str = None, footer_icon_url: str = None): await ctx.send("Embed Built!", hidden=True) - await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer=footer)) + await ctx.channel.send(embed=framework.isobot.embedengine.embed(title, description, image=image_url, thumbnail=thumbnail_url, color=color, footer_text=footer_text, footer_img=footer_icon_url)) # Initialization utils.ping.host() From 8314126b86d0f31b25e605eb75a07328580a732c Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:17:53 +0530 Subject: [PATCH 10/12] Set `-1` as matching value for `discord.Color.random()` --- framework/isobot/embedengine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index 76ccbb32..5f1e22c1 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -14,7 +14,7 @@ def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, co Footer must be in a json format ONLY, otherwise it cannot be parsed. - Correct format: {"text": "something", "img" "an image url"} """ - if color == "rand": color = Color.random() + if color == -1: color = Color.random() elif color == None: color = Color.blurple() local_embed = discord.Embed( title=title, From eb43dceacdf7dfe3b1d8fe527ec7755ace138ad3 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:20:23 +0530 Subject: [PATCH 11/12] Update `color` argument description to describe the use of `-1` value --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index db5a8739..50abe630 100644 --- a/main.py +++ b/main.py @@ -1002,7 +1002,7 @@ async def repo(ctx:SlashContext): create_option(name="description", description="The body of the embed", option_type=3, required=True), create_option(name="image_url", description="The main image you want to show for the embed (URL ONLY)", option_type=3, required=False), create_option(name="thumbnail_url", description="The thumbnail image you want to show for the embed (URL ONLY)", option_type=3, required=False), - create_option(name="color", description="The embed's accent color", option_type=4, required=False), + create_option(name="color", description="The embed's accent color (Use -1 for random color)", option_type=4, required=False), create_option(name="footer_text", description="The text at the footer of the embed", option_type=3, required=False), create_option(name="footer_icon_url", description="The icon you want to show in the embed (URL ONLY)", option_type=3, required=False) ] From b539d77cd0d159ed59a492e358398edc87dcccd2 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Sun, 14 Aug 2022 14:21:42 +0530 Subject: [PATCH 12/12] Remove json documentation for footer argument --- framework/isobot/embedengine.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/framework/isobot/embedengine.py b/framework/isobot/embedengine.py index 5f1e22c1..51db96ed 100644 --- a/framework/isobot/embedengine.py +++ b/framework/isobot/embedengine.py @@ -10,9 +10,6 @@ def embed(title: str, desc: str, *, image: str = None, thumbnail: str = None, co - If a color is set, it will return the embed in that color only. - If the color is set as "rand", then it will return the embed with a random color. - If a color is not set, it will appear as Discord's blurple embed color. - - Footer must be in a json format ONLY, otherwise it cannot be parsed. - - Correct format: {"text": "something", "img" "an image url"} """ if color == -1: color = Color.random() elif color == None: color = Color.blurple()