From c423d3c441c38e66164b1b22db21f852603d9d77 Mon Sep 17 00:00:00 2001 From: Luana Date: Wed, 11 Oct 2023 18:04:13 -0300 Subject: [PATCH 1/4] case insensitive prefixes --- lightbulb/app.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lightbulb/app.py b/lightbulb/app.py index bd4e5c2d..3c94d945 100644 --- a/lightbulb/app.py +++ b/lightbulb/app.py @@ -166,6 +166,8 @@ class BotApp(hikari.GatewayBot): as well as a prefix command. Defaults to ``False``. delete_unbound_commands (:obj:`bool`): Whether or not the bot should delete application commands that it cannot find an implementation for when the bot starts. Defaults to ``True``. + case_insensitive_prefixes (:obj:`bool`): Wheter or not command prefixes should be case-insensitive. + Defaults to ``False``. case_insensitive_prefix_commands (:obj:`bool`): Whether or not prefix command names should be case-insensitive. Defaults to ``False``. **kwargs (Any): Additional keyword arguments passed to the constructor of the :obj:`~hikari.impl.gateway_bot.GatewayBot` @@ -189,6 +191,7 @@ class BotApp(hikari.GatewayBot): "default_enabled_guilds", "_help_command", "_delete_unbound_commands", + "_case_insensitive_prefixes", "_case_insensitive_prefix_commands", "_running_tasks", ) @@ -203,6 +206,7 @@ def __init__( help_class: t.Optional[t.Type[help_command_.BaseHelpCommand]] = help_command_.DefaultHelpCommand, help_slash_command: bool = False, delete_unbound_commands: bool = True, + case_insensitive_prefixes: bool = False, case_insensitive_prefix_commands: bool = False, **kwargs: t.Any, ) -> None: @@ -220,6 +224,7 @@ def __init__( ] = prefix self._delete_unbound_commands = delete_unbound_commands + self._case_insensitive_prefixes = case_insensitive_prefixes self._case_insensitive_prefix_commands = case_insensitive_prefix_commands self.ignore_bots: bool = ignore_bots @@ -965,9 +970,14 @@ async def get_prefix_context( prefixes = [prefixes] prefixes = sorted(prefixes, key=len, reverse=True) + message = event.message.content + if self.case_insensitive_prefixes: + message = message.lower() + prefixes = [x.lower() for x in prefixes] + invoked_prefix = None for prefix in prefixes: - if event.message.content.startswith(prefix): + if message.startswith(prefix): invoked_prefix = prefix break From 26f5634dda17579fb825ce0a951a6cb6344de72f Mon Sep 17 00:00:00 2001 From: Luana Date: Wed, 11 Oct 2023 18:05:23 -0300 Subject: [PATCH 2/4] oops --- lightbulb/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightbulb/app.py b/lightbulb/app.py index 3c94d945..d4e4a14d 100644 --- a/lightbulb/app.py +++ b/lightbulb/app.py @@ -971,7 +971,7 @@ async def get_prefix_context( prefixes = sorted(prefixes, key=len, reverse=True) message = event.message.content - if self.case_insensitive_prefixes: + if self._case_insensitive_prefixes: message = message.lower() prefixes = [x.lower() for x in prefixes] From c9ee43bfb724cb1bcdc73422dcc3bde97e59f199 Mon Sep 17 00:00:00 2001 From: Luana Date: Mon, 16 Oct 2023 12:27:15 -0300 Subject: [PATCH 3/4] map prefixes to str.lower Co-authored-by: nulldomain --- lightbulb/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightbulb/app.py b/lightbulb/app.py index d4e4a14d..5ab25152 100644 --- a/lightbulb/app.py +++ b/lightbulb/app.py @@ -973,7 +973,7 @@ async def get_prefix_context( message = event.message.content if self._case_insensitive_prefixes: message = message.lower() - prefixes = [x.lower() for x in prefixes] + prefixes = map(str.lower, prefixes) invoked_prefix = None for prefix in prefixes: From 9b6e464174860441976115b2c12fd3ebf7895f8d Mon Sep 17 00:00:00 2001 From: nulldomain Date: Mon, 27 Nov 2023 14:56:40 +0000 Subject: [PATCH 4/4] unpack the mapped lowercase prefixes to a tuple technically it should work without tuple() but mypy keeps complaining and i'm honestly quite fed up of it --- lightbulb/app.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lightbulb/app.py b/lightbulb/app.py index 5ab25152..bc840f14 100644 --- a/lightbulb/app.py +++ b/lightbulb/app.py @@ -967,13 +967,14 @@ async def get_prefix_context( prefixes = t.cast(t.Sequence[str], prefixes) if isinstance(prefixes, str): - prefixes = [prefixes] - prefixes = sorted(prefixes, key=len, reverse=True) + prefixes = (prefixes,) message = event.message.content if self._case_insensitive_prefixes: message = message.lower() - prefixes = map(str.lower, prefixes) + prefixes = tuple(map(str.lower, prefixes)) + + prefixes = sorted(prefixes, key=len, reverse=True) invoked_prefix = None for prefix in prefixes: