[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
This commit is contained in:
palmtree5
2018-05-27 20:28:22 -08:00
committed by Kowlin
parent 07eb6bf88e
commit 971ccf9df4
6 changed files with 107 additions and 24 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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):

View File

@@ -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()

View File

@@ -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: