Merge branch 'V3/release/3.0.0' into V3/develop

# Conflicts:
#	redbot/cogs/customcom/customcom.py
#	redbot/core/errors.py
This commit is contained in:
Toby Harradine
2018-10-16 09:42:38 +11:00
11 changed files with 317 additions and 195 deletions

View File

@@ -3,13 +3,14 @@ import random
from datetime import datetime, timedelta
from inspect import Parameter
from collections import OrderedDict
from typing import Mapping, Tuple, Dict
from typing import Mapping, Tuple, Dict, Set
import discord
from redbot.core import Config, checks, commands
from redbot.core.utils.chat_formatting import box, pagify, escape
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils import menus
from redbot.core.utils.chat_formatting import box, pagify, escape
from redbot.core.utils.predicates import MessagePredicate
_ = Translator("CustomCommands", __file__)
@@ -121,7 +122,7 @@ class CommandObj:
*,
response=None,
cooldowns: Mapping[str, int] = None,
ask_for: bool = True
ask_for: bool = True,
):
"""Edit an already existing custom command"""
ccinfo = await self.db(ctx.guild).commands.get_raw(command, default=None)
@@ -330,12 +331,16 @@ class CustomCommands(commands.Cog):
await ctx.send(e.args[0])
@customcom.command(name="list")
async def cc_list(self, ctx):
"""List all available custom commands."""
@checks.bot_has_permissions(add_reactions=True)
async def cc_list(self, ctx: commands.Context):
"""List all available custom commands.
response = await CommandObj.get_commands(self.config.guild(ctx.guild))
The list displays a preview of each command's response, with
markdown escaped and newlines replaced with spaces.
"""
cc_dict = await CommandObj.get_commands(self.config.guild(ctx.guild))
if not response:
if not cc_dict:
await ctx.send(
_(
"There are no custom commands in this server."
@@ -345,8 +350,7 @@ class CustomCommands(commands.Cog):
return
results = []
for command, body in response.items():
for command, body in sorted(cc_dict.items(), key=lambda t: t[0]):
responses = body["response"]
if isinstance(responses, list):
result = ", ".join(responses)
@@ -354,15 +358,33 @@ class CustomCommands(commands.Cog):
result = responses
else:
continue
results.append("{command:<15} : {result}".format(command=command, result=result))
# Replace newlines with spaces
# Cut preview to 52 characters max
if len(result) > 52:
result = result[:49] + "..."
# Replace newlines with spaces
result = result.replace("\n", " ")
# Escape markdown and mass mentions
result = escape(result, formatting=True, mass_mentions=True)
results.append((f"{ctx.clean_prefix}{command}", result))
_commands = "\n".join(results)
if len(_commands) < 1500:
await ctx.send(box(_commands))
if await ctx.embed_requested():
content = "\n".join(map("**{0[0]}** {0[1]}".format, results))
pages = list(pagify(content, page_length=1024))
embed_pages = []
for idx, page in enumerate(pages, start=1):
embed = discord.Embed(
title=_("Custom Command List"),
description=page,
colour=await ctx.embed_colour(),
)
embed.set_footer(text=_("Page {num}/{total}").format(num=idx, total=len(pages)))
embed_pages.append(embed)
await menus.menu(ctx, embed_pages, menus.DEFAULT_CONTROLS)
else:
for page in pagify(_commands, delims=[" ", "\n"]):
await ctx.author.send(box(page))
content = "\n".join(map("{0[0]:<12} : {0[1]}".format, results))
pages = list(map(box, pagify(content, page_length=2000, shorten_by=10)))
await menus.menu(ctx, pages, menus.DEFAULT_CONTROLS)
@customcom.command(name="show")
async def cc_show(self, ctx, command_name: str):
@@ -606,3 +628,14 @@ class CustomCommands(commands.Cog):
else:
return raw_result
return str(getattr(first, second, raw_result))
async def get_command_names(self, guild: discord.Guild) -> Set[str]:
"""Get all custom command names in a guild.
Returns
--------
Set[str]
A set of all custom command names.
"""
return set(await CommandObj.get_commands(self.config.guild(guild)))