dbfunctions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. """ Notes functions """
  33. async def get_notes_count() -> dict:
  34. chats = notesdb.find({"chat_id": {"$lt": 0}})
  35. if not chats:
  36. return {}
  37. chats_count = 0
  38. notes_count = 0
  39. for chat in await chats.to_list(length=1000000000):
  40. notes_name = await get_note_names(chat['chat_id'])
  41. notes_count += len(notes_name)
  42. chats_count += 1
  43. return {
  44. "chats_count": chats_count,
  45. "notes_count": notes_count
  46. }
  47. async def _get_notes(chat_id: int) -> Dict[str, int]:
  48. _notes = await notesdb.find_one({"chat_id": chat_id})
  49. if _notes:
  50. _notes = _notes["notes"]
  51. else:
  52. _notes = {}
  53. return _notes
  54. async def get_note_names(chat_id: int) -> List[str]:
  55. _notes = []
  56. for note in await _get_notes(chat_id):
  57. _notes.append(note)
  58. return _notes
  59. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  60. name = name.lower().strip()
  61. _notes = await _get_notes(chat_id)
  62. if name in _notes:
  63. return _notes[name]
  64. else:
  65. return False
  66. async def save_note(chat_id: int, name: str, note: dict):
  67. name = name.lower().strip()
  68. _notes = await _get_notes(chat_id)
  69. _notes[name] = note
  70. await notesdb.update_one(
  71. {"chat_id": chat_id},
  72. {
  73. "$set": {
  74. "notes": _notes
  75. }
  76. },
  77. upsert=True
  78. )
  79. async def delete_note(chat_id: int, name: str) -> bool:
  80. notesd = await _get_notes(chat_id)
  81. name = name.lower().strip()
  82. if name in notesd:
  83. del notesd[name]
  84. await notesdb.update_one(
  85. {"chat_id": chat_id},
  86. {
  87. "$set": {
  88. "notes": notesd
  89. }
  90. },
  91. upsert=True
  92. )
  93. return True
  94. return False
  95. """ Filters funcions """
  96. async def get_filters_count() -> dict:
  97. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  98. if not chats:
  99. return {}
  100. chats_count = 0
  101. filters_count = 0
  102. for chat in await chats.to_list(length=1000000000):
  103. filters_name = await get_filters_names(chat['chat_id'])
  104. filters_count += len(filters_name)
  105. chats_count += 1
  106. return {
  107. "chats_count": chats_count,
  108. "filters_count": filters_count
  109. }
  110. async def _get_filters(chat_id: int) -> Dict[str, int]:
  111. _filters = await filtersdb.find_one({"chat_id": chat_id})
  112. if _filters:
  113. _filters = _filters['filters']
  114. else:
  115. _filters = {}
  116. return _filters
  117. async def get_filters_names(chat_id: int) -> List[str]:
  118. _filters = []
  119. for _filter in await _get_filters(chat_id):
  120. _filters.append(_filter)
  121. return _filters
  122. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  123. name = name.lower().strip()
  124. _filters = await _get_filters(chat_id)
  125. if name in _filters:
  126. return _filters[name]
  127. else:
  128. return False
  129. async def save_filter(chat_id: int, name: str, _filter: dict):
  130. name = name.lower().strip()
  131. _filters = await _get_filters(chat_id)
  132. _filters[name] = _filter
  133. await filtersdb.update_one(
  134. {"chat_id": chat_id},
  135. {
  136. "$set": {
  137. "filters": _filters
  138. }
  139. },
  140. upsert=True
  141. )
  142. async def delete_filter(chat_id: int, name: str) -> bool:
  143. filtersd = await _get_filters(chat_id)
  144. name = name.lower().strip()
  145. if name in filtersd:
  146. del filtersd[name]
  147. await filtersdb.update_one(
  148. {"chat_id": chat_id},
  149. {
  150. "$set": {
  151. "filters": filtersd
  152. }
  153. },
  154. upsert=True
  155. )
  156. return True
  157. return False
  158. """ Warn functions """
  159. async def int_to_alpha(user_id: int) -> str:
  160. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  161. text = ""
  162. user_id = str(user_id)
  163. for i in user_id:
  164. text += alphabet[int(i)]
  165. return text
  166. async def alpha_to_int(user_id_alphabet: str) -> int:
  167. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  168. user_id = ""
  169. for i in user_id_alphabet:
  170. index = alphabet.index(i)
  171. user_id += str(index)
  172. user_id = int(user_id)
  173. return user_id
  174. async def get_warns_count() -> dict:
  175. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  176. if not chats:
  177. return {}
  178. chats_count = 0
  179. warns_count = 0
  180. for chat in await chats.to_list(length=100000000):
  181. for user in chat['warns']:
  182. warns_count += chat['warns'][user]['warns']
  183. chats_count += 1
  184. return {
  185. "chats_count": chats_count,
  186. "warns_count": warns_count
  187. }
  188. async def get_warns(chat_id: int) -> Dict[str, int]:
  189. warns = await warnsdb.find_one({"chat_id": chat_id})
  190. if warns:
  191. warns = warns['warns']
  192. else:
  193. warns = {}
  194. return warns
  195. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  196. name = name.lower().strip()
  197. warns = await get_warns(chat_id)
  198. if name in warns:
  199. return warns[name]
  200. async def add_warn(chat_id: int, name: str, warn: dict):
  201. name = name.lower().strip()
  202. warns = await get_warns(chat_id)
  203. warns[name] = warn
  204. await warnsdb.update_one(
  205. {"chat_id": chat_id},
  206. {
  207. "$set": {
  208. "warns": warns
  209. }
  210. },
  211. upsert=True
  212. )
  213. async def remove_warns(chat_id: int, name: str) -> bool:
  214. warnsd = await get_warns(chat_id)
  215. name = name.lower().strip()
  216. if name in warnsd:
  217. del warnsd[name]
  218. await warnsdb.update_one(
  219. {"chat_id": chat_id},
  220. {
  221. "$set": {
  222. "warns": warnsd
  223. }
  224. },
  225. upsert=True
  226. )
  227. return True
  228. return False
  229. """ Karma functions """
  230. async def get_karmas_count() -> dict:
  231. chats = karmadb.find({"chat_id": {"$lt": 0}})
  232. if not chats:
  233. return {}
  234. chats_count = 0
  235. karmas_count = 0
  236. for chat in await chats.to_list(length=1000000):
  237. for i in chat['karma']:
  238. karmas_count += chat['karma'][i]['karma']
  239. chats_count += 1
  240. return {
  241. "chats_count": chats_count,
  242. "karmas_count": karmas_count
  243. }
  244. async def get_karmas(chat_id: int) -> Dict[str, int]:
  245. karma = await karmadb.find_one({"chat_id": chat_id})
  246. if karma:
  247. karma = karma['karma']
  248. else:
  249. karma = {}
  250. return karma
  251. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  252. name = name.lower().strip()
  253. karmas = await get_karmas(chat_id)
  254. if name in karmas:
  255. return karmas[name]
  256. async def update_karma(chat_id: int, name: str, karma: dict):
  257. name = name.lower().strip()
  258. karmas = await get_karmas(chat_id)
  259. karmas[name] = karma
  260. await karmadb.update_one(
  261. {"chat_id": chat_id},
  262. {
  263. "$set": {
  264. "karma": karmas
  265. }
  266. },
  267. upsert=True
  268. )
  269. """ Chats log functions """
  270. async def is_served_chat(chat_id: int) -> bool:
  271. chat = await chatsdb.find_one({"chat_id": chat_id})
  272. if not chat:
  273. return False
  274. return True
  275. async def get_served_chats() -> list:
  276. chats = chatsdb.find({"chat_id": {'$lt': 0}})
  277. if not chats:
  278. return []
  279. chats_list = []
  280. for chat in await chats.to_list(length=1000000000):
  281. chats_list.append(chat)
  282. return chats_list
  283. async def add_served_chat(chat_id: int):
  284. is_served = await is_served_chat(chat_id)
  285. if is_served:
  286. return
  287. return await chatsdb.insert_one({"chat_id": chat_id})
  288. async def remove_served_chat(chat_id: int):
  289. is_served = await is_served_chat(chat_id)
  290. if not is_served:
  291. return
  292. return await chatsdb.delete_one({"chat_id": chat_id})
  293. """ Gban functions """
  294. async def get_gbans_count() -> int:
  295. users = gbansdb.find({"user_id": {"$gt": 0}})
  296. users = await users.to_list(length=100000)
  297. return len(users)
  298. async def is_gbanned_user(user_id: int) -> bool:
  299. user = await gbansdb.find_one({"user_id": user_id})
  300. if not user:
  301. return False
  302. return True
  303. async def add_gban_user(user_id: int):
  304. is_gbanned = await is_gbanned_user(user_id)
  305. if is_gbanned:
  306. return
  307. return await gbansdb.insert_one({"user_id": user_id})
  308. async def remove_gban_user(user_id: int):
  309. is_gbanned = await is_gbanned_user(user_id)
  310. if not is_gbanned:
  311. return
  312. return await gbansdb.delete_one({"user_id": user_id})
  313. # Couple Chooser
  314. async def _get_lovers(chat_id: int):
  315. lovers = await coupledb.find_one({"chat_id": chat_id})
  316. if lovers:
  317. lovers = lovers["couple"]
  318. else:
  319. lovers = {}
  320. return lovers
  321. async def get_couple(chat_id: int, date: str):
  322. lovers = await _get_lovers(chat_id)
  323. if date in lovers:
  324. return lovers[date]
  325. else:
  326. return False
  327. async def save_couple(chat_id: int, date: str, couple: dict):
  328. lovers = await _get_lovers(chat_id)
  329. lovers[date] = couple
  330. await coupledb.update_one(
  331. {"chat_id": chat_id},
  332. {
  333. "$set": {
  334. "couple": lovers
  335. }
  336. },
  337. upsert=True
  338. )
  339. # Captcha
  340. async def is_captcha_on(chat_id: int) -> bool:
  341. chat = await captchadb.find_one({"chat_id": chat_id})
  342. if not chat:
  343. return True
  344. return False
  345. async def captcha_on(chat_id: int):
  346. is_captcha = await is_captcha_on(chat_id)
  347. if is_captcha:
  348. return
  349. return await captchadb.delete_one({"chat_id": chat_id})
  350. async def captcha_off(chat_id: int):
  351. is_captcha = await is_captcha_on(chat_id)
  352. if not is_captcha:
  353. return
  354. return await captchadb.insert_one({"chat_id": chat_id})
  355. """Anti Service System"""
  356. async def is_antiservice_on(chat_id: int) -> bool:
  357. chat = await antiservicedb.find_one({"chat_id": chat_id})
  358. if not chat:
  359. return True
  360. return False
  361. async def antiservice_on(chat_id: int):
  362. is_antiservice = await is_antiservice_on(chat_id)
  363. if is_antiservice:
  364. return
  365. return await antiservicedb.delete_one({"chat_id": chat_id})
  366. async def antiservice_off(chat_id: int):
  367. is_antiservice = await is_antiservice_on(chat_id)
  368. if not is_antiservice:
  369. return
  370. return await antiservicedb.insert_one({"chat_id": chat_id})
  371. """ PM PERMIT """
  372. async def is_pmpermit_approved(user_id: int) -> bool:
  373. user = await pmpermitdb.find_one({"user_id": user_id})
  374. if not user:
  375. return False
  376. return True
  377. async def approve_pmpermit(user_id: int):
  378. is_pmpermit = await is_pmpermit_approved(user_id)
  379. if is_pmpermit:
  380. return
  381. return await pmpermitdb.insert_one({"user_id": user_id})
  382. async def disapprove_pmpermit(user_id: int):
  383. is_pmpermit = await is_pmpermit_approved(user_id)
  384. if not is_pmpermit:
  385. return
  386. return await pmpermitdb.delete_one({"user_id": user_id})