[Core] Limit server prefix length (#5117)

* Restrict prefix length

* Update docs
This commit is contained in:
El Laggron 2021-06-12 17:41:29 +02:00 committed by GitHub
parent a428e42f1f
commit dafffd969f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -3174,6 +3174,8 @@ Sets Red's server prefix(es).
This is not additive. It will replace all current server prefixes. This is not additive. It will replace all current server prefixes.
You cannot have a prefix with more than 20 characters.
**Examples:** **Examples:**
- ``[p]set serverprefix !`` - ``[p]set serverprefix !``
- ``[p]set serverprefix "! "`` - Quotes are needed to use spaces in prefixes. - ``[p]set serverprefix "! "`` - Quotes are needed to use spaces in prefixes.

View File

@ -97,6 +97,8 @@ _ = i18n.Translator("Core", __file__)
TokenConverter = commands.get_dict_converter(delims=[" ", ",", ";"]) TokenConverter = commands.get_dict_converter(delims=[" ", ",", ";"])
MAX_PREFIX_LENGTH = 20
class CoreLogic: class CoreLogic:
def __init__(self, bot: "Red"): def __init__(self, bot: "Red"):
@ -2695,6 +2697,23 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
**Arguments:** **Arguments:**
- `<prefixes...>` - The prefixes the bot will respond to globally. - `<prefixes...>` - The prefixes the bot will respond to globally.
""" """
if any(len(x) > MAX_PREFIX_LENGTH for x in prefixes):
await ctx.send(
_(
"Warning: A prefix is above the recommended length (20 characters).\n"
"Do you want to continue? (y/n)"
)
)
pred = MessagePredicate.yes_or_no(ctx)
try:
await self.bot.wait_for("message", check=pred, timeout=30)
except asyncio.TimeoutError:
await ctx.send(_("Response timed out."))
return
else:
if pred.result is False:
await ctx.send(_("Cancelled."))
return
await ctx.bot.set_prefixes(guild=None, prefixes=prefixes) await ctx.bot.set_prefixes(guild=None, prefixes=prefixes)
if len(prefixes) == 1: if len(prefixes) == 1:
await ctx.send(_("Prefix set.")) await ctx.send(_("Prefix set."))
@ -2710,6 +2729,7 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
Warning: This will override global prefixes, the bot will not respond to any global prefixes in this server. Warning: This will override global prefixes, the bot will not respond to any global prefixes in this server.
This is not additive. It will replace all current server prefixes. This is not additive. It will replace all current server prefixes.
A prefix cannot have more than 20 characters.
**Examples:** **Examples:**
- `[p]set serverprefix !` - `[p]set serverprefix !`
@ -2724,6 +2744,9 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
await ctx.bot.set_prefixes(guild=ctx.guild, prefixes=[]) await ctx.bot.set_prefixes(guild=ctx.guild, prefixes=[])
await ctx.send(_("Server prefixes have been reset.")) await ctx.send(_("Server prefixes have been reset."))
return return
if any(len(x) > MAX_PREFIX_LENGTH for x in prefixes):
await ctx.send(_("You cannot have a prefix longer than 20 characters."))
return
prefixes = sorted(prefixes, reverse=True) prefixes = sorted(prefixes, reverse=True)
await ctx.bot.set_prefixes(guild=ctx.guild, prefixes=prefixes) await ctx.bot.set_prefixes(guild=ctx.guild, prefixes=prefixes)
if len(prefixes) == 1: if len(prefixes) == 1: