Skip to content

Commit

Permalink
feat: Case insensitive prefixes (#351)
Browse files Browse the repository at this point in the history
* case insensitive prefixes

* oops

* map prefixes to str.lower

Co-authored-by: nulldomain <[email protected]>

* 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

---------

Co-authored-by: nulldomain <[email protected]>
  • Loading branch information
luanalatte and null-domain authored Nov 27, 2023
1 parent 2039ee0 commit 30dbd4f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lightbulb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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",
)
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -964,12 +969,18 @@ async def get_prefix_context(
prefixes = t.cast(t.Sequence[str], prefixes)

if isinstance(prefixes, str):
prefixes = [prefixes]
prefixes = (prefixes,)

message = event.message.content
if self._case_insensitive_prefixes:
message = message.lower()
prefixes = tuple(map(str.lower, prefixes))

prefixes = sorted(prefixes, key=len, reverse=True)

invoked_prefix = None
for prefix in prefixes:
if event.message.content.startswith(prefix):
if message.startswith(prefix):
invoked_prefix = prefix
break

Expand Down

0 comments on commit 30dbd4f

Please sign in to comment.