Skip to content

Commit abc8786

Browse files
konardclaude
andcommitted
Add optional reasons to karma voting
- Modified APPLY_KARMA regex pattern to capture optional reasons after karma operators - Updated apply_karma method to extract and pass reason parameter - Enhanced build_karma_change to display reasons in karma change messages - Added test case for karma reasons functionality - Supports formats like "+ решение сработало" and "- вопрос недостаточно конкретный" 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 2d8f2b3 commit abc8786

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

python/modules/commands.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def apply_karma(self) -> NoReturn:
174174
operator = self.matched.group("operator")[0]
175175
amount = self.matched.group("amount")
176176
amount = int(amount) if amount else 0
177+
reason = self.matched.group("reason")
177178

178179
utcnow = datetime.utcnow()
179180

@@ -211,7 +212,7 @@ def apply_karma(self) -> NoReturn:
211212
return
212213

213214
user_karma_change, selected_user_karma_change, collective_vote_applied, voters = self.apply_karma_change(
214-
operator, amount)
215+
operator, amount, reason)
215216

216217
if collective_vote_applied:
217218
self.current_user.last_collective_vote = int(utcnow.timestamp())
@@ -223,14 +224,15 @@ def apply_karma(self) -> NoReturn:
223224
self.data_service.save_user(self.user)
224225
self.vk_instance.send_msg(
225226
CommandsBuilder.build_karma_change(
226-
user_karma_change, selected_user_karma_change, voters),
227+
user_karma_change, selected_user_karma_change, voters, reason),
227228
self.peer_id)
228229
self.vk_instance.delete_message(self.peer_id, self.msg_id)
229230

230231
def apply_karma_change(
231232
self,
232233
operator: str,
233-
amount: int
234+
amount: int,
235+
reason: Optional[str] = None
234236
) -> Tuple[
235237
Optional[Tuple[int, str, int, int]], # current user karma changed
236238
Optional[Tuple[int, str, int, int]], # selected user karma changed

python/modules/commands_builder.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,16 @@ def build_top_users(
174174
def build_karma_change(
175175
user_karma_change: Optional[Tuple[int, str, int, int]],
176176
selected_user_karma_change: Optional[Tuple[int, str, int, int]],
177-
voters: List[int]
177+
voters: List[int],
178+
reason: Optional[str] = None
178179
) -> Optional[str]:
179180
"""Builds karma changing
180181
"""
181182
if selected_user_karma_change:
183+
reason_text = f" Причина: {reason}" if reason else ""
182184
if user_karma_change:
183-
return ("Карма изменена: [id%s|%s] [%s]->[%s], [id%s|%s] [%s]->[%s]." %
184-
(user_karma_change + selected_user_karma_change))
185-
return ("Карма изменена: [id%s|%s] [%s]->[%s]. Голосовали: (%s)" %
186-
(selected_user_karma_change + (", ".join([f"@id{voter}" for voter in voters]),)))
185+
return ("Карма изменена: [id%s|%s] [%s]->[%s], [id%s|%s] [%s]->[%s].%s" %
186+
(user_karma_change + selected_user_karma_change + (reason_text,)))
187+
return ("Карма изменена: [id%s|%s] [%s]->[%s]. Голосовали: (%s)%s" %
188+
(selected_user_karma_change + (", ".join([f"@id{voter}" for voter in voters]), reason_text)))
187189
return None

python/patterns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
r'\A\s*(карма|karma)\s*\Z', IGNORECASE)
2020

2121
APPLY_KARMA = recompile(
22-
r'\A(\[id(?<selectedUserId>\d+)\|@\w+\])?\s*(?P<operator>\+|\-)(?P<amount>[0-9]*)\s*\Z')
22+
r'\A(\[id(?P<selectedUserId>\d+)\|@\w+\])?\s*(?P<operator>\+|\-)(?P<amount>[0-9]*)(?:\s+(?P<reason>.+?))?\s*\Z')
2323

2424
ADD_PROGRAMMING_LANGUAGE = recompile(
2525
r'\A\s*\+=\s*(?P<language>' + DEFAULT_LANGUAGES + r')\s*\Z', IGNORECASE)

python/tests.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,31 @@ def test_apply_collective_vote(
219219
def test_apply_karma_change(
220220
self
221221
) -> NoReturn:
222-
self.commands.apply_karma_change('-', 6)
222+
self.commands.apply_karma_change('-', 6, "тестовая причина")
223223
self.commands.karma_message()
224224

225+
@ordered
226+
def test_karma_with_reasons(
227+
self
228+
) -> NoReturn:
229+
# Test karma with reasons
230+
test_messages = [
231+
"+ решение сработало",
232+
"- вопрос недостаточно конкретный",
233+
"+5 отличная помощь",
234+
"-2 неточный ответ"
235+
]
236+
237+
for msg in test_messages:
238+
self.commands.msg = msg
239+
self.commands.match_command(patterns.APPLY_KARMA)
240+
if self.commands.matched:
241+
operator = self.commands.matched.group("operator")
242+
amount = self.commands.matched.group("amount")
243+
amount = int(amount) if amount else 0
244+
reason = self.commands.matched.group("reason")
245+
print(f"Message: '{msg}' -> Operator: '{operator}', Amount: {amount}, Reason: '{reason}'")
246+
225247

226248
if __name__ == '__main__':
227249
db = BetterBotBaseDataService("test_db")

0 commit comments

Comments
 (0)