jsdecryptor 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/Database/database.js +446 -0
  2. package/Database/migration.js +327 -0
  3. package/Database/pgSync.js +229 -0
  4. package/Database/settingsStore.js +305 -0
  5. package/config.js +32 -0
  6. package/dave.js +3063 -0
  7. package/davelib/antibadword.js +278 -0
  8. package/davelib/antilink.js +88 -0
  9. package/davelib/antilinkHelper.js +48 -0
  10. package/davelib/botConfig.js +71 -0
  11. package/davelib/connect4.js +111 -0
  12. package/davelib/converter.js +89 -0
  13. package/davelib/dicegame.js +114 -0
  14. package/davelib/exif.js +138 -0
  15. package/davelib/fakeContact.js +85 -0
  16. package/davelib/fontStyles.js +237 -0
  17. package/davelib/greetings.js +47 -0
  18. package/davelib/id.js +11 -0
  19. package/davelib/index.js +577 -0
  20. package/davelib/isAdmin.js +32 -0
  21. package/davelib/isBanned.js +12 -0
  22. package/davelib/isOwner.js +15 -0
  23. package/davelib/lightweight_store.js +169 -0
  24. package/davelib/messageConfig.js +15 -0
  25. package/davelib/messageHandler.js +87 -0
  26. package/davelib/myfunc.js +379 -0
  27. package/davelib/myfunc2.js +170 -0
  28. package/davelib/reactions.js +180 -0
  29. package/davelib/server.html +563 -0
  30. package/davelib/sticker.js +208 -0
  31. package/davelib/tempCleanup.js +53 -0
  32. package/davelib/tictactoe.js +104 -0
  33. package/davelib/uploadImage.js +100 -0
  34. package/davelib/uploader.js +110 -0
  35. package/davelib/welcome.js +100 -0
  36. package/davelib/wordchain.js +121 -0
  37. package/daveset.js +16 -0
  38. package/davexcore/ai/ai.js +100 -0
  39. package/davexcore/ai/aiGpt4.js +59 -0
  40. package/davexcore/ai/aimodels.js +284 -0
  41. package/davexcore/ai/aivideo.js +31 -0
  42. package/davexcore/ai/analyze.js +233 -0
  43. package/davexcore/ai/bard.js +101 -0
  44. package/davexcore/ai/bird.js +101 -0
  45. package/davexcore/ai/blackbox.js +30 -0
  46. package/davexcore/ai/character.js +80 -0
  47. package/davexcore/ai/copilot.js +58 -0
  48. package/davexcore/ai/dalle.js +39 -0
  49. package/davexcore/ai/davex.js +50 -0
  50. package/davexcore/ai/deepseek.js +96 -0
  51. package/davexcore/ai/grok.js +67 -0
  52. package/davexcore/ai/imagine.js +89 -0
  53. package/davexcore/ai/meta.js +82 -0
  54. package/davexcore/ai/mistral.js +101 -0
  55. package/davexcore/ai/perplexity.js +95 -0
  56. package/davexcore/ai/sora.js +44 -0
  57. package/davexcore/ai/speechwriter.js +46 -0
  58. package/davexcore/ai/vision.js +244 -0
  59. package/davexcore/ai/wormgpt.js +56 -0
  60. package/davexcore/anti/antiaudio.js +106 -0
  61. package/davexcore/anti/antibadword.js +313 -0
  62. package/davexcore/anti/antibug.js +156 -0
  63. package/davexcore/anti/anticall.js +219 -0
  64. package/davexcore/anti/antichart.js +280 -0
  65. package/davexcore/anti/antidelete.js +673 -0
  66. package/davexcore/anti/antideletestatus.js +535 -0
  67. package/davexcore/anti/antidemote.js +352 -0
  68. package/davexcore/anti/antidocument.js +105 -0
  69. package/davexcore/anti/antiedit.js +410 -0
  70. package/davexcore/anti/antifiles.js +109 -0
  71. package/davexcore/anti/antigroupmention.js +206 -0
  72. package/davexcore/anti/antiimage.js +105 -0
  73. package/davexcore/anti/antikick.js +125 -0
  74. package/davexcore/anti/antilink.js +237 -0
  75. package/davexcore/anti/antimention.js +143 -0
  76. package/davexcore/anti/antipromote.js +320 -0
  77. package/davexcore/anti/antisticker.js +105 -0
  78. package/davexcore/anti/antitag.js +191 -0
  79. package/davexcore/anti/antivideo.js +105 -0
  80. package/davexcore/anti/antiviewonce.js +396 -0
  81. package/davexcore/anti/groupanticall.js +262 -0
  82. package/davexcore/anti/mention.js +242 -0
  83. package/davexcore/automation/alwaysonline.js +226 -0
  84. package/davexcore/automation/autoReadReciepts.js +96 -0
  85. package/davexcore/automation/autoread.js +104 -0
  86. package/davexcore/automation/autorecording.js +197 -0
  87. package/davexcore/automation/autostatus.js +317 -0
  88. package/davexcore/automation/autotyping.js +197 -0
  89. package/davexcore/automation/chatbot.js +444 -0
  90. package/davexcore/automation/chatmanage.js +199 -0
  91. package/davexcore/automation/devReact.js +43 -0
  92. package/davexcore/automation/goodbye.js +181 -0
  93. package/davexcore/automation/greetings.js +154 -0
  94. package/davexcore/automation/welcome.js +187 -0
  95. package/davexcore/downloads/apk.js +157 -0
  96. package/davexcore/downloads/facebook.js +94 -0
  97. package/davexcore/downloads/gitclone.js +137 -0
  98. package/davexcore/downloads/igs.js +333 -0
  99. package/davexcore/downloads/instagram.js +133 -0
  100. package/davexcore/downloads/mediafire.js +144 -0
  101. package/davexcore/downloads/pinterest.js +38 -0
  102. package/davexcore/downloads/play.js +158 -0
  103. package/davexcore/downloads/saveStatus.js +3 -0
  104. package/davexcore/downloads/song.js +135 -0
  105. package/davexcore/downloads/spotify.js +133 -0
  106. package/davexcore/downloads/tiktok.js +167 -0
  107. package/davexcore/downloads/tiktokaudio.js +158 -0
  108. package/davexcore/downloads/video.js +188 -0
  109. package/davexcore/downloads/ytdl.js +252 -0
  110. package/davexcore/downloads/ytdocplay.js +130 -0
  111. package/davexcore/downloads/ytdocvideo.js +95 -0
  112. package/davexcore/downloads/yts.js +64 -0
  113. package/davexcore/games/connect4.js +267 -0
  114. package/davexcore/games/dice.js +286 -0
  115. package/davexcore/games/eightball.js +24 -0
  116. package/davexcore/games/hangman.js +60 -0
  117. package/davexcore/games/rps.js +25 -0
  118. package/davexcore/games/ship.js +36 -0
  119. package/davexcore/games/slot.js +21 -0
  120. package/davexcore/games/tictactoe.js +263 -0
  121. package/davexcore/games/trivia.js +46 -0
  122. package/davexcore/games/wordchain.js +242 -0
  123. package/davexcore/group/addmember.js +101 -0
  124. package/davexcore/group/ban.js +63 -0
  125. package/davexcore/group/blockUnblock.js +177 -0
  126. package/davexcore/group/clear.js +196 -0
  127. package/davexcore/group/creategroup.js +43 -0
  128. package/davexcore/group/demote.js +115 -0
  129. package/davexcore/group/disappear.js +67 -0
  130. package/davexcore/group/groupinfo.js +167 -0
  131. package/davexcore/group/groupmanage.js +133 -0
  132. package/davexcore/group/hidetag.js +108 -0
  133. package/davexcore/group/joinrequests.js +145 -0
  134. package/davexcore/group/kick.js +92 -0
  135. package/davexcore/group/kickall.js +63 -0
  136. package/davexcore/group/leave.js +38 -0
  137. package/davexcore/group/linkgroup.js +63 -0
  138. package/davexcore/group/mute.js +57 -0
  139. package/davexcore/group/online.js +117 -0
  140. package/davexcore/group/pmblocker.js +65 -0
  141. package/davexcore/group/promote.js +93 -0
  142. package/davexcore/group/resetlink.js +57 -0
  143. package/davexcore/group/staff.js +99 -0
  144. package/davexcore/group/tag.js +111 -0
  145. package/davexcore/group/tagadmins.js +88 -0
  146. package/davexcore/group/tagall.js +99 -0
  147. package/davexcore/group/tagnotadmin.js +92 -0
  148. package/davexcore/group/topmembers.js +202 -0
  149. package/davexcore/group/unban.js +64 -0
  150. package/davexcore/group/unmute.js +45 -0
  151. package/davexcore/group/warn.js +83 -0
  152. package/davexcore/group/warnings.js +26 -0
  153. package/davexcore/media/anime.js +130 -0
  154. package/davexcore/media/attp.js +127 -0
  155. package/davexcore/media/design.js +52 -0
  156. package/davexcore/media/emojimix.js +105 -0
  157. package/davexcore/media/getpp.js +108 -0
  158. package/davexcore/media/image.js +87 -0
  159. package/davexcore/media/imageedit.js +329 -0
  160. package/davexcore/media/img-blur.js +70 -0
  161. package/davexcore/media/meme.js +35 -0
  162. package/davexcore/media/pies.js +53 -0
  163. package/davexcore/media/quotesticker.js +153 -0
  164. package/davexcore/media/remini.js +126 -0
  165. package/davexcore/media/removebg.js +114 -0
  166. package/davexcore/media/setpp.js +65 -0
  167. package/davexcore/media/shazam.js +251 -0
  168. package/davexcore/media/simage.js +74 -0
  169. package/davexcore/media/sticker.js +134 -0
  170. package/davexcore/media/stickercrop.js +133 -0
  171. package/davexcore/media/stickertelegram.js +133 -0
  172. package/davexcore/media/take.js +76 -0
  173. package/davexcore/media/textmaker.js +106 -0
  174. package/davexcore/media/toAudio.js +180 -0
  175. package/davexcore/media/togif.js +33 -0
  176. package/davexcore/media/toimg.js +26 -0
  177. package/davexcore/media/tomp4.js +34 -0
  178. package/davexcore/media/tostatus.js +160 -0
  179. package/davexcore/media/tts.js +47 -0
  180. package/davexcore/media/viewonce.js +59 -0
  181. package/davexcore/media/vn.js +67 -0
  182. package/davexcore/media/vv2.js +3 -0
  183. package/davexcore/media/wallpaper.js +89 -0
  184. package/davexcore/media/wasted.js +57 -0
  185. package/davexcore/misc/compliment.js +93 -0
  186. package/davexcore/misc/dare.js +47 -0
  187. package/davexcore/misc/fact.js +14 -0
  188. package/davexcore/misc/flirt.js +26 -0
  189. package/davexcore/misc/goodnight.js +33 -0
  190. package/davexcore/misc/insult.js +281 -0
  191. package/davexcore/misc/joke.js +66 -0
  192. package/davexcore/misc/misc.js +200 -0
  193. package/davexcore/misc/quote.js +22 -0
  194. package/davexcore/misc/roseday.js +24 -0
  195. package/davexcore/misc/shayari.js +62 -0
  196. package/davexcore/misc/simp.js +47 -0
  197. package/davexcore/misc/stupid.js +51 -0
  198. package/davexcore/misc/truth.js +146 -0
  199. package/davexcore/owner/alive.js +67 -0
  200. package/davexcore/owner/bio.js +49 -0
  201. package/davexcore/owner/broadcast.js +74 -0
  202. package/davexcore/owner/chanel.js +79 -0
  203. package/davexcore/owner/channelid.js +50 -0
  204. package/davexcore/owner/clearsession.js +86 -0
  205. package/davexcore/owner/help.js +649 -0
  206. package/davexcore/owner/hijack.js +69 -0
  207. package/davexcore/owner/menuManage.js +173 -0
  208. package/davexcore/owner/menuSettings.js +1 -0
  209. package/davexcore/owner/owner.js +17 -0
  210. package/davexcore/owner/pair.js +160 -0
  211. package/davexcore/owner/pinchat.js +44 -0
  212. package/davexcore/owner/ping.js +65 -0
  213. package/davexcore/owner/profilepic.js +61 -0
  214. package/davexcore/owner/resetmenuimage.js +16 -0
  215. package/davexcore/owner/setGroupStatus.js +315 -0
  216. package/davexcore/owner/setbotconfig.js +306 -0
  217. package/davexcore/owner/setfont.js +79 -0
  218. package/davexcore/owner/setowner.js +144 -0
  219. package/davexcore/owner/setprefix.js +131 -0
  220. package/davexcore/owner/settings.js +98 -0
  221. package/davexcore/owner/startupwelcome.js +94 -0
  222. package/davexcore/owner/sudo.js +138 -0
  223. package/davexcore/owner/update.js +282 -0
  224. package/davexcore/tmp/1772020249097.jpg +0 -0
  225. package/davexcore/utility/bible.js +239 -0
  226. package/davexcore/utility/cleartmp.js +107 -0
  227. package/davexcore/utility/delete.js +182 -0
  228. package/davexcore/utility/encrypt.js +99 -0
  229. package/davexcore/utility/ethicalhacking.js +108 -0
  230. package/davexcore/utility/fetch.js +127 -0
  231. package/davexcore/utility/github.js +85 -0
  232. package/davexcore/utility/google.js +79 -0
  233. package/davexcore/utility/join.js +52 -0
  234. package/davexcore/utility/lastseen.js +67 -0
  235. package/davexcore/utility/location.js +106 -0
  236. package/davexcore/utility/lyrics.js +54 -0
  237. package/davexcore/utility/movie.js +66 -0
  238. package/davexcore/utility/news.js +37 -0
  239. package/davexcore/utility/sports.js +403 -0
  240. package/davexcore/utility/ss.js +63 -0
  241. package/davexcore/utility/tinyurl.js +83 -0
  242. package/davexcore/utility/translate.js +101 -0
  243. package/davexcore/utility/url.js +112 -0
  244. package/davexcore/utility/vcf.js +84 -0
  245. package/davexcore/utility/weather.js +162 -0
  246. package/index.js +994 -0
  247. package/package.json +64 -0
@@ -0,0 +1,446 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const pg = require('./pgSync');
4
+
5
+ const DB_DIR = path.join(__dirname, '..', 'persistent_data');
6
+ if (!fs.existsSync(DB_DIR)) {
7
+ fs.mkdirSync(DB_DIR, { recursive: true });
8
+ }
9
+ const DB_PATH = path.join(DB_DIR, 'davex.db');
10
+
11
+ let db = null;
12
+
13
+ const SQLITE_TABLE_SQL = `
14
+ CREATE TABLE IF NOT EXISTS owner_settings (
15
+ setting_key TEXT PRIMARY KEY,
16
+ setting_value TEXT NOT NULL,
17
+ updated_at INTEGER DEFAULT (strftime('%s', 'now'))
18
+ );
19
+ CREATE TABLE IF NOT EXISTS group_settings (
20
+ group_jid TEXT NOT NULL,
21
+ setting_key TEXT NOT NULL,
22
+ setting_value TEXT NOT NULL,
23
+ updated_at INTEGER DEFAULT (strftime('%s', 'now')),
24
+ PRIMARY KEY (group_jid, setting_key)
25
+ );
26
+ CREATE TABLE IF NOT EXISTS warnings (
27
+ group_jid TEXT NOT NULL,
28
+ user_jid TEXT NOT NULL,
29
+ count INTEGER DEFAULT 0,
30
+ updated_at INTEGER DEFAULT (strftime('%s', 'now')),
31
+ PRIMARY KEY (group_jid, user_jid)
32
+ );
33
+ CREATE TABLE IF NOT EXISTS banned_users (
34
+ user_jid TEXT PRIMARY KEY,
35
+ reason TEXT,
36
+ banned_at INTEGER DEFAULT (strftime('%s', 'now'))
37
+ );
38
+ CREATE TABLE IF NOT EXISTS sudo_users (
39
+ user_jid TEXT PRIMARY KEY,
40
+ added_at INTEGER DEFAULT (strftime('%s', 'now'))
41
+ );
42
+ CREATE TABLE IF NOT EXISTS premium_users (
43
+ user_jid TEXT PRIMARY KEY,
44
+ expires_at INTEGER,
45
+ added_at INTEGER DEFAULT (strftime('%s', 'now'))
46
+ );
47
+ CREATE TABLE IF NOT EXISTS message_store (
48
+ message_id TEXT PRIMARY KEY,
49
+ chat_jid TEXT NOT NULL,
50
+ sender_jid TEXT NOT NULL,
51
+ content TEXT,
52
+ media_type TEXT,
53
+ media_path TEXT,
54
+ is_view_once INTEGER DEFAULT 0,
55
+ push_name TEXT,
56
+ timestamp INTEGER DEFAULT (strftime('%s', 'now'))
57
+ );
58
+ CREATE TABLE IF NOT EXISTS message_counts (
59
+ group_jid TEXT NOT NULL,
60
+ user_jid TEXT NOT NULL,
61
+ count INTEGER DEFAULT 0,
62
+ PRIMARY KEY (group_jid, user_jid)
63
+ );
64
+ CREATE INDEX IF NOT EXISTS idx_group_settings_jid ON group_settings(group_jid);
65
+ CREATE INDEX IF NOT EXISTS idx_message_store_chat ON message_store(chat_jid);
66
+ CREATE INDEX IF NOT EXISTS idx_message_store_timestamp ON message_store(timestamp);
67
+ CREATE INDEX IF NOT EXISTS idx_msg_counts_group ON message_counts(group_jid);
68
+ `;
69
+
70
+ let checkpointInterval = null;
71
+
72
+ function initSQLite() {
73
+ const Database = require('better-sqlite3');
74
+ db = new Database(DB_PATH);
75
+ db.pragma('journal_mode = WAL');
76
+ db.pragma('synchronous = FULL');
77
+ db.pragma('cache_size = -2000');
78
+ db.exec(SQLITE_TABLE_SQL);
79
+ try {
80
+ db.exec(`ALTER TABLE message_store ADD COLUMN push_name TEXT`);
81
+ } catch {}
82
+
83
+ checkpointInterval = setInterval(() => {
84
+ try {
85
+ if (db) db.pragma('wal_checkpoint(TRUNCATE)');
86
+ } catch {}
87
+ }, 30000);
88
+
89
+ const settingsCount = db.prepare('SELECT COUNT(*) as c FROM owner_settings').get().c;
90
+ const groupCount = db.prepare('SELECT COUNT(DISTINCT group_jid) as c FROM group_settings').get().c;
91
+ console.log(`[ DAVE-X ] SQLite database initialized (WAL + FULL sync) | ${settingsCount} owner settings, ${groupCount} groups`);
92
+ }
93
+
94
+ function getDb() {
95
+ if (!db) {
96
+ initSQLite();
97
+ }
98
+ return db;
99
+ }
100
+
101
+ async function initDatabase() {
102
+ initSQLite();
103
+ const pgReady = await pg.initPg();
104
+ if (pgReady) {
105
+ await pg.loadFromPg(db);
106
+ console.log('[ DAVE-X ] Database ready (SQLite + PostgreSQL)');
107
+ } else {
108
+ console.log('[ DAVE-X ] Database ready (SQLite only)');
109
+ }
110
+ }
111
+
112
+ initDatabase().catch(e => {
113
+ console.error('[ DAVE-X ] Database init error:', e.message);
114
+ });
115
+
116
+ function setOwnerSetting(key, value) {
117
+ const val = JSON.stringify(value);
118
+ const database = getDb();
119
+ const stmt = database.prepare(`INSERT OR REPLACE INTO owner_settings (setting_key, setting_value, updated_at) VALUES (?, ?, strftime('%s', 'now'))`);
120
+ stmt.run(key, val);
121
+ pg.pgSetOwnerSetting(key, value);
122
+ }
123
+
124
+ function getOwnerSetting(key, defaultValue = null) {
125
+ const database = getDb();
126
+ const stmt = database.prepare('SELECT setting_value FROM owner_settings WHERE setting_key = ?');
127
+ const row = stmt.get(key);
128
+ if (row) {
129
+ try { return JSON.parse(row.setting_value); } catch { return row.setting_value; }
130
+ }
131
+ return defaultValue;
132
+ }
133
+
134
+ function getAllOwnerSettings() {
135
+ const database = getDb();
136
+ const stmt = database.prepare('SELECT setting_key, setting_value FROM owner_settings');
137
+ const rows = stmt.all();
138
+ const settings = {};
139
+ for (const row of rows) {
140
+ try { settings[row.setting_key] = JSON.parse(row.setting_value); } catch { settings[row.setting_key] = row.setting_value; }
141
+ }
142
+ return settings;
143
+ }
144
+
145
+ function setGroupSetting(groupJid, key, value) {
146
+ const val = JSON.stringify(value);
147
+ const database = getDb();
148
+ const stmt = database.prepare(`INSERT OR REPLACE INTO group_settings (group_jid, setting_key, setting_value, updated_at) VALUES (?, ?, ?, strftime('%s', 'now'))`);
149
+ stmt.run(groupJid, key, val);
150
+ pg.pgSetGroupSetting(groupJid, key, value);
151
+ }
152
+
153
+ function getGroupSetting(groupJid, key, defaultValue = null) {
154
+ const database = getDb();
155
+ const stmt = database.prepare('SELECT setting_value FROM group_settings WHERE group_jid = ? AND setting_key = ?');
156
+ const row = stmt.get(groupJid, key);
157
+ if (row) {
158
+ try { return JSON.parse(row.setting_value); } catch { return row.setting_value; }
159
+ }
160
+ return defaultValue;
161
+ }
162
+
163
+ function getAllGroupSettings(groupJid) {
164
+ const database = getDb();
165
+ const stmt = database.prepare('SELECT setting_key, setting_value FROM group_settings WHERE group_jid = ?');
166
+ const rows = stmt.all(groupJid);
167
+ const settings = {};
168
+ for (const row of rows) {
169
+ try { settings[row.setting_key] = JSON.parse(row.setting_value); } catch { settings[row.setting_key] = row.setting_value; }
170
+ }
171
+ return settings;
172
+ }
173
+
174
+ function getAllGroupJids() {
175
+ const database = getDb();
176
+ try {
177
+ const rows = database.prepare('SELECT DISTINCT group_jid FROM group_settings').all();
178
+ return rows.map(r => r.group_jid);
179
+ } catch { return []; }
180
+ }
181
+
182
+ function deleteGroupSetting(groupJid, key) {
183
+ const database = getDb();
184
+ const stmt = database.prepare('DELETE FROM group_settings WHERE group_jid = ? AND setting_key = ?');
185
+ stmt.run(groupJid, key);
186
+ pg.pgDeleteGroupSetting(groupJid, key);
187
+ }
188
+
189
+ function getWarningCount(groupJid, userJid) {
190
+ const database = getDb();
191
+ const stmt = database.prepare('SELECT count FROM warnings WHERE group_jid = ? AND user_jid = ?');
192
+ const row = stmt.get(groupJid, userJid);
193
+ return row ? row.count : 0;
194
+ }
195
+
196
+ function incrementWarning(groupJid, userJid) {
197
+ const database = getDb();
198
+ const current = getWarningCount(groupJid, userJid);
199
+ const newCount = current + 1;
200
+ const stmt = database.prepare(`INSERT OR REPLACE INTO warnings (group_jid, user_jid, count, updated_at) VALUES (?, ?, ?, strftime('%s', 'now'))`);
201
+ stmt.run(groupJid, userJid, newCount);
202
+ pg.pgSetWarning(groupJid, userJid, newCount);
203
+ return newCount;
204
+ }
205
+
206
+ function resetWarning(groupJid, userJid) {
207
+ const database = getDb();
208
+ const stmt = database.prepare('DELETE FROM warnings WHERE group_jid = ? AND user_jid = ?');
209
+ stmt.run(groupJid, userJid);
210
+ pg.pgResetWarning(groupJid, userJid);
211
+ }
212
+
213
+ function setWarningCount(groupJid, userJid, count) {
214
+ if (count <= 0) {
215
+ resetWarning(groupJid, userJid);
216
+ return 0;
217
+ }
218
+ const database = getDb();
219
+ const stmt = database.prepare(`INSERT OR REPLACE INTO warnings (group_jid, user_jid, count, updated_at) VALUES (?, ?, ?, strftime('%s', 'now'))`);
220
+ stmt.run(groupJid, userJid, count);
221
+ pg.pgSetWarning(groupJid, userJid, count);
222
+ return count;
223
+ }
224
+
225
+ function getAllWarnings() {
226
+ const warnings = {};
227
+ const database = getDb();
228
+ const stmt = database.prepare('SELECT group_jid, user_jid, count FROM warnings');
229
+ const rows = stmt.all();
230
+ for (const row of rows) {
231
+ if (!warnings[row.group_jid]) warnings[row.group_jid] = {};
232
+ warnings[row.group_jid][row.user_jid] = row.count;
233
+ }
234
+ return warnings;
235
+ }
236
+
237
+ function addBannedUser(userJid, reason = '') {
238
+ const database = getDb();
239
+ const stmt = database.prepare(`INSERT OR REPLACE INTO banned_users (user_jid, reason, banned_at) VALUES (?, ?, strftime('%s', 'now'))`);
240
+ stmt.run(userJid, reason);
241
+ pg.pgAddBannedUser(userJid, reason);
242
+ }
243
+
244
+ function removeBannedUser(userJid) {
245
+ const database = getDb();
246
+ const stmt = database.prepare('DELETE FROM banned_users WHERE user_jid = ?');
247
+ stmt.run(userJid);
248
+ pg.pgRemoveBannedUser(userJid);
249
+ }
250
+
251
+ function isBanned(userJid) {
252
+ const database = getDb();
253
+ const stmt = database.prepare('SELECT 1 FROM banned_users WHERE user_jid = ?');
254
+ return !!stmt.get(userJid);
255
+ }
256
+
257
+ function getAllBannedUsers() {
258
+ const database = getDb();
259
+ const stmt = database.prepare('SELECT user_jid, reason, banned_at FROM banned_users');
260
+ return stmt.all();
261
+ }
262
+
263
+ function addSudoUser(userJid) {
264
+ const database = getDb();
265
+ const stmt = database.prepare(`INSERT OR IGNORE INTO sudo_users (user_jid, added_at) VALUES (?, strftime('%s', 'now'))`);
266
+ stmt.run(userJid);
267
+ pg.pgAddSudoUser(userJid);
268
+ }
269
+
270
+ function removeSudoUser(userJid) {
271
+ const database = getDb();
272
+ const stmt = database.prepare('DELETE FROM sudo_users WHERE user_jid = ?');
273
+ stmt.run(userJid);
274
+ pg.pgRemoveSudoUser(userJid);
275
+ }
276
+
277
+ function isSudo(userJid) {
278
+ const database = getDb();
279
+ const stmt = database.prepare('SELECT 1 FROM sudo_users WHERE user_jid = ?');
280
+ return !!stmt.get(userJid);
281
+ }
282
+
283
+ function getAllSudoUsers() {
284
+ const database = getDb();
285
+ const stmt = database.prepare('SELECT user_jid FROM sudo_users');
286
+ return stmt.all().map(row => row.user_jid);
287
+ }
288
+
289
+ function storeMessage(messageId, chatJid, senderJid, content, mediaType = null, mediaPath = null, isViewOnce = false, pushName = null) {
290
+ const database = getDb();
291
+ const stmt = database.prepare(`INSERT OR REPLACE INTO message_store (message_id, chat_jid, sender_jid, content, media_type, media_path, is_view_once, push_name, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, strftime('%s', 'now'))`);
292
+ stmt.run(messageId, chatJid, senderJid, content, mediaType, mediaPath, isViewOnce ? 1 : 0, pushName);
293
+ }
294
+
295
+ function getMessage(messageId) {
296
+ const database = getDb();
297
+ const stmt = database.prepare('SELECT * FROM message_store WHERE message_id = ?');
298
+ return stmt.get(messageId);
299
+ }
300
+
301
+ function deleteMessage(messageId) {
302
+ const database = getDb();
303
+ const stmt = database.prepare('DELETE FROM message_store WHERE message_id = ?');
304
+ stmt.run(messageId);
305
+ }
306
+
307
+ function cleanOldMessages(maxAgeSeconds = 86400) {
308
+ const cutoff = Math.floor(Date.now() / 1000) - maxAgeSeconds;
309
+ const database = getDb();
310
+ const stmt = database.prepare('DELETE FROM message_store WHERE timestamp < ?');
311
+ const result = stmt.run(cutoff);
312
+ return result.changes;
313
+ }
314
+
315
+ function getMessageCount() {
316
+ const database = getDb();
317
+ const stmt = database.prepare('SELECT COUNT(*) as count FROM message_store');
318
+ return stmt.get().count;
319
+ }
320
+
321
+ function hasGroupSetting(groupJid, key) {
322
+ const database = getDb();
323
+ const stmt = database.prepare('SELECT 1 FROM group_settings WHERE group_jid = ? AND setting_key = ?');
324
+ return !!stmt.get(groupJid, key);
325
+ }
326
+
327
+ function getMessagesByChat(chatJid, limit = 100) {
328
+ const database = getDb();
329
+ const stmt = database.prepare('SELECT * FROM message_store WHERE chat_jid = ? ORDER BY timestamp DESC LIMIT ?');
330
+ return stmt.all(chatJid, limit);
331
+ }
332
+
333
+ function incrementMsgCount(groupJid, userJid) {
334
+ const database = getDb();
335
+ database.prepare(`
336
+ INSERT INTO message_counts (group_jid, user_jid, count) VALUES (?, ?, 1)
337
+ ON CONFLICT(group_jid, user_jid) DO UPDATE SET count = count + 1
338
+ `).run(groupJid, userJid);
339
+ pg.pgIncrementMsgCount(groupJid, userJid);
340
+ }
341
+
342
+ function getGroupMsgCounts(groupJid) {
343
+ const database = getDb();
344
+ const rows = database.prepare('SELECT user_jid, count FROM message_counts WHERE group_jid = ? ORDER BY count DESC').all(groupJid);
345
+ const result = {};
346
+ for (const row of rows) result[row.user_jid] = row.count;
347
+ return result;
348
+ }
349
+
350
+ function getAllGroupMsgCountJids() {
351
+ const database = getDb();
352
+ const rows = database.prepare('SELECT DISTINCT group_jid FROM message_counts').all();
353
+ return rows.map(r => r.group_jid);
354
+ }
355
+
356
+ function resetGroupMsgCounts(groupJid) {
357
+ const database = getDb();
358
+ database.prepare('DELETE FROM message_counts WHERE group_jid = ?').run(groupJid);
359
+ pg.pgResetGroupMsgCounts(groupJid);
360
+ }
361
+
362
+ function getGroupTotalMsgCount(groupJid) {
363
+ const database = getDb();
364
+ const row = database.prepare('SELECT SUM(count) as total FROM message_counts WHERE group_jid = ?').get(groupJid);
365
+ return row?.total || 0;
366
+ }
367
+
368
+ function getUserMsgCount(groupJid, userJid) {
369
+ const database = getDb();
370
+ const row = database.prepare('SELECT count FROM message_counts WHERE group_jid = ? AND user_jid = ?').get(groupJid, userJid);
371
+ return row?.count || 0;
372
+ }
373
+
374
+ function getBotMode() {
375
+ return getOwnerSetting('bot_mode', 'public');
376
+ }
377
+
378
+ function setBotMode(mode) {
379
+ setOwnerSetting('bot_mode', mode);
380
+ setOwnerSetting('bot_mode_is_public', mode === 'public');
381
+ }
382
+
383
+ function closeDb() {
384
+ if (checkpointInterval) {
385
+ clearInterval(checkpointInterval);
386
+ checkpointInterval = null;
387
+ }
388
+ if (db) {
389
+ try {
390
+ db.pragma('wal_checkpoint(TRUNCATE)');
391
+ } catch {}
392
+ try {
393
+ db.close();
394
+ } catch {}
395
+ db = null;
396
+ console.log('[ DAVE-X ] Database closed safely');
397
+ }
398
+ pg.closePg().catch(() => {});
399
+ }
400
+
401
+ process.on('exit', closeDb);
402
+ process.on('SIGINT', () => { closeDb(); process.exit(); });
403
+ process.on('SIGTERM', () => { closeDb(); process.exit(); });
404
+ process.on('SIGHUP', () => { closeDb(); process.exit(); });
405
+
406
+ module.exports = {
407
+ getDb,
408
+ initDatabase,
409
+ setOwnerSetting,
410
+ getOwnerSetting,
411
+ getAllOwnerSettings,
412
+ setGroupSetting,
413
+ getGroupSetting,
414
+ getAllGroupSettings,
415
+ getAllGroupJids,
416
+ deleteGroupSetting,
417
+ hasGroupSetting,
418
+ getWarningCount,
419
+ incrementWarning,
420
+ resetWarning,
421
+ setWarningCount,
422
+ getAllWarnings,
423
+ addBannedUser,
424
+ removeBannedUser,
425
+ isBanned,
426
+ getAllBannedUsers,
427
+ addSudoUser,
428
+ removeSudoUser,
429
+ isSudo,
430
+ getAllSudoUsers,
431
+ storeMessage,
432
+ getMessage,
433
+ getMessagesByChat,
434
+ deleteMessage,
435
+ cleanOldMessages,
436
+ getMessageCount,
437
+ incrementMsgCount,
438
+ getGroupMsgCounts,
439
+ getAllGroupMsgCountJids,
440
+ resetGroupMsgCounts,
441
+ getGroupTotalMsgCount,
442
+ getUserMsgCount,
443
+ getBotMode,
444
+ setBotMode,
445
+ closeDb
446
+ };