def __create_user(user:Union[discord.Member, discord.User]) -> Karma: if not UserRepo.user_exist(user.id): UserRepo.create_user(user) item = Karma(user_id=str(user.id)) session.add(item) session.commit() return item
def updateMemberKarma(self, member_id: int, value: int): """Add karma to user""" # TODO This is duplicate for `update_karma_get` user = self.getMember(member_id) if user is None: session.add(Karma(discord_id=member_id, karma=value)) else: user.karma += value session.commit()
def get_karma(self, member_id): karma_object = self.get_karma_object(member_id) if karma_object is None: karma_object = Karma(karma=0, positive=0, negative=0) order = self.get_karma_position("karma", karma_object.karma) pos_order = self.get_karma_position("positive", karma_object.positive) neg_order = self.get_karma_position("negative", karma_object.negative) karma = Karma_row_data(karma_object.karma, order) positive = Karma_row_data(karma_object.positive, pos_order) negative = Karma_row_data(karma_object.negative, neg_order) result = Karma_data(karma, positive, negative) return result
def update_karma_give(self, giver, emoji_value, remove): if emoji_value > 0: if remove: column = "negative" else: column = "positive" else: if remove: column = "positive" else: column = "negative" if column == "negative": emoji_value *= -1 givers_karma = self.get_karma_object(giver.id) if givers_karma is not None: setattr(givers_karma, column, getattr(givers_karma, column) + emoji_value) else: new_giver = Karma(discord_id=giver.id) setattr(new_giver, column, emoji_value) session.add(new_giver)
def update_karma_give(self, giver, emoji_value, remove): if emoji_value > 0: if remove: column = 'negative' else: column = 'positive' else: if remove: column = 'positive' else: column = 'negative' if column == 'negative': emoji_value *= -1 givers_karma = self.get_karma_object(giver.id) if givers_karma is not None: setattr(givers_karma, column, getattr(givers_karma, column) + emoji_value) else: new_giver = Karma(member_ID=giver.id) setattr(new_giver, column, emoji_value) session.add(new_giver)
def update_karma_get(self, member, emoji_value): members_karma = self.get_karma_object(member.id) if members_karma is not None: members_karma.karma += emoji_value else: session.add(Karma(discord_id=member.id, karma=emoji_value))
def load_dump(filename: str): init_db(False) session.query(Karma).delete() session.query(Karma_emoji).delete() session.query(Permit).delete() session.query(Valid_person).delete() session.commit() print(f'Loading dump from {filename}') data = database.base.metadata.tables.keys() for row in data: print(row) with open(filename, "r", encoding='utf-8') as backup_file: data = backup_file.readlines() inserts = [line for line in data if line.startswith("INSERT")] karma_values = [] for insert in inserts: values = insert.split("VALUES", 1)[1] if insert.startswith("INSERT INTO `bot_karma`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 3): karma_values.append( Karma(member_ID=values[i], karma=values[i + 1])) elif insert.startswith("INSERT INTO `bot_karma_giving`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 4): karma_values.append( Karma(member_ID=values[i], positive=values[i + 1], negative=values[i + 2])) elif insert.startswith("INSERT INTO `bot_karma_emoji`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 2): session.add( Karma_emoji(emoji_ID=values[i], value=values[i + 1])) elif insert.startswith("INSERT INTO `bot_permit`"): values = values[1:-2] values = values.replace('(', '').replace(')', '') values = re.split(r',(?=\')', values) values = [value.replace('\'', '') for value in values] for i in range(0, len(values), 3): session.add(Permit(login=values[i], discord_ID=values[i + 2])) elif insert.startswith("INSERT INTO `bot_valid_persons`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 5): session.add( Valid_person( login=values[i], name=values[i + 1], year=values[i + 2], code=values[i + 3] if values[i + 3] != "NULL" else None, status=values[i + 4])) for karma in karma_values: session.merge(karma) session.commit()