From 971ccf9df45d1c6e542f51fd1ab48f5f5f0ab71d Mon Sep 17 00:00:00 2001 From: palmtree5 <3577255+palmtree5@users.noreply.github.com> Date: Sun, 27 May 2018 20:28:22 -0800 Subject: [PATCH] [V3 Core] add support for setting a color for embeds (#1707) * [V3 Core] add support for setting a color for embeds * Add a guild toggle for whether to use the bot color * Add a function for getting embed color in Context * Coroutines need to be awaited lol --- Pipfile.lock | 34 ++++++++++++++++++++++++------- redbot/core/bot.py | 10 ++++++++- redbot/core/commands/context.py | 14 +++++++++++++ redbot/core/core_commands.py | 36 +++++++++++++++++++++++++++++++++ redbot/core/events.py | 1 + redbot/core/help_formatter.py | 36 ++++++++++++++++++--------------- 6 files changed, 107 insertions(+), 24 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index 4a710cc19..e5a038940 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -83,6 +83,13 @@ ], "version": "==1.0.2" }, + "fuzzywuzzy": { + "hashes": [ + "sha256:d40c22d2744dff84885b30bbfc07fab7875f641d070374331777a4d1808b8d4e", + "sha256:ecf490216fb4d76b558a03042ff8f45a8782f17326caca1384d834cbaa2c7e6f" + ], + "version": "==0.16.0" + }, "idna": { "hashes": [ "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f", @@ -121,6 +128,12 @@ ], "version": "==4.3.1" }, + "python-levenshtein": { + "hashes": [ + "sha256:033a11de5e3d19ea25c9302d11224e1a1898fe5abd23c61c7c360c25195e3eb1" + ], + "version": "==0.12.0" + }, "pyyaml": { "hashes": [ "sha256:0c507b7f74b3d2dd4d1322ec8a94794927305ab4cebbe89cc47fe5e81541e6e8", @@ -219,6 +232,13 @@ ], "version": "==1.4.3" }, + "atomicwrites": { + "hashes": [ + "sha256:240831ea22da9ab882b551b31d4225591e5e447a68c5e188db5b89ca1d487585", + "sha256:a24da68318b08ac9c9c45029f4a10371ab5b20e4226738e150e6e7c571630ae6" + ], + "version": "==1.1.5" + }, "attrs": { "hashes": [ "sha256:4b90b09eeeb9b88c35bc642cbac057e45a5fd85367b985bd2809c62b7b939265", @@ -300,11 +320,11 @@ }, "more-itertools": { "hashes": [ - "sha256:0dd8f72eeab0d2c3bd489025bb2f6a1b8342f9b198f6fc37b52d15cfa4531fea", - "sha256:11a625025954c20145b37ff6309cd54e39ca94f72f6bb9576d1195db6fa2442e", - "sha256:c9ce7eccdcb901a2c75d326ea134e0886abfbea5f93e91cc95de9507c0816c44" + "sha256:2b6b9893337bfd9166bee6a62c2b0c9fe7735dcf85948b387ec8cba30e85d8e8", + "sha256:6703844a52d3588f951883005efcf555e49566a48afd4db4e965d69b883980d3", + "sha256:a18d870ef2ffca2b8463c0070ad17b5978056f403fb64e3f15fe62a52db21cc0" ], - "version": "==4.1.0" + "version": "==4.2.0" }, "packaging": { "hashes": [ @@ -349,11 +369,11 @@ }, "pytest": { "hashes": [ - "sha256:54713b26c97538db6ff0703a12b19aeaeb60b5e599de542e7fca0ec83b9038e8", - "sha256:829230122facf05a5f81a6d4dfe6454a04978ea3746853b2b84567ecf8e5c526" + "sha256:39555d023af3200d004d09e51b4dd9fdd828baa863cded3fd6ba2f29f757ae2d", + "sha256:c76e93f3145a44812955e8d46cdd302d8a45fbfc7bf22be24fe231f9d8d8853a" ], "index": "pypi", - "version": "==3.5.1" + "version": "==3.6.0" }, "pytest-asyncio": { "hashes": [ diff --git a/redbot/core/bot.py b/redbot/core/bot.py index c5acda519..85998fe01 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -49,13 +49,20 @@ class RedBase(BotBase): enable_sentry=None, locale="en", embeds=True, + color=15158332, help__page_char_limit=1000, help__max_pages_in_guild=2, help__tagline="", ) self.db.register_guild( - prefix=[], whitelist=[], blacklist=[], admin_role=None, mod_role=None, embeds=None + prefix=[], + whitelist=[], + blacklist=[], + admin_role=None, + mod_role=None, + embeds=None, + use_bot_color=False, ) self.db.register_user(embeds=None) @@ -92,6 +99,7 @@ class RedBase(BotBase): self.counter = Counter() self.uptime = None + self.color = None self.main_dir = bot_dir diff --git a/redbot/core/commands/context.py b/redbot/core/commands/context.py index 533b43118..b42840d23 100644 --- a/redbot/core/commands/context.py +++ b/redbot/core/commands/context.py @@ -121,6 +121,20 @@ class Context(commands.Context): await query.delete() return ret + async def embed_colour(self): + """ + Helper function to get the colour for an embed. + + Returns + ------- + discord.Colour: + The colour to be used + """ + if self.guild and await self.bot.db.guild(self.guild).use_bot_color(): + return self.guild.me.color + else: + return self.bot.color + async def embed_requested(self): """ Simple helper to call bot.embed_requested diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index 5f5747151..14d47eb96 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -556,6 +556,42 @@ class Core: await ctx.bot.db.guild(ctx.guild).mod_role.set(role.id) await ctx.send(_("The mod role for this guild has been set.")) + @_set.command(aliases=["usebotcolor"]) + @checks.guildowner() + @commands.guild_only() + async def usebotcolour(self, ctx): + """ + Toggle whether to use the bot owner-configured colour for embeds. + + Default is to not use the bot's configured colour, in which case the + colour used will be the colour of the bot's top role. + """ + current_setting = await ctx.bot.db.guild(ctx.guild).use_bot_color() + await ctx.bot.db.guild(ctx.guild).use_bot_color.set(not current_setting) + await ctx.send( + _("The bot {} use its configured color for embeds.").format( + _("will not") if current_setting else _("will") + ) + ) + + @_set.command(aliases=["color"]) + @checks.is_owner() + async def colour(self, ctx, *, colour: discord.Colour = None): + """ + Sets a default colour to be used for the bot's embeds. + + Acceptable values cor the colour parameter can be found at: + + http://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.ColourConverter + """ + if colour is None: + ctx.bot.color = discord.Color.red() + await ctx.bot.db.color.set(discord.Color.red().value) + return await ctx.send(_("The color has been reset.")) + ctx.bot.color = colour + await ctx.bot.db.color.set(colour.value) + await ctx.send(_("The color has been set.")) + @_set.command() @checks.is_owner() async def avatar(self, ctx, url: str): diff --git a/redbot/core/events.py b/redbot/core/events.py index 86cf939ac..9ebd59d8b 100644 --- a/redbot/core/events.py +++ b/redbot/core/events.py @@ -172,6 +172,7 @@ def init_events(bot, cli_flags): if invite_url: print("\nInvite URL: {}\n".format(invite_url)) + bot.color = discord.Colour(await bot.db.color()) if bot.rpc_enabled: await bot.rpc.initialize() diff --git a/redbot/core/help_formatter.py b/redbot/core/help_formatter.py index be969f63e..93d566a3f 100644 --- a/redbot/core/help_formatter.py +++ b/redbot/core/help_formatter.py @@ -72,12 +72,14 @@ class Help(formatter.HelpFormatter): def avatar(self): return self.context.bot.user.avatar_url_as(format="png") - @property - def color(self): + async def color(self): if self.pm_check(self.context): - return 0 + return self.context.bot.color else: - return self.me.color + if await self.context.bot.db.guild(self.context.guild).use_bot_color(): + return self.context.bot.color + else: + return self.me.color @property def destination(self): @@ -250,7 +252,7 @@ class Help(formatter.HelpFormatter): field_groups = self.group_fields(emb["fields"], page_char_limit) for i, group in enumerate(field_groups, 1): - embed = discord.Embed(color=self.color, **emb["embed"]) + embed = discord.Embed(color=await self.color(), **emb["embed"]) if len(field_groups) > 1: description = "{} *- Page {} of {}*".format( @@ -269,26 +271,26 @@ class Help(formatter.HelpFormatter): return ret - def simple_embed(self, ctx, title=None, description=None, color=None): + async def simple_embed(self, ctx, title=None, description=None, color=None): # Shortcut self.context = ctx if color is None: - color = self.color + color = await self.color() embed = discord.Embed(title=title, description=description, color=color) embed.set_footer(text=ctx.bot.formatter.get_ending_note()) embed.set_author(**self.author) return embed - def cmd_not_found(self, ctx, cmd, color=None): + async def cmd_not_found(self, ctx, cmd, color=None): # Shortcut for a shortcut. Sue me out = fuzzy_command_search(ctx, " ".join(ctx.args[1:])) - embed = self.simple_embed( + embed = await self.simple_embed( ctx, title="Command {} not found.".format(cmd), description=out, color=color ) return embed - def cmd_has_no_subcommands(self, ctx, cmd, color=None): - embed = self.simple_embed( + async def cmd_has_no_subcommands(self, ctx, cmd, color=None): + embed = await self.simple_embed( ctx, title=ctx.bot.command_has_no_subcommands.format(cmd), color=color ) return embed @@ -324,7 +326,7 @@ async def help(ctx, *cmds: str): command = ctx.bot.all_commands.get(name) if command is None: if use_embeds: - await destination.send(embed=ctx.bot.formatter.cmd_not_found(ctx, name)) + await destination.send(embed=await ctx.bot.formatter.cmd_not_found(ctx, name)) else: await destination.send( ctx.bot.command_not_found.format(name, fuzzy_command_search(ctx, name)) @@ -339,7 +341,7 @@ async def help(ctx, *cmds: str): command = ctx.bot.all_commands.get(name) if command is None: if use_embeds: - await destination.send(embed=ctx.bot.formatter.cmd_not_found(ctx, name)) + await destination.send(embed=await ctx.bot.formatter.cmd_not_found(ctx, name)) else: await destination.send( ctx.bot.command_not_found.format(name, fuzzy_command_search(ctx, name)) @@ -352,7 +354,9 @@ async def help(ctx, *cmds: str): command = command.all_commands.get(key) if command is None: if use_embeds: - await destination.send(embed=ctx.bot.formatter.cmd_not_found(ctx, key)) + await destination.send( + embed=await ctx.bot.formatter.cmd_not_found(ctx, key) + ) else: await destination.send( ctx.bot.command_not_found.format(key, fuzzy_command_search(ctx, name)) @@ -361,10 +365,10 @@ async def help(ctx, *cmds: str): except AttributeError: if use_embeds: await destination.send( - embed=ctx.bot.formatter.simple_embed( + embed=await ctx.bot.formatter.simple_embed( ctx, title='Command "{0.name}" has no subcommands.'.format(command), - color=ctx.bot.formatter.color, + color=await ctx.bot.formatter.color(), ) ) else: