hook.command¶
- exception immp.hook.command.BadUsage¶
Bases:
HookError
May be raised from within a command to indicate that the arguments were invalid.
- class immp.hook.command.CommandParser(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
Enum
Constants representing the method used to parse the argument text following a used command.
- spaces¶
Split using
str.split()
, for simple inputs breaking on whitespace characters.
- shlex¶
Split using
shlex.split()
, which allows quoting of multi-word arguments.
- none¶
Don’t split the argument, just provide the rich message text as-is.
- hybrid¶
Split as with
spaces
up to the number of accepted arguments, and return rich text in the last argument. If optional arguments are present, only the last will receive the rich text, or none of them if not filled in.
- class immp.hook.command.CommandScope(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
Enum
Constants representing the types of conversations a command is available in.
- anywhere¶
All configured channels.
- private¶
Only private channels, as configured per-plug.
Only non-private channels, as configured per-channel.
- class immp.hook.command.BoundCommand(hook, cmd)¶
Bases:
object
Wrapper object returned when accessing a command via a
Hook
instance, similar totypes.MethodType
.This object is callable, which invokes the command’s underlying method against the bound hook.
- applicable(channel, user, private)¶
Test the availability of the current command based on the scope.
- Parameters:
channel (.Channel) – Source channel where the command will be executed.
user (.User) – Author of the message to trigger the command.
private (bool) – Result of
Channel.is_private()
.
- Returns:
True
if the command may be used.- Return type:
- complete(name, *args)¶
Fully-qualify an underlying base command.
- class immp.hook.command.BaseCommand(fn, parser=CommandParser.spaces, scope=CommandScope.anywhere, test=None, sync_aware=False)¶
Bases:
object
Container of a command function. Use the
command()
decorator to wrap a class method and convert it into an instance of this class. Unless dynamic, command objects will be an instance of theFullCommand
subclass.Accessing an instance of this class via the attribute of a containing class will create a new
BoundCommand
allowing invocation of the method.- fn¶
Callback method from a hook to process the command usage.
- Type:
method
- parser¶
Parse mode for the command arguments.
- Type:
.CommandParser
- scope¶
Accessibility of this command for the different channel types.
- Type:
.CommandScope
- test¶
Additional predicate that can enable or disable a command based on hook state.
- Type:
method
- parse(args)¶
Convert a string of multiple arguments into a list according to the chosen parse mode.
- valid(*args)¶
Test the validity of the given arguments against the command’s underlying method. Raises
ValueError
if the arguments don’t match the signature.- Parameters:
args (str list) – Parsed arguments.
- complete(name, *args)¶
Fully-qualify a base command.
- class immp.hook.command.FullCommand(name, fn, parser=CommandParser.spaces, scope=CommandScope.anywhere, test=None, sync_aware=False, fixed_args=())¶
Bases:
BaseCommand
Fully-qualified and named command, an extension of
BaseCommand
to make it callable.- complete(name, *args)¶
Fully-qualify a base command.
- immp.hook.command.command(name=None, parser=CommandParser.spaces, scope=CommandScope.anywhere, test=None, sync_aware=False)¶
Decorator: mark up a hook method as a command.
This doesn’t return the original function, rather a
BaseCommand
object.The method’s accepted arguments must start with
self
, and must accept one calledmsg
, which will be set to theMessage
instance that triggered this command.A dynamic command is an unnamed instance that can be used to create multiple commands with the same underlying method call. These commands are not collected automatically – the parent hook must implement
DynamicCommands
.- Parameters:
name (str) – Command name, used to access the command when directly following the prefix.
parser (.CommandParser) – Parse mode for the command arguments.
scope (.CommandScope) – Accessibility of this command for the different channel types.
test (method) – Additional predicate that can enable or disable a command based on hook state.
sync_aware (bool) –
True
if the hook is aware of synced channels provided by aSyncPlug
. In this case, the command handler will receive the native channel rather than the virtual sync channel. SeeSyncPlug.sync_for()
for resolving this to a virtual channel.
- class immp.hook.command.DynamicCommands¶
Bases:
object
Interface for commands generated at runtime. Typically combined with additional arguments in a command method’s signature.
- commands()¶
Provide additional commands dependent on state or config. Dynamic commands must be filled in using
BaseCommand.complete()
to provide their name and any fixed arguments.- Returns:
Set of available commands.
- Return type:
.BoundCommand set
- class immp.hook.command.CommandHook(name, config, host)¶
Bases:
Hook
Generic command handler for other hooks.
Hooks should wrap command handler methods with the
command()
decorator, which will be picked up by any instances of this class configured to include that hook.- async commands(channel, user)¶
Retrieve all commands, and filter against the mappings.
- async on_receive(sent, source, primary)¶
Handle an incoming message received by any plug.
- Parameters:
sent (.SentMessage) – Raw message received from another plug.
source (.Message) – Original message data used to generate the raw message, if sent via the plug (e.g. from another hook), equivalent to
msg
if the source is otherwise unknown.primary (bool) –
False
for supplementary messages if the source message required multiple raw messages in order to represent it (e.g. messages with multiple attachments where the underlying network doesn’t support it), otherwiseTrue
.