Skip to content

Commit e6e8afe

Browse files
konardclaude
andcommitted
Implement automatic karma-based chat membership management
- Add configuration for karma-based chats with customizable thresholds - Create KarmaChatManager class to handle automatic member management - Hook into karma changes to automatically add/remove users from chats - Add management commands for manual membership checks and status - Include comprehensive tests and usage examples - Support multiple chats with different karma requirements Addresses issue #131: Users are automatically added to chats when karma ≥ 2 and removed when karma < 2. System is configurable for any karma threshold and supports multiple chats with different levels. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8ca92cb commit e6e8afe

File tree

8 files changed

+697
-2
lines changed

8 files changed

+697
-2
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Example usage of the karma-based chat membership system.
5+
6+
This script demonstrates how to configure and use the automatic
7+
chat membership management based on user karma levels.
8+
"""
9+
10+
# Example configuration that would go in config.py:
11+
12+
EXAMPLE_KARMA_BASED_CHATS = [
13+
{
14+
"chat_id": 2000000001, # Main programming chat
15+
"karma_threshold": 2, # Users need 2+ karma to join
16+
"name": "Main Programming Chat"
17+
},
18+
{
19+
"chat_id": 2000000002, # Advanced developers chat
20+
"karma_threshold": 10, # Users need 10+ karma to join
21+
"name": "Advanced Developers"
22+
},
23+
{
24+
"chat_id": 2000000003, # Expert level chat
25+
"karma_threshold": 50, # Users need 50+ karma to join
26+
"name": "Expert Developers"
27+
},
28+
{
29+
"chat_id": 2000000004, # Mentor level chat
30+
"karma_threshold": 100, # Users need 100+ karma to join
31+
"name": "Mentors & Leads"
32+
}
33+
]
34+
35+
# Example scenarios:
36+
37+
def example_scenarios():
38+
"""Show example scenarios of how the system works."""
39+
40+
scenarios = [
41+
{
42+
"user": "Alice",
43+
"karma": 1,
44+
"description": "New user with 1 karma - not in any karma chats yet"
45+
},
46+
{
47+
"user": "Bob",
48+
"karma": 3,
49+
"description": "Gets added to Main Programming Chat (threshold: 2)"
50+
},
51+
{
52+
"user": "Charlie",
53+
"karma": 15,
54+
"description": "Gets added to Main Programming Chat and Advanced Developers (thresholds: 2, 10)"
55+
},
56+
{
57+
"user": "David",
58+
"karma": 75,
59+
"description": "Gets added to first three chats (thresholds: 2, 10, 50)"
60+
},
61+
{
62+
"user": "Eve",
63+
"karma": 150,
64+
"description": "Gets added to all chats (thresholds: 2, 10, 50, 100)"
65+
}
66+
]
67+
68+
print("🎯 Karma-Based Chat Membership Examples")
69+
print("=" * 50)
70+
71+
for scenario in scenarios:
72+
print(f"\n👤 {scenario['user']} (Karma: {scenario['karma']})")
73+
print(f" {scenario['description']}")
74+
75+
# Show which chats they would be in
76+
eligible_chats = []
77+
for chat in EXAMPLE_KARMA_BASED_CHATS:
78+
if scenario['karma'] >= chat['karma_threshold']:
79+
eligible_chats.append(f"{chat['name']} (≥{chat['karma_threshold']})")
80+
81+
if eligible_chats:
82+
print(f" 📩 Eligible for: {', '.join(eligible_chats)}")
83+
else:
84+
print(f" ❌ Not eligible for any karma-based chats")
85+
86+
def example_commands():
87+
"""Show example bot commands for managing karma chats."""
88+
89+
commands = [
90+
{
91+
"command": "check chat membership",
92+
"description": "Check your membership in all karma-based chats and update if needed"
93+
},
94+
{
95+
"command": "check all membership",
96+
"description": "Admin command to check and update all users' memberships"
97+
},
98+
{
99+
"command": "chat status",
100+
"description": "Show status of current chat (member counts, karma threshold)"
101+
},
102+
{
103+
"command": "chat status 2000000001",
104+
"description": "Show status of specific chat by ID"
105+
},
106+
{
107+
"command": "+",
108+
"description": "Give karma to a user (triggers automatic membership check)"
109+
},
110+
{
111+
"command": "-",
112+
"description": "Remove karma from a user (triggers automatic membership check)"
113+
}
114+
]
115+
116+
print("\n🤖 Available Bot Commands")
117+
print("=" * 50)
118+
119+
for cmd in commands:
120+
print(f"\n💬 '{cmd['command']}'")
121+
print(f" {cmd['description']}")
122+
123+
def example_workflow():
124+
"""Show example workflow of how the system operates."""
125+
126+
print("\n⚙️ System Workflow")
127+
print("=" * 50)
128+
129+
steps = [
130+
"1. User receives karma (+1 or -1) from another user",
131+
"2. System automatically checks user's new karma level",
132+
"3. For each configured karma-based chat:",
133+
" a. Check if user meets karma threshold",
134+
" b. Check if user is currently in the chat",
135+
" c. Add user to chat if they meet threshold but aren't in it",
136+
" d. Remove user from chat if they don't meet threshold but are in it",
137+
"4. Log all membership changes for admin review",
138+
"5. Send confirmation messages for successful operations"
139+
]
140+
141+
for step in steps:
142+
print(f"\n{step}")
143+
144+
def example_configuration_tips():
145+
"""Provide tips for configuring the karma chat system."""
146+
147+
print("\n💡 Configuration Tips")
148+
print("=" * 50)
149+
150+
tips = [
151+
"Start with low thresholds (2-5 karma) for basic chats",
152+
"Create progressive tiers (2, 10, 25, 50, 100)",
153+
"Test with a small group before rolling out to all chats",
154+
"Monitor logs to ensure the system is working correctly",
155+
"Consider having fallback manual commands for edge cases",
156+
"Set up different thresholds for different types of chats",
157+
"Use descriptive names for easier administration"
158+
]
159+
160+
for i, tip in enumerate(tips, 1):
161+
print(f"\n{i}. {tip}")
162+
163+
if __name__ == "__main__":
164+
example_scenarios()
165+
example_commands()
166+
example_workflow()
167+
example_configuration_tips()
168+
169+
print("\n" + "=" * 50)
170+
print("🎉 Karma-based chat membership system is ready!")
171+
print("Configure your KARMA_BASED_CHATS in config.py to get started.")

python/__main__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import requests
99

1010
from modules import (
11-
BetterBotBaseDataService, Commands
11+
BetterBotBaseDataService, Commands, KarmaChatManager
1212
)
1313
from tokens import BOT_TOKEN
1414
from userbot import UserBot
@@ -39,6 +39,8 @@ def __init__(
3939
self.userbot = UserBot()
4040
self.data = BetterBotBaseDataService()
4141
self.commands = Commands(self, self.data)
42+
self.karma_chat_manager = KarmaChatManager(self)
43+
self.commands.set_karma_chat_manager(self.karma_chat_manager)
4244
self.commands.register_cmds(
4345
(patterns.HELP, self.commands.help_message),
4446
(patterns.INFO, self.commands.info_message),
@@ -63,7 +65,10 @@ def __init__(
6365
(patterns.WHAT_IS, self.commands.what_is),
6466
(patterns.WHAT_MEAN, self.commands.what_is),
6567
(patterns.APPLY_KARMA, self.commands.apply_karma),
66-
(patterns.GITHUB_COPILOT, self.commands.github_copilot)
68+
(patterns.GITHUB_COPILOT, self.commands.github_copilot),
69+
(patterns.CHECK_CHAT_MEMBERSHIP, self.commands.check_chat_membership),
70+
(patterns.CHECK_ALL_MEMBERSHIP, self.commands.check_all_membership),
71+
(patterns.CHAT_STATUS, self.commands.chat_status)
6772
)
6873

6974
def message_new(

python/config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@
2727
2000000011
2828
]
2929

30+
# Configuration for automatic karma-based chat membership
31+
# Each entry defines a chat with its karma threshold for automatic membership
32+
KARMA_BASED_CHATS = [
33+
{
34+
"chat_id": 2000000001, # Chat ID where users should be added/removed
35+
"karma_threshold": 2, # Minimum karma required to be in the chat
36+
"name": "Main Chat" # Optional name for logging
37+
},
38+
# Example of multiple chats with different thresholds:
39+
# {
40+
# "chat_id": 2000000002,
41+
# "karma_threshold": 10,
42+
# "name": "Advanced Chat"
43+
# },
44+
# {
45+
# "chat_id": 2000000003,
46+
# "karma_threshold": 50,
47+
# "name": "Expert Chat"
48+
# }
49+
]
50+
3051
POSITIVE_VOTES_PER_KARMA = 2
3152
NEGATIVE_VOTES_PER_KARMA = 3
3253

python/modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .data_service import BetterBotBaseDataService
55
from .data_builder import DataBuilder
66
from .vk_instance import VkInstance
7+
from .chat_membership import KarmaChatManager
78
from .utils import (
89
get_default_programming_language,
910
contains_string,

0 commit comments

Comments
 (0)