Triggers#
Triggers are how Sopel tells callables about their runtime context.
A Trigger is the main type of user input plugins will see.
Sopel uses PreTriggers internally while processing
incoming IRC messages. Plugin authors can reasonably expect that their code
will never receive one. They are documented here for completeness, and for the
aid of Sopel’s core development.
- class sopel.trigger.PreTrigger(
- own_nick: Identifier,
- line: str,
- url_schemes: Sequence | None = None,
- identifier_factory: IdentifierFactory = Identifier,
- statusmsg_prefixes: tuple[str, ...] = tuple(),
A parsed raw message from the server.
- Parameters:
At the
PreTriggerstage, the line has not been matched against any rules yet. This is what Sopel uses to perform matching.own_nickis needed to correctly parse who sent the line (Sopel or someone else).linecan also be a simulated echo-message, useful if the connected server does not support the capability.- args#
The IRC command’s arguments.
These are split on spaces, per the IRC protocol.
- ctcp#
The CTCP command name, if present (
Noneotherwise)
- event#
The IRC command name or numeric value.
See
sopel.tools.eventsfor a built-in mapping of names to numeric values.
- host#
The sender’s hostname.
- hostmask#
The sender’s hostmask, as sent by the IRC server.
- line#
The raw line received from the server.
- nick#
The nickname that sent this command.
- sender#
Channel name or query where this message was received.
Warning
The
senderWill beNonefor commands that have no implicit channel or private-message context, for example AWAY or QUIT.The
COMMANDS_WITH_CONTEXTattribute lists IRC commands for whichsendercan be relied upon.
- text#
The last argument of the IRC command.
If the line contains
:,textwill be the text following it.For lines that do not contain
:,textwill be the last argument inargsinstead.
- urls: tuple#
List of URLs found in the
text.This is for
PRIVMSGandNOTICEmessages only. For other messages, this will be an emptytuple.
- plain#
The last argument of the IRC command with control codes stripped.
- time#
The time when the message was received.
If the IRC server sent a message tag indicating when it received the message, that is used instead of the time when Sopel received it.
Changed in version 8.0: Now a timezone-aware
datetimeobject.
- user#
The sender’s local username.
- class sopel.trigger.Trigger(
- settings: config.Config,
- message: PreTrigger,
- match: Match,
- account: str | None = None,
A line from the server, which has matched a callable’s rules.
- Parameters:
config (
Config) – Sopel’s current configuration settings objectmessage (
PreTrigger) – the message that matched the callablematch (Match object) – what in the message matched
account (str) – services account name of the
message’s sender (optional; only applies on networks with theaccount-tagcapability enabled)
A
Triggerobject itself can be used as a string; when used in this way, it represents the matching line’s full text.The
matchis based on the matching regular expression rule; Sopel’s command decorators auto-generate rules containing specific numbered groups that are documented separately. (Seegroupbelow.)Note that CTCP messages (
PRIVMSGes andNOTICEes which start and end with'\x01') will have the'\x01'bytes stripped, andtrigger.ctcpwill contain the command (e.g.ACTION).Note
CTCP used to be stored as the
intenttag. Since message intents never made it past the IRCv3 draft stage, Sopel dropped support for them in Sopel 8.- property account#
The services account name of the user sending the message.
- Type:
str or None
Note: This is only available if the
account-tagcapability or both theaccount-notifyandextended-joincapabilities are available on the connected IRC network. If this is not the case, or if the user sending the message isn’t logged in to services, this property will beNone.
- property admin#
Whether the triggering
nickis one of the bot’s admins.- Type:
Trueif the triggeringnickis a Sopel admin;Falseif not.Note that Sopel’s
owneris also considered to be an admin.
- property args#
A list containing each of the arguments to an event.
These are the strings passed between the event name and the colon. For example, when setting
mode -mon the channel#example, args would be['#example', '-m']
- property ctcp#
The CTCP command (if any).
- Type:
Common CTCP commands are
ACTION,VERSION, andTIME. Other commands includeUSERINFO,PING, and variousDCCoperations.New in version 7.1.
Important
Use this attribute instead of the
intenttag intags. Message intents never made it past the IRCv3 draft stage, and Sopel dropped support for them in Sopel 8.
- property event#
The IRC event which triggered the message.
- Type:
Most plugin
callablesprimarily need to deal withPRIVMSG. Other event types likeNOTICE,NICK,TOPIC,KICK, etc. must be requested usingplugin.event().
- property group#
The
group()function of thematchattribute.Any regular-expression
rulesattached to the triggeredcallable()may define numbered or named groups that can be retrieved through this property.Sopel’s command decorators each define a predetermined set of numbered groups containing fragments of the line that plugins commonly use.
See also
For more information on predefined numbered match groups in commands, see
plugin.command(),plugin.action_command(), andplugin.nickname_command().Also see Python’s
re.Match.group()documentation for details about this method’s behavior.
- property groupdict#
The
groupdict()function of thematchattribute.See Python’s
re.Match.groupdict()documentation for details.
- property groups#
The
groups()function of thematchattribute.See Python’s
re.Match.groups()documentation for details.
- property is_privmsg#
Whether the message was triggered in a private message.
- Type:
Trueif the trigger was received in a private message;Falseif it came from a channel.
- property match#
The Match object for the triggering line.
- Type:
- property nick#
The nickname who sent the message.
- Type:
- property owner#
Whether the
nickwhich triggered the command is the bot’s owner.- Type:
Trueif the triggeringnickis Sopel’s owner;Falseif not.
- property plain#
The text without formatting control codes.
- Type:
This is the text of the trigger object without formatting control codes.
- property raw#
The entire raw IRC message, as sent from the server.
- Type:
This includes the CTCP
\x01bytes and command, if they were included.
- property sender#
Where the message arrived from.
- Type:
This will be a channel name for “regular” (channel) messages, or the nick that sent a private message.
You can check if the trigger comes from a channel or a nick with its
is_nick()method:if trigger.sender.is_nick(): # message sent from a private message else: # message sent from a channel
Important
If the message was sent to a specific status prefix, the
senderdoes not include the status prefix. Be sure to use thestatus_prefixwhen replying.Note that the
botargument passed to plugin callables is aSopelWrapperthat handles this for the defaultdestinationof the methods it overrides (most importantly,say()&reply()).Warning
The
senderWill beNonefor commands that have no implicit channel or private-message context, for example AWAY or QUIT.The
COMMANDS_WITH_CONTEXTattribute lists IRC commands for whichsendercan be relied upon.
- property status_prefix#
The prefix used for the
senderfor status-specific messages.- Type:
Optional[str]
This will be
Noneby default. If a message is sent to a channel, it may have a status prefix for status-specific messages. In that case, the prefix is removed from the channel’s identifier (seesender) and saved to this attribute.
- property time#
When the message was received.
- Type:
naïve
datetimeobject (no timezone)
If the IRC server supports
server-time,timewill give that value. Otherwise,timewill use the time when the message was received by Sopel. In both cases, this time is in UTC.
- property urls#
A tuple containing all URLs found in the text.
- Type:
URLs are listed only for
PRIVMSGor aNOTICE, otherwise this is an empty tuple.
Command context#
- sopel.trigger.COMMANDS_WITH_CONTEXT = frozenset({'INVITE', 'JOIN', 'KICK', 'MODE', 'NOTICE', 'PART', 'PRIVMSG', 'TOPIC'})#
Set of commands with a
trigger.sender.Most IRC messages a plugin will want to handle (channel messages and PMs) are associated with a context, exposed in the Trigger’s
senderproperty. However, not all commands can be directly associated to a channel or nick.For IRC message types other than those listed here, the
trigger’ssenderproperty will beNone.