Add get_message_types() to plugin API
This allows plugins to specify the types of messages they handle. This will be used specifically for the nickserv plugin, but could be useful for other things too. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -61,7 +61,7 @@ class Bot:
|
||||
self.connection.register("JOIN", self.on_join)
|
||||
self.connection.register("PART", self.on_part)
|
||||
self.connection.register("KICK", self.on_kick)
|
||||
self.connection.register("PRIVMSG", self.on_message)
|
||||
self.connection.register("*", self.on_message)
|
||||
# Connect
|
||||
log.info("Connecting to %s", self.server_config.server)
|
||||
await self.connection.connect()
|
||||
@@ -135,7 +135,13 @@ class Bot:
|
||||
# Don't raise on_message events for ourselves.
|
||||
return
|
||||
line = message.parameters[1]
|
||||
plugins = self.channel_plugins(channel)
|
||||
# Filter plugins by get_message_types() and channel
|
||||
plugins = [
|
||||
plugin
|
||||
for plugin in self.channel_plugins(channel)
|
||||
if message.command in plugin.get_message_types()
|
||||
]
|
||||
if plugins:
|
||||
await asyncio.gather(
|
||||
*[plugin.on_message(conn, channel, who, line) for plugin in plugins]
|
||||
)
|
||||
|
||||
@@ -19,6 +19,16 @@ class Plugin:
|
||||
self.__plugin_config = plugin_config
|
||||
self.__bot = bot
|
||||
|
||||
def get_message_types(self) -> Sequence[str]:
|
||||
"""
|
||||
Gets the message types that this plugin listens for.
|
||||
|
||||
This is usually going to be just PRIVMSG by itself, i.e. `["PRIVMSG"]`.
|
||||
However, if you want to handle different messages (such as PART, JOIN,
|
||||
KICK, etc) then you can change that here.
|
||||
"""
|
||||
return ["PRIVMSG"]
|
||||
|
||||
@property
|
||||
def channels(self) -> Sequence[str]:
|
||||
if "channels" in self.plugin_config:
|
||||
|
||||
Reference in New Issue
Block a user