diff options
Diffstat (limited to 'models.py')
| -rw-r--r-- | models.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/models.py b/models.py new file mode 100644 index 0000000..5cee04a --- /dev/null +++ b/models.py @@ -0,0 +1,71 @@ +"""Models used by geldschieberbot""" + +import json +import typing as T + +from dataclasses import dataclass, field + +from utils import to_euro + + +@dataclass +class MessageContext: + """Class representing the context of a message passed to a command function""" + sender_number: str + sender: T.Optional[str] + args: list[str] + body: list[str] + timestamp: str + + +@dataclass +class Modification: + """Class representing a single modification to the balance + + Amount is transfered from donor to the recipient. + """ + recipient: str + donor: str + amount: int + + def in_string(self) -> str: + """Format the change using the recipient as initiator""" + return (f'{self.recipient} {"->" if self.amount < 0 else "<-"}' + f' {to_euro(abs(self.amount))} {self.donor}') + + def out_string(self) -> str: + """Format the change using the donor as initiator""" + return (f'{self.donor} {"->" if self.amount < 0 else "<-"}' + f' {to_euro(abs(self.amount))} {self.recipient}') + + +@dataclass +class Change: + """Class representing a change to the state caused by a single command""" + cmd: list[str] + modifications: list[Modification] + timestamp: str + rewind_cmds: list[list[str]] = field(default_factory=lambda: []) + + +@dataclass +class Quote: + """Class representing a message to quote""" + timestamp: str + author: str + + +class GeldschieberbotJSONEncoder(json.JSONEncoder): + """Custom JSONEncoder supporting our dataclasses""" + + def default(self, o): + if isinstance(o, (Modification, Change)): + return o.__dict__ + return json.JSONEncoder.default(self, o) + + +@dataclass +class Reply: + """Class representing a reply from the bot""" + msg: str + attachment: T.Optional[str] = None |
