-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_processing.py
More file actions
150 lines (122 loc) · 4.48 KB
/
command_processing.py
File metadata and controls
150 lines (122 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import sqlite3
def parse_command(text):
# Handle /CCP adduser <username>
if text.startswith("/CCP adduser"):
parts = text.split()
if len(parts) == 3:
command, action, user = parts
if action == "adduser":
return {"action": "adduser", "user": user}
# Handle /CCP listall
elif text.startswith("/CCP listall"):
return {"action": "listall"}
# Handle /CCP gloriousleader
elif text.startswith("/CCP gloriousleader"):
return {"action": "gloriousleader"}
# Handle /CCP help
elif text.startswith("/CCP help"):
return {"action": "help"}
# Handle /CCP <username> <points>
elif text.startswith("/CCP"):
parts = text.split()
if len(parts) == 3:
command, user, points_str = parts
try:
points = int(points_str)
return {"action": "update_points", "user": user, "points": points}
except ValueError:
return {"error": "Invalid points value"}
def process_command(command_data):
action = command_data.get("action")
if action == "gloriousleader":
return print_leader()
if action == "help":
return print_help()
if action == "adduser":
user = command_data.get("user")
result = add_user_to_database(user)
return f"User {user} has been added." if result else f"Failed to add user {user}. It may already exist."
elif action == "update_points":
user = command_data.get("user")
points = command_data.get("points")
if points is not None:
success, total_points = update_user_points(user, points)
return f"{points} points updated for user {user}. Total points: {total_points}." if success else f"Failed to update points for user {user}."
else:
return "Error: Invalid points value."
elif action == "listall":
return list_users_and_scores()
elif "error" in command_data:
return command_data["error"]
return "Invalid action"
def print_leader():
return "ATTENTION CITIZEN This is the Central Intelligentsia of the Chinese Communist Party Glory to the CCP"
def print_help():
return """
CCP COMMAND HELP:
/CCP adduser <NAME>
/CCP <NAME> <POINTS>
/CCP listall
/CCP gloriousleader
"""
def add_user_to_database(username):
conn = sqlite3.connect('points.db')
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO users (username) VALUES (?)", (username,))
conn.commit()
print(f"User {username} successfully added.")
return True
except sqlite3.IntegrityError:
print(f"User {username} already exists.")
return False
except Exception as e:
print(f"Exception: {e}")
return False
finally:
conn.close()
def update_user_points(username, points_to_add):
conn = sqlite3.connect('points.db')
cursor = conn.cursor()
try:
cursor.execute("SELECT COUNT(*) FROM users WHERE username=?", (username,))
if cursor.fetchone()[0] == 0:
print(f"User {username} does not exist.")
return False, 0
cursor.execute("SELECT points FROM user_points WHERE username=?", (username,))
result = cursor.fetchone()
if result:
current_points = result[0]
new_points = current_points + points_to_add
else:
new_points = points_to_add
cursor.execute("INSERT OR REPLACE INTO user_points (username, points) VALUES (?, ?)", (username, new_points))
conn.commit()
return True, new_points
except Exception as e:
print(f"Exception: {e}")
return False, 0
finally:
conn.close()
def list_users_and_scores():
conn = sqlite3.connect('points.db')
cursor = conn.cursor()
try:
cursor.execute('''
SELECT u.username, COALESCE(up.points, 0) as points
FROM users u
LEFT JOIN user_points up ON u.username = up.username
''')
rows = cursor.fetchall()
if not rows:
return "No users found."
result = "User Scores:\n"
for row in rows:
username, points = row
result += f"{username}: {points} points\n"
return result.strip()
except Exception as e:
print(f"Exception: {e}")
return "Failed to retrieve user scores."
finally:
conn.close()