Triggers¶
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.Trigger(config, message, match, account=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, and the command (e.g.ACTION) placed mapped to the'intent'key inTrigger.tags.-
property
account¶ The services account name of the user sending the message.
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 will drop support for them in a future release.
-
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
A map of the IRCv3 message tags on the message.
- Type
-
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.
-
class
sopel.trigger.PreTrigger(own_nick, line, url_schemes=None)¶ 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.
-
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.
Any IRCv3 message tags attached to the line, as a
dict.
-
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.
-
user¶ The sender’s local username.