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 PreTrigger
s 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
PreTrigger
stage, the line has not been matched against any rules yet. This is what Sopel uses to perform matching.own_nick
is needed to correctly parse who sent the line (Sopel or someone else).line
can 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 (
None
otherwise)
- event#
The IRC command name or numeric value.
See
sopel.tools.events
for 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
sender
Will beNone
for commands that have no implicit channel or private-message context, for example AWAY or QUIT.The
COMMANDS_WITH_CONTEXT
attribute lists IRC commands for whichsender
can be relied upon.
- text#
The last argument of the IRC command.
If the line contains
:
,text
will be the text following it.For lines that do not contain
:
,text
will be the last argument inargs
instead.
- urls: tuple#
List of URLs found in the
text
.This is for
PRIVMSG
andNOTICE
messages 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
datetime
object.
- 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-tag
capability enabled)
A
Trigger
object itself can be used as a string; when used in this way, it represents the matching line’s full text.The
match
is based on the matching regular expression rule; Sopel’s command decorators auto-generate rules containing specific numbered groups that are documented separately. (Seegroup
below.)Note that CTCP messages (
PRIVMSG
es andNOTICE
es which start and end with'\x01'
) will have the'\x01'
bytes stripped, andtrigger.ctcp
will contain the command (e.g.ACTION
).Note
CTCP used to be stored as the
intent
tag. 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-tag
capability or both theaccount-notify
andextended-join
capabilities 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
nick
is one of the bot’s admins.- Type:
True
if the triggeringnick
is a Sopel admin;False
if not.Note that Sopel’s
owner
is 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 -m
on 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 variousDCC
operations.New in version 7.1.
Important
Use this attribute instead of the
intent
tag 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
callables
primarily need to deal withPRIVMSG
. Other event types likeNOTICE
,NICK
,TOPIC
,KICK
, etc. must be requested usingplugin.event()
.
- property group#
The
group()
function of thematch
attribute.Any regular-expression
rules
attached 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 thematch
attribute.See Python’s
re.Match.groupdict()
documentation for details.
- property groups#
The
groups()
function of thematch
attribute.See Python’s
re.Match.groups()
documentation for details.
- property is_privmsg#
Whether the message was triggered in a private message.
- Type:
True
if the trigger was received in a private message;False
if 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
nick
which triggered the command is the bot’s owner.- Type:
True
if the triggeringnick
is Sopel’s owner;False
if 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
\x01
bytes 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
sender
does not include the status prefix. Be sure to use thestatus_prefix
when replying.Note that the
bot
argument passed to plugin callables is aSopelWrapper
that handles this for the defaultdestination
of the methods it overrides (most importantly,say()
&reply()
).Warning
The
sender
Will beNone
for commands that have no implicit channel or private-message context, for example AWAY or QUIT.The
COMMANDS_WITH_CONTEXT
attribute lists IRC commands for whichsender
can be relied upon.
- property status_prefix#
The prefix used for the
sender
for status-specific messages.- Type:
Optional[str]
This will be
None
by 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
datetime
object (no timezone)
If the IRC server supports
server-time
,time
will give that value. Otherwise,time
will 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
PRIVMSG
or 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
sender
property. However, not all commands can be directly associated to a channel or nick.For IRC message types other than those listed here, the
trigger
’ssender
property will beNone
.