/botly/users.py (4e4eb32e073028e8e5eef0ad3ce51365b7e9a0d2) (2102 bytes) (mode 100644) (type blob)

#!/usr/bin/env python
"""Contains Users class which represents the users database"""

from botly.db import Db


class Users(Db):
    
    def __init__(self):
        assert self.is_loaded(), "Db is not loaded upon Users object creation."
        self.userCache = []
        self.root = self.get_node('/BotDb/Knowledge')

    # Shortcut higher level methods:

    def is_master(self, userid):
        """Returns whether or not this user can control the bot."""
        user = self.get(userid)
        if user is not None:
            if user['IsMaster'] == '1':
                return True
        return False

    def affinity(self, userid):
        """Returns the bot affinity to the user."""
        user = self.get(userid)
        if user is not None:
            return int(user['BotAffinity'])
        return 0

    def change_affinity(self, userid, delta):
        """Increase or decrease the affinity with the user."""
        curr = self.affinity(userid)
        return self.set(userid, 'BotAffinity', str(curr + delta))

    # Low level methods:

    def get(self, userid):
        """Returns the user with given ID or None if it does not exist."""
        user = self._get_cached_user(userid)
        if user is None:
            user = self.query_object('Users', userid)
        return user

    def set(self, userid, valueName, value):
        """Modify the user valueName with given value."""
        user = self.get(userid)
        if user is not None:
            user[valueName] = value
            return self.store_object('Users', user) 
        return False

    def get_all(self):
        """Returns all users from the database."""
        users = self.query_object_list('Users')
        self.userCache.clear()
        self.userCache = users
        return users

    # Internal methods

    def _new_user(self, userid):
        # TODO
        pass

    def _get_cached_user(self, userid):
        """Returns the cached user if it exists, None otherwise."""
        for user in self.userCache:
            if user['Id'] == userid:
                return user
        return None




Mode Type Size Ref File
100644 blob 10175 e454a52586f29b8ce8a6799163eac1f875e9ac01 LICENSE
100644 blob 912 43cc367ee8608a8760e6665ac29c411d58dc8b99 README.md
040000 tree - 66dcfaf2ba5cd5742f36e01d7785daf5113c2107 botly
100644 blob 172 3f148ab99d152565c46e51fd45e51030417f7faa example.py
040000 tree - e05b34e00f419ee6a20bf1bb6a0603727b1b4399 examplebot
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/detche/Botly

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/detche/Botly

Clone this repository using git:
git clone git://git.rocketgit.com/user/detche/Botly

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main