Only accept positive integers in [p]cleanup commands (#4115)

This commit is contained in:
jack1142
2020-07-29 02:44:36 +02:00
committed by GitHub
parent d73ad3115f
commit 8ada1ee152
2 changed files with 36 additions and 14 deletions

View File

@@ -1,12 +1,30 @@
from redbot.core.commands import Converter, BadArgument
from typing import NewType, TYPE_CHECKING
from redbot.core.commands import BadArgument, Context, Converter
from redbot.core.i18n import Translator
from redbot.core.utils.chat_formatting import inline
_ = Translator("Cleanup", __file__)
class RawMessageIds(Converter):
async def convert(self, ctx, argument):
async def convert(self, ctx: Context, argument: str) -> int:
if argument.isnumeric() and len(argument) >= 17:
return int(argument)
raise BadArgument(_("{} doesn't look like a valid message ID.").format(argument))
PositiveInt = NewType("PositiveInt", int)
if TYPE_CHECKING:
positive_int = PositiveInt
else:
def positive_int(arg: str) -> int:
try:
ret = int(arg)
except ValueError:
raise BadArgument(_("{arg} is not an integer.").format(arg=inline(arg)))
if ret <= 0:
raise BadArgument(_("{arg} is not a positive integer.").format(arg=inline(arg)))
return ret