Exemplo n.º 1
0
 def __init__(self, db_name: str = "users"):
     self.base = BetterBotBase(db_name, "dat")
     self.base.addPattern("programming_languages", [])
     self.base.addPattern("last_collective_vote", 0)
     self.base.addPattern("github_profile", "")
     self.base.addPattern("supporters", [])
     self.base.addPattern("opponents", [])
     self.base.addPattern("karma", 0)
Exemplo n.º 2
0
class BetterBotBaseDataService:
    """Class for interacting with the database.
    """
    def __init__(self, db_name: str = "users"):
        self.base = BetterBotBase(db_name, "dat")
        self.base.addPattern("programming_languages", [])
        self.base.addPattern("last_collective_vote", 0)
        self.base.addPattern("github_profile", "")
        self.base.addPattern("supporters", [])
        self.base.addPattern("opponents", [])
        self.base.addPattern("karma", 0)

    def get_or_create_user(self,
                           uid: int,
                           vk: Optional[Vk] = None) -> BetterUser:
        """Returns a user object. Automatically creates it, if need.

        :param uid: user ID
        :param vk: Vk instance
        """
        if self.base.notInBD(uid):
            if vk:
                name = vk.users.get(user_ids=uid)['response'][0]["first_name"]
            else:
                name = "Пользователь"
            return self.base.addNew(uid=uid, name=name)
        return self.base.load(uid)

    def get_user(self, uid: int, vk: Optional[Vk] = None) -> BetterUser:
        """Alias for get_or_create_user.
        """
        return self.get_or_create_user(uid, vk)

    def get_users(self,
                  other_keys: List[str],
                  sort_key: Optional[Callable[[Any], Any]],
                  reverse_sort: bool = True) -> List[Dict[str, Any]]:
        """Returns users and their key values.

        :param other_key: list of user keys
        :param sort_key: base key
        :param reverse_sort: if True returns reversed list.
        """
        users = self.base.getByKeys(*other_keys)
        if sort_key:
            users = sorted(users, key=sort_key, reverse=reverse_sort)
        return users

    @staticmethod
    def get_user_sorted_programming_languages(
            user: BetterUser,
            sort: bool = True,
            reverse_sort: bool = False) -> List[str]:
        """Returns user's programming languages.

        :param user: -- user object
        :param sort: -- return sorted list, if True
        :param reverse_sort: -- uses for {sort} arg.
        """
        languages = BetterBotBaseDataService.get_user_property(
            user, "programming_languages")
        languages = languages if isinstance(languages, list) else []
        if sort:
            return sorted(languages, reverse=reverse_sort)
        return languages

    @staticmethod
    def get_user_property(user: Union[Dict[str, Any], BetterUser],
                          property_name: str) -> Any:
        """
        :param user: dict or BetterUser
        :param property_name: needed property
        """
        return user[property_name] if isinstance(
            user, dict) else eval(f"user.{property_name}")

    @staticmethod
    def set_user_property(user: Union[Dict[str, Any], BetterUser],
                          property_name: str, value: Any) -> NoReturn:
        """Changes user property

        :param user: dict or BetterUser
        :param property_name: needed property
        :param value: new value
        """
        if isinstance(user, dict):
            user[property_name] = value
        else:
            exec(f"user.{property_name} = value")

    def save_user(self, user: BetterUser) -> NoReturn:
        self.base.save(user)
Exemplo n.º 3
0
from random import randint

from saya import Vk
from social_ethosa import BetterBotBase
from datetime import datetime, timedelta
import regex

import patterns

from tokens import BotToken
from userbot import UserBot
from config import *

base = BetterBotBase("users", "dat")
base.addPattern("rating", 0)
base.addPattern("programming_languages", [])
base.addPattern("current", [])
base.addPattern("current_sub", [])


class V(Vk):
    def __init__(self):
        Vk.__init__(self, token=BotToken, group_id=bot_group_id, debug=True)
        self.messages_to_delete = {}
        self.userbot = UserBot()
        self.debug = True

    def message_new(self, event):
        """
        Handling all new messages.
        """
Exemplo n.º 4
0
    def __init__(self, token, group_id, debug=True):
        Vk.__init__(self, token=token, group_id=group_id, debug=debug)
        self.messages_to_delete = {}
        self.userbot = UserBot()
        self.debug = True

        base = BetterBotBase("users", "dat")
        base.addPattern("karma", 0)
        base.addPattern("programming_languages", [])
        base.addPattern("github_profile", "")
        base.addPattern("supporters", [])
        base.addPattern("opponents", [])
        base.addPattern("last_collective_vote", 0)

        self.base = base
Exemplo n.º 5
0
    def __init__(self, token, group_id, debug=True):
        Vk.__init__(self, token=token, group_id=group_id, debug=debug)
        self.messages_to_delete = {}
        self.userbot = UserBot()
        self.debug = True

        base = BetterBotBase("users", "dat")
        #base.addPattern("rating", 0)
        base.addPattern("karma", 0)
        base.addPattern("programming_languages", [])
        base.addPattern("github_profile", "")
        #base.addPattern("current", [])
        base.addPattern("supporters", [])
        #base.addPattern("current_sub", [])
        base.addPattern("opponents", [])
        base.addPattern("last_collective_vote", 0)

        #xusers = base.getSortedByKeys("karma", otherKeys=["current", "current_sub"])
        #for xuser in xusers:
        #    uuser = base.load(xuser["uid"])
        #    uuser.supporters = []
        #    uuser.opponents = []
        #    uuser.karma = 0
        #    base.save(uuser)

        self.base = base