Skip to content

kamihi.bot.action ⚓︎

Action helper class.

License

MIT

Classes:

Name Description
Action

Action class for Kamihi bot.

Action ⚓︎

Action(
    name: str,
    commands: list[str],
    description: str,
    func: Callable,
)

Action class for Kamihi bot.

This class provides helpers for defining actions, their commands and their handlers.

Attributes:

Name Type Description
name str

The name of the action.

commands list[str]

List of commands associated.

description str

Description of the action.

Initialize the Action class.

Parameters:

Name Type Description Default

name ⚓︎

str

The name of the action.

required

commands ⚓︎

list[str]

List of commands associated.

required

description ⚓︎

str

Description of the action.

required

func ⚓︎

Callable

The function to be executed when the action is called.

required

Methods:

Name Description
clean_up

Clean up the action from the database.

is_valid

Check if the action is valid.

save_to_db

Save the action to the database.

Source code in src/kamihi/bot/action.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(self, name: str, commands: list[str], description: str, func: Callable) -> None:
    """
    Initialize the Action class.

    Args:
        name (str): The name of the action.
        commands (list[str]): List of commands associated.
        description (str): Description of the action.
        func (Callable): The function to be executed when the action is called.

    """
    self.name = name
    self.commands = commands
    self.description = description

    self._func = func
    self._logger = logger.bind(action=self.name)

    self._validate_commands()
    self._validate_function()

    if self.is_valid():
        self._db_object = self.save_to_db()
        self._logger.debug("Successfully registered")
    else:
        self._db_object = None
        self._logger.warning("Failed to register")

handler property ⚓︎

handler: AuthHandler

Construct a CommandHandler for the action.

clean_up classmethod ⚓︎

clean_up(keep: list[str]) -> None

Clean up the action from the database.

Source code in src/kamihi/bot/action.py
145
146
147
148
@classmethod
def clean_up(cls, keep: list[str]) -> None:
    """Clean up the action from the database."""
    RegisteredAction.objects(name__nin=keep).delete()

is_valid ⚓︎

is_valid() -> bool

Check if the action is valid.

Source code in src/kamihi/bot/action.py
134
135
136
def is_valid(self) -> bool:
    """Check if the action is valid."""
    return self._valid

save_to_db ⚓︎

save_to_db() -> RegisteredAction

Save the action to the database.

Source code in src/kamihi/bot/action.py
138
139
140
141
142
143
def save_to_db(self) -> RegisteredAction:
    """Save the action to the database."""
    return RegisteredAction.objects(name=self.name).upsert_one(
        name=self.name,
        description=self.description,
    )