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:
2022-05-30 18:14:48 -07:00
parent 0bb17050ec
commit 061cf9ee7b
2 changed files with 21 additions and 5 deletions

View File

@@ -61,7 +61,7 @@ class Bot:
self.connection.register("JOIN", self.on_join) self.connection.register("JOIN", self.on_join)
self.connection.register("PART", self.on_part) self.connection.register("PART", self.on_part)
self.connection.register("KICK", self.on_kick) self.connection.register("KICK", self.on_kick)
self.connection.register("PRIVMSG", self.on_message) self.connection.register("*", self.on_message)
# Connect # Connect
log.info("Connecting to %s", self.server_config.server) log.info("Connecting to %s", self.server_config.server)
await self.connection.connect() await self.connection.connect()
@@ -135,10 +135,16 @@ class Bot:
# Don't raise on_message events for ourselves. # Don't raise on_message events for ourselves.
return return
line = message.parameters[1] line = message.parameters[1]
plugins = self.channel_plugins(channel) # Filter plugins by get_message_types() and channel
await asyncio.gather( plugins = [
*[plugin.on_message(conn, channel, who, line) for plugin in 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]
)
async def keepalive(self): async def keepalive(self):
# loop while we're connected, check every second # loop while we're connected, check every second

View File

@@ -19,6 +19,16 @@ class Plugin:
self.__plugin_config = plugin_config self.__plugin_config = plugin_config
self.__bot = bot 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 @property
def channels(self) -> Sequence[str]: def channels(self) -> Sequence[str]:
if "channels" in self.plugin_config: if "channels" in self.plugin_config: