|
|
@ -46,6 +46,9 @@ class BotConfig(Config): |
|
|
|
commands_prefix : str |
|
|
|
A string prefix that is required for a message to be considered for |
|
|
|
command parsing. |
|
|
|
commands_prefixes : list[string] |
|
|
|
A list of string prefixes that are required for a message to be considered |
|
|
|
for command parsing. |
|
|
|
commands_allow_edit : bool |
|
|
|
If true, the bot will re-parse an edited message if it was the last sent |
|
|
|
message in a channel, and did not previously trigger a command. This is |
|
|
@ -88,6 +91,7 @@ class BotConfig(Config): |
|
|
|
'user': True, |
|
|
|
} |
|
|
|
commands_prefix = '' |
|
|
|
commands_prefixes = [] |
|
|
|
commands_allow_edit = True |
|
|
|
commands_level_getter = None |
|
|
|
commands_group_abbrev = True |
|
|
@ -275,21 +279,33 @@ class Bot(LoggingClass): |
|
|
|
else: |
|
|
|
self.command_matches_re = None |
|
|
|
|
|
|
|
def get_commands_for_message(self, require_mention, mention_rules, prefix, msg): |
|
|
|
def get_commands_for_message(self, require_mention, mention_rules, msg_prefix, msg, prefixes=[]): |
|
|
|
""" |
|
|
|
Generator of all commands that a given message object triggers, based on |
|
|
|
the bots plugins and configuration. |
|
|
|
|
|
|
|
Parameters |
|
|
|
--------- |
|
|
|
require_mention : bool |
|
|
|
Checks if the message starts with a mention (and then ignores the prefix(es)) |
|
|
|
mention_rules : dict(str, bool) |
|
|
|
Whether `user`, `everyone`, and `role` mentions are allowed. Defaults to: |
|
|
|
`{'user': True, 'everyone': False, 'role': False} |
|
|
|
msg_prefix : str |
|
|
|
The prefix to check the message starts with. |
|
|
|
msg : :class:`disco.types.message.Message` |
|
|
|
The message object to parse and find matching commands for. |
|
|
|
The newly created or updated message object to parse/handle. |
|
|
|
prefixes : list[string] |
|
|
|
A list of prefixes to check the message starts with (combines with `prefix`) |
|
|
|
|
|
|
|
Yields |
|
|
|
------- |
|
|
|
tuple(:class:`disco.bot.command.Command`, `re.MatchObject`) |
|
|
|
All commands the message triggers. |
|
|
|
""" |
|
|
|
# somebody better figure out what this yields... |
|
|
|
|
|
|
|
prefixes = (prefixes or []) + [msg_prefix] # don't break the tests and keep compatibility |
|
|
|
content = msg.content |
|
|
|
|
|
|
|
if require_mention: |
|
|
@ -326,11 +342,18 @@ class Bot(LoggingClass): |
|
|
|
content = content.replace('<@{}>'.format(role), '', 1) |
|
|
|
|
|
|
|
content = content.lstrip() |
|
|
|
|
|
|
|
if prefix and not content.startswith(prefix): |
|
|
|
return [] |
|
|
|
else: |
|
|
|
content = content[len(prefix):] |
|
|
|
# Scan through the prefixes to find the first one that matches. |
|
|
|
# This may lead to unexpected results, but said unexpectedness |
|
|
|
# should be easy to avoid. An example of the unexpected results |
|
|
|
# that may occur would be if one prefix was `!` and one was `!a`. |
|
|
|
if not any([content.startswith(prefix) for prefix in prefixes]): |
|
|
|
return [] |
|
|
|
else: |
|
|
|
for prefix in prefixes: |
|
|
|
if prefix and content.startswith(prefix): |
|
|
|
content = content[len(prefix):] |
|
|
|
break |
|
|
|
|
|
|
|
if not self.command_matches_re or not self.command_matches_re.match(content): |
|
|
|
return [] |
|
|
@ -340,6 +363,7 @@ class Bot(LoggingClass): |
|
|
|
match = command.compiled_regex.match(content) |
|
|
|
if match: |
|
|
|
options.append((command, match)) |
|
|
|
|
|
|
|
return sorted(options, key=lambda obj: obj[0].group is None) |
|
|
|
|
|
|
|
def get_level(self, actor): |
|
|
@ -388,6 +412,7 @@ class Bot(LoggingClass): |
|
|
|
self.config.commands_mention_rules, |
|
|
|
self.config.commands_prefix, |
|
|
|
msg, |
|
|
|
self.config.commands_prefixes, |
|
|
|
)) |
|
|
|
|
|
|
|
if not len(commands): |
|
|
|