-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_from_json.py
More file actions
282 lines (227 loc) · 8.88 KB
/
migrate_from_json.py
File metadata and controls
282 lines (227 loc) · 8.88 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
============================================================================
MIGRATION SCRIPT - JSON TO SQLITE
============================================================================
Migrate old bot data from JSON files to new SQLite database.
This script will:
1. Read old user_data.json and gamification_data.json
2. Create new SQLite database
3. Import all user data, warnings, XP, levels, etc.
4. Preserve as much data as possible
Usage:
python migrate_from_json.py
Before running:
- Make sure old JSON files are in anti_spam/ folder
- Backup your JSON files first!
- Bot should NOT be running
"""
import json
import asyncio
import os
from datetime import datetime
from pathlib import Path
# Import database
from database import Database
import config
async def migrate():
"""Main migration function."""
print("=" * 60)
print("TENBOT DATA MIGRATION - JSON TO SQLITE")
print("=" * 60)
# Check if old files exist
old_user_data_path = "anti_spam/user_data.json"
old_gamif_data_path = "anti_spam/gamification_data.json"
if not os.path.exists(old_user_data_path):
print(f"❌ File not found: {old_user_data_path}")
print(" Place your old user_data.json in anti_spam/ folder")
return
if not os.path.exists(old_gamif_data_path):
print(f"❌ File not found: {old_gamif_data_path}")
print(" Place your old gamification_data.json in anti_spam/ folder")
return
# Load old data
print("\n📂 Loading old JSON data...")
with open(old_user_data_path, 'r') as f:
user_data = json.load(f)
print(f"✅ Loaded {len(user_data)} users from user_data.json")
with open(old_gamif_data_path, 'r') as f:
gamification_data = json.load(f)
print(f"✅ Loaded {len(gamification_data)} users from gamification_data.json")
# Initialize database
print("\n🗄️ Initializing new database...")
db = Database(config.DATABASE_PATH)
await db.initialize()
print("✅ Database created successfully!")
# Migrate user data
print("\n👥 Migrating user data...")
migrated_users = 0
migrated_warnings = 0
migrated_gamif = 0
for user_id, data in user_data.items():
try:
# Create user
await db.execute(
"""
INSERT OR IGNORE INTO users (
user_id, username, display_name,
total_messages, total_reactions_given, total_reactions_received,
total_voice_minutes, joined_server
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
user_id,
data.get('username', f'User_{user_id}'),
data.get('username', f'User_{user_id}'),
data.get('messages', 0),
data.get('reactions_given', 0),
data.get('reactions_received', 0),
data.get('voice_time', 0),
data.get('join_date')
)
)
migrated_users += 1
# Migrate warnings
warnings = data.get('warnings', 0)
warning_types = data.get('warning_types', [])
for i in range(warnings):
warning_type = warning_types[i] if i < len(warning_types) else 'unknown'
await db.execute(
"""
INSERT INTO warnings (
user_id, reason, issued_by, warning_type, severity, issued_at
) VALUES (?, ?, ?, ?, ?, ?)
""",
(
user_id,
f"Migrated from old bot: {warning_type}",
'system_migration',
warning_type,
'medium',
data.get('last_warning_time', datetime.now().isoformat())
)
)
migrated_warnings += 1
# Initialize trust scores and reputation
await db.execute(
"INSERT OR IGNORE INTO trust_scores (user_id) VALUES (?)",
(user_id,)
)
await db.execute(
"INSERT OR IGNORE INTO reputation (user_id) VALUES (?)",
(user_id,)
)
except Exception as e:
print(f"⚠️ Error migrating user {user_id}: {e}")
print(f"✅ Migrated {migrated_users} users")
print(f"✅ Migrated {migrated_warnings} warnings")
# Migrate gamification data
print("\n🎮 Migrating gamification data...")
for user_id, data in gamification_data.items():
try:
xp = data.get('xp', 0)
level = data.get('level', 1)
streak = data.get('streak_days', 0)
achievements = data.get('achievements', [])
await db.execute(
"""
INSERT OR REPLACE INTO gamification (
user_id, total_xp, current_level, current_streak_days,
last_active_date, total_xp_earned, achievements_unlocked
) VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
user_id,
xp,
level,
streak,
data.get('last_active_date'),
data.get('total_xp_earned', xp),
len(achievements)
)
)
# Migrate achievements
for achievement_key in achievements:
achievement_info = config.ACHIEVEMENTS.get(achievement_key, {})
await db.execute(
"""
INSERT OR IGNORE INTO achievements (
user_id, achievement_key, achievement_name,
achievement_description, xp_reward
) VALUES (?, ?, ?, ?, ?)
""",
(
user_id,
achievement_key,
achievement_info.get('name', achievement_key),
achievement_info.get('description', ''),
achievement_info.get('xp_reward', 0)
)
)
migrated_gamif += 1
except Exception as e:
print(f"⚠️ Error migrating gamification for {user_id}: {e}")
print(f"✅ Migrated {migrated_gamif} gamification records")
# Create backup of database
print("\n💾 Creating database backup...")
await db.backup()
print("✅ Backup created!")
# Close database
await db.close()
# Summary
print("\n" + "=" * 60)
print("MIGRATION COMPLETE!")
print("=" * 60)
print(f"✅ Users migrated: {migrated_users}")
print(f"✅ Warnings migrated: {migrated_warnings}")
print(f"✅ Gamification records migrated: {migrated_gamif}")
print()
print("📝 Next steps:")
print(" 1. Verify data by running: python bot.py")
print(" 2. Check a few user profiles with /stats")
print(" 3. If everything looks good, archive the old JSON files")
print()
print("⚠️ Note: Trust scores will be calculated automatically when bot starts")
print("=" * 60)
async def verify_migration():
"""Verify migration was successful."""
print("\n🔍 Verifying migration...")
db = Database(config.DATABASE_PATH)
await db.initialize()
# Count records
user_count = await db.fetch_value("SELECT COUNT(*) FROM users")
warning_count = await db.fetch_value("SELECT COUNT(*) FROM warnings")
gamif_count = await db.fetch_value("SELECT COUNT(*) FROM gamification")
achievement_count = await db.fetch_value("SELECT COUNT(*) FROM achievements")
print(f"\n📊 Database Statistics:")
print(f" Users: {user_count}")
print(f" Warnings: {warning_count}")
print(f" Gamification records: {gamif_count}")
print(f" Achievements: {achievement_count}")
# Sample some data
sample_users = await db.fetch_all(
"SELECT user_id, username, total_messages FROM users LIMIT 5"
)
if sample_users:
print(f"\n👥 Sample Users:")
for user in sample_users:
print(f" - {user['username']}: {user['total_messages']} messages")
await db.close()
print("\n✅ Verification complete!")
if __name__ == "__main__":
import sys
print("TENBOT Migration Tool")
print()
print("This will migrate your old JSON data to the new SQLite database.")
print("Make sure you have backups of your JSON files!")
print()
response = input("Continue? (yes/no): ").lower().strip()
if response == 'yes':
# Run migration
asyncio.run(migrate())
# Verify
print()
verify_response = input("Run verification? (yes/no): ").lower().strip()
if verify_response == 'yes':
asyncio.run(verify_migration())
else:
print("❌ Migration cancelled")