Source code for pinger_bot.ext.events

"""Module for handling events."""
import lightbulb
from hikari.events import lifetime_events
from lightbulb import events
from structlog import stdlib as structlog

from pinger_bot import bot
from pinger_bot.config import gettext as _
from pinger_bot.ext import scheduling

log = structlog.get_logger()

[docs]plugin = lightbulb.Plugin(name="events")
""":class:`lightbulb.Plugin <lightbulb.plugins.Plugin>` object."""
[docs]class Events: """Class for handling events.""" @staticmethod @plugin.listener(events.SlashCommandInvocationEvent)
[docs] async def pre_execution(event: events.SlashCommandInvocationEvent) -> None: """Pre-execution hook. Just logs the call of command. Args: event: Event that triggered listener. """ if event.context.command is None: return options = {} for key in event.context.raw_options: options[key] = event.context.raw_options[key] log.info(_("Command '{}'").format(event.context.command.name), user=str(event.context.author), **options)
@staticmethod @plugin.listener(lifetime_events.StartedEvent)
[docs] async def on_started(__: lifetime_events.StartedEvent) -> None: """On-started hook. Just logs that the bot started and run scheduler.""" log.info(_("Bot running! For stop it, use CTRL C.")) scheduling.scheduler.start()
@staticmethod @plugin.listener(lifetime_events.StoppingEvent)
[docs] async def on_stopping(__: lifetime_events.StoppingEvent) -> None: """On-started hook. Just logs that the bot stopping and stop scheduler.""" log.info(_("Bot stopping. Bye!")) scheduling.scheduler.shutdown()
[docs]def load(bot_obj: bot.PingerBot) -> None: """Load the :py:data:`plugin`.""" bot_obj.add_plugin(plugin)