dbfunctions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from wbb import db
  21. from typing import Dict, List, Union
  22. notesdb = db.notes
  23. filtersdb = db.filters
  24. warnsdb = db.warns
  25. karmadb = db.karma
  26. chatsdb = db.chats
  27. gbansdb = db.gban
  28. coupledb = db.couple
  29. captchadb = db.captcha
  30. antiservicedb = db.antiservice
  31. pmpermitdb = db.pmpermit
  32. welcomedb = db.welcome_text
  33. """ Notes functions """
  34. async def get_notes_count() -> dict:
  35. chats = notesdb.find({"chat_id": {"$lt": 0}})
  36. if not chats:
  37. return {}
  38. chats_count = 0
  39. notes_count = 0
  40. for chat in await chats.to_list(length=1000000000):
  41. notes_name = await get_note_names(chat['chat_id'])
  42. notes_count += len(notes_name)
  43. chats_count += 1
  44. return {
  45. "chats_count": chats_count,
  46. "notes_count": notes_count
  47. }
  48. async def _get_notes(chat_id: int) -> Dict[str, int]:
  49. _notes = await notesdb.find_one({"chat_id": chat_id})
  50. if _notes:
  51. _notes = _notes["notes"]
  52. else:
  53. _notes = {}
  54. return _notes
  55. async def get_note_names(chat_id: int) -> List[str]:
  56. _notes = []
  57. for note in await _get_notes(chat_id):
  58. _notes.append(note)
  59. return _notes
  60. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  61. name = name.lower().strip()
  62. _notes = await _get_notes(chat_id)
  63. if name in _notes:
  64. return _notes[name]
  65. else:
  66. return False
  67. async def save_note(chat_id: int, name: str, note: dict):
  68. name = name.lower().strip()
  69. _notes = await _get_notes(chat_id)
  70. _notes[name] = note
  71. await notesdb.update_one(
  72. {"chat_id": chat_id},
  73. {
  74. "$set": {
  75. "notes": _notes
  76. }
  77. },
  78. upsert=True
  79. )
  80. async def delete_note(chat_id: int, name: str) -> bool:
  81. notesd = await _get_notes(chat_id)
  82. name = name.lower().strip()
  83. if name in notesd:
  84. del notesd[name]
  85. await notesdb.update_one(
  86. {"chat_id": chat_id},
  87. {
  88. "$set": {
  89. "notes": notesd
  90. }
  91. },
  92. upsert=True
  93. )
  94. return True
  95. return False
  96. """ Filters funcions """
  97. async def get_filters_count() -> dict:
  98. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  99. if not chats:
  100. return {}
  101. chats_count = 0
  102. filters_count = 0
  103. for chat in await chats.to_list(length=1000000000):
  104. filters_name = await get_filters_names(chat['chat_id'])
  105. filters_count += len(filters_name)
  106. chats_count += 1
  107. return {
  108. "chats_count": chats_count,
  109. "filters_count": filters_count
  110. }
  111. async def _get_filters(chat_id: int) -> Dict[str, int]:
  112. _filters = await filtersdb.find_one({"chat_id": chat_id})
  113. if _filters:
  114. _filters = _filters['filters']
  115. else:
  116. _filters = {}
  117. return _filters
  118. async def get_filters_names(chat_id: int) -> List[str]:
  119. _filters = []
  120. for _filter in await _get_filters(chat_id):
  121. _filters.append(_filter)
  122. return _filters
  123. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  124. name = name.lower().strip()
  125. _filters = await _get_filters(chat_id)
  126. if name in _filters:
  127. return _filters[name]
  128. else:
  129. return False
  130. async def save_filter(chat_id: int, name: str, _filter: dict):
  131. name = name.lower().strip()
  132. _filters = await _get_filters(chat_id)
  133. _filters[name] = _filter
  134. await filtersdb.update_one(
  135. {"chat_id": chat_id},
  136. {
  137. "$set": {
  138. "filters": _filters
  139. }
  140. },
  141. upsert=True
  142. )
  143. async def delete_filter(chat_id: int, name: str) -> bool:
  144. filtersd = await _get_filters(chat_id)
  145. name = name.lower().strip()
  146. if name in filtersd:
  147. del filtersd[name]
  148. await filtersdb.update_one(
  149. {"chat_id": chat_id},
  150. {
  151. "$set": {
  152. "filters": filtersd
  153. }
  154. },
  155. upsert=True
  156. )
  157. return True
  158. return False
  159. """ Warn functions """
  160. async def int_to_alpha(user_id: int) -> str:
  161. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  162. text = ""
  163. user_id = str(user_id)
  164. for i in user_id:
  165. text += alphabet[int(i)]
  166. return text
  167. async def alpha_to_int(user_id_alphabet: str) -> int:
  168. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  169. user_id = ""
  170. for i in user_id_alphabet:
  171. index = alphabet.index(i)
  172. user_id += str(index)
  173. user_id = int(user_id)
  174. return user_id
  175. async def get_warns_count() -> dict:
  176. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  177. if not chats:
  178. return {}
  179. chats_count = 0
  180. warns_count = 0
  181. for chat in await chats.to_list(length=100000000):
  182. for user in chat['warns']:
  183. warns_count += chat['warns'][user]['warns']
  184. chats_count += 1
  185. return {
  186. "chats_count": chats_count,
  187. "warns_count": warns_count
  188. }
  189. async def get_warns(chat_id: int) -> Dict[str, int]:
  190. warns = await warnsdb.find_one({"chat_id": chat_id})
  191. if warns:
  192. warns = warns['warns']
  193. else:
  194. warns = {}
  195. return warns
  196. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  197. name = name.lower().strip()
  198. warns = await get_warns(chat_id)
  199. if name in warns:
  200. return warns[name]
  201. async def add_warn(chat_id: int, name: str, warn: dict):
  202. name = name.lower().strip()
  203. warns = await get_warns(chat_id)
  204. warns[name] = warn
  205. await warnsdb.update_one(
  206. {"chat_id": chat_id},
  207. {
  208. "$set": {
  209. "warns": warns
  210. }
  211. },
  212. upsert=True
  213. )
  214. async def remove_warns(chat_id: int, name: str) -> bool:
  215. warnsd = await get_warns(chat_id)
  216. name = name.lower().strip()
  217. if name in warnsd:
  218. del warnsd[name]
  219. await warnsdb.update_one(
  220. {"chat_id": chat_id},
  221. {
  222. "$set": {
  223. "warns": warnsd
  224. }
  225. },
  226. upsert=True
  227. )
  228. return True
  229. return False
  230. """ Karma functions """
  231. async def get_karmas_count() -> dict:
  232. chats = karmadb.find({"chat_id": {"$lt": 0}})
  233. if not chats:
  234. return {}
  235. chats_count = 0
  236. karmas_count = 0
  237. for chat in await chats.to_list(length=1000000):
  238. for i in chat['karma']:
  239. karmas_count += chat['karma'][i]['karma']
  240. chats_count += 1
  241. return {
  242. "chats_count": chats_count,
  243. "karmas_count": karmas_count
  244. }
  245. async def get_karmas(chat_id: int) -> Dict[str, int]:
  246. karma = await karmadb.find_one({"chat_id": chat_id})
  247. if karma:
  248. karma = karma['karma']
  249. else:
  250. karma = {}
  251. return karma
  252. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  253. name = name.lower().strip()
  254. karmas = await get_karmas(chat_id)
  255. if name in karmas:
  256. return karmas[name]
  257. async def update_karma(chat_id: int, name: str, karma: dict):
  258. name = name.lower().strip()
  259. karmas = await get_karmas(chat_id)
  260. karmas[name] = karma
  261. await karmadb.update_one(
  262. {"chat_id": chat_id},
  263. {
  264. "$set": {
  265. "karma": karmas
  266. }
  267. },
  268. upsert=True
  269. )
  270. """ Chats log functions """
  271. async def is_served_chat(chat_id: int) -> bool:
  272. chat = await chatsdb.find_one({"chat_id": chat_id})
  273. if not chat:
  274. return False
  275. return True
  276. async def get_served_chats() -> list:
  277. chats = chatsdb.find({"chat_id": {'$lt': 0}})
  278. if not chats:
  279. return []
  280. chats_list = []
  281. for chat in await chats.to_list(length=1000000000):
  282. chats_list.append(chat)
  283. return chats_list
  284. async def add_served_chat(chat_id: int):
  285. is_served = await is_served_chat(chat_id)
  286. if is_served:
  287. return
  288. return await chatsdb.insert_one({"chat_id": chat_id})
  289. async def remove_served_chat(chat_id: int):
  290. is_served = await is_served_chat(chat_id)
  291. if not is_served:
  292. return
  293. return await chatsdb.delete_one({"chat_id": chat_id})
  294. """ Gban functions """
  295. async def get_gbans_count() -> int:
  296. users = gbansdb.find({"user_id": {"$gt": 0}})
  297. users = await users.to_list(length=100000)
  298. return len(users)
  299. async def is_gbanned_user(user_id: int) -> bool:
  300. user = await gbansdb.find_one({"user_id": user_id})
  301. if not user:
  302. return False
  303. return True
  304. async def add_gban_user(user_id: int):
  305. is_gbanned = await is_gbanned_user(user_id)
  306. if is_gbanned:
  307. return
  308. return await gbansdb.insert_one({"user_id": user_id})
  309. async def remove_gban_user(user_id: int):
  310. is_gbanned = await is_gbanned_user(user_id)
  311. if not is_gbanned:
  312. return
  313. return await gbansdb.delete_one({"user_id": user_id})
  314. # Couple Chooser
  315. async def _get_lovers(chat_id: int):
  316. lovers = await coupledb.find_one({"chat_id": chat_id})
  317. if lovers:
  318. lovers = lovers["couple"]
  319. else:
  320. lovers = {}
  321. return lovers
  322. async def get_couple(chat_id: int, date: str):
  323. lovers = await _get_lovers(chat_id)
  324. if date in lovers:
  325. return lovers[date]
  326. else:
  327. return False
  328. async def save_couple(chat_id: int, date: str, couple: dict):
  329. lovers = await _get_lovers(chat_id)
  330. lovers[date] = couple
  331. await coupledb.update_one(
  332. {"chat_id": chat_id},
  333. {
  334. "$set": {
  335. "couple": lovers
  336. }
  337. },
  338. upsert=True
  339. )
  340. # Captcha
  341. async def is_captcha_on(chat_id: int) -> bool:
  342. chat = await captchadb.find_one({"chat_id": chat_id})
  343. if not chat:
  344. return True
  345. return False
  346. async def captcha_on(chat_id: int):
  347. is_captcha = await is_captcha_on(chat_id)
  348. if is_captcha:
  349. return
  350. return await captchadb.delete_one({"chat_id": chat_id})
  351. async def captcha_off(chat_id: int):
  352. is_captcha = await is_captcha_on(chat_id)
  353. if not is_captcha:
  354. return
  355. return await captchadb.insert_one({"chat_id": chat_id})
  356. """Anti Service System"""
  357. async def is_antiservice_on(chat_id: int) -> bool:
  358. chat = await antiservicedb.find_one({"chat_id": chat_id})
  359. if not chat:
  360. return True
  361. return False
  362. async def antiservice_on(chat_id: int):
  363. is_antiservice = await is_antiservice_on(chat_id)
  364. if is_antiservice:
  365. return
  366. return await antiservicedb.delete_one({"chat_id": chat_id})
  367. async def antiservice_off(chat_id: int):
  368. is_antiservice = await is_antiservice_on(chat_id)
  369. if not is_antiservice:
  370. return
  371. return await antiservicedb.insert_one({"chat_id": chat_id})
  372. """ PM PERMIT """
  373. async def is_pmpermit_approved(user_id: int) -> bool:
  374. user = await pmpermitdb.find_one({"user_id": user_id})
  375. if not user:
  376. return False
  377. return True
  378. async def approve_pmpermit(user_id: int):
  379. is_pmpermit = await is_pmpermit_approved(user_id)
  380. if is_pmpermit:
  381. return
  382. return await pmpermitdb.insert_one({"user_id": user_id})
  383. async def disapprove_pmpermit(user_id: int):
  384. is_pmpermit = await is_pmpermit_approved(user_id)
  385. if not is_pmpermit:
  386. return
  387. return await pmpermitdb.delete_one({"user_id": user_id})
  388. """ WELCOME FUNCTIONS """
  389. async def get_welcome(chat_id: int)-> str:
  390. text = await welcomedb.find_one({"chat_id": chat_id})
  391. return text['text']
  392. async def set_welcome(chat_id: int, text: str):
  393. return await welcomedb.update_one(
  394. {"chat_id": chat_id},
  395. {
  396. "$set": {
  397. "text": text
  398. }
  399. },
  400. upsert=True
  401. )
  402. async def del_welcome(chat_id: int):
  403. return await welcomedb.delete_one({"chat_id": chat_id})