dbfunctions.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. from wbb import db
  2. from typing import Dict, List, Union
  3. notesdb = db.notes
  4. filtersdb = db.filters
  5. warnsdb = db.warns
  6. karmadb = db.karma
  7. chatsdb = db.chats
  8. gbansdb = db.gban
  9. """ Notes functions """
  10. async def get_notes_count() -> dict:
  11. chats = notesdb.find({"chat_id": {"$lt": 0}})
  12. if not chats:
  13. return {}
  14. chats_count = 0
  15. notes_count = 0
  16. for chat in await chats.to_list(length=1000000000):
  17. notes_name = await get_note_names(chat['chat_id'])
  18. notes_count += len(notes_name)
  19. chats_count += 1
  20. return {
  21. "chats_count": chats_count,
  22. "notes_count": notes_count
  23. }
  24. async def _get_notes(chat_id: int) -> Dict[str, int]:
  25. _notes = await notesdb.find_one({"chat_id": chat_id})
  26. if _notes:
  27. _notes = _notes["notes"]
  28. else:
  29. _notes = {}
  30. return _notes
  31. async def get_note_names(chat_id: int) -> List[str]:
  32. _notes = []
  33. for note in await _get_notes(chat_id):
  34. _notes.append(note)
  35. return _notes
  36. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  37. name = name.lower().strip()
  38. _notes = await _get_notes(chat_id)
  39. if name in _notes:
  40. return _notes[name]
  41. else:
  42. return False
  43. async def save_note(chat_id: int, name: str, note: dict):
  44. name = name.lower().strip()
  45. _notes = await _get_notes(chat_id)
  46. _notes[name] = note
  47. await notesdb.update_one(
  48. {"chat_id": chat_id},
  49. {
  50. "$set": {
  51. "notes": _notes
  52. }
  53. },
  54. upsert=True
  55. )
  56. async def delete_note(chat_id: int, name: str) -> bool:
  57. notesd = await _get_notes(chat_id)
  58. name = name.lower().strip()
  59. if name in notesd:
  60. del notesd[name]
  61. await notesdb.update_one(
  62. {"chat_id": chat_id},
  63. {
  64. "$set": {
  65. "notes": notesd
  66. }
  67. },
  68. upsert=True
  69. )
  70. return True
  71. return False
  72. """ Filters funcions """
  73. async def get_filters_count() -> dict:
  74. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  75. if not chats:
  76. return {}
  77. chats_count = 0
  78. filters_count = 0
  79. for chat in await chats.to_list(length=1000000000):
  80. filters_name = await get_filters_names(chat['chat_id'])
  81. filters_count += len(filters_name)
  82. chats_count += 1
  83. return {
  84. "chats_count": chats_count,
  85. "filters_count": filters_count
  86. }
  87. async def _get_filters(chat_id: int) -> Dict[str, int]:
  88. _filters = await filtersdb.find_one({"chat_id": chat_id})
  89. if _filters:
  90. _filters = _filters['filters']
  91. else:
  92. _filters = {}
  93. return _filters
  94. async def get_filters_names(chat_id: int) -> List[str]:
  95. _filters = []
  96. for _filter in await _get_filters(chat_id):
  97. _filters.append(_filter)
  98. return _filters
  99. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  100. name = name.lower().strip()
  101. _filters = await _get_filters(chat_id)
  102. if name in _filters:
  103. return _filters[name]
  104. else:
  105. return False
  106. async def save_filter(chat_id: int, name: str, _filter: dict):
  107. name = name.lower().strip()
  108. _filters = await _get_filters(chat_id)
  109. _filters[name] = _filter
  110. await filtersdb.update_one(
  111. {"chat_id": chat_id},
  112. {
  113. "$set": {
  114. "filters": _filters
  115. }
  116. },
  117. upsert=True
  118. )
  119. async def delete_filter(chat_id: int, name: str) -> bool:
  120. filtersd = await _get_filters(chat_id)
  121. name = name.lower().strip()
  122. if name in filtersd:
  123. del filtersd[name]
  124. await filtersdb.update_one(
  125. {"chat_id": chat_id},
  126. {
  127. "$set": {
  128. "filters": filtersd
  129. }
  130. },
  131. upsert=True
  132. )
  133. return True
  134. return False
  135. """ Warn functions """
  136. async def int_to_alpha(user_id: int) -> str:
  137. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  138. text = ""
  139. user_id = str(user_id)
  140. for i in user_id:
  141. text += alphabet[int(i)]
  142. return text
  143. async def alpha_to_int(user_id_alphabet: str) -> int:
  144. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  145. user_id = ""
  146. for i in user_id_alphabet:
  147. index = alphabet.index(i)
  148. user_id += str(index)
  149. user_id = int(user_id)
  150. return user_id
  151. async def get_warns_count() -> dict:
  152. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  153. if not chats:
  154. return {}
  155. chats_count = 0
  156. warns_count = 0
  157. for chat in await chats.to_list(length=100000000):
  158. for user in chat['warns']:
  159. warns_count += chat['warns'][user]['warns']
  160. chats_count += 1
  161. return {
  162. "chats_count": chats_count,
  163. "warns_count": warns_count
  164. }
  165. async def get_warns(chat_id: int) -> Dict[str, int]:
  166. warns = await warnsdb.find_one({"chat_id": chat_id})
  167. if warns:
  168. warns = warns['warns']
  169. else:
  170. warns = {}
  171. return warns
  172. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  173. name = name.lower().strip()
  174. warns = await get_warns(chat_id)
  175. if name in warns:
  176. return warns[name]
  177. async def add_warn(chat_id: int, name: str, warn: dict):
  178. name = name.lower().strip()
  179. warns = await get_warns(chat_id)
  180. warns[name] = warn
  181. await warnsdb.update_one(
  182. {"chat_id": chat_id},
  183. {
  184. "$set": {
  185. "warns": warns
  186. }
  187. },
  188. upsert=True
  189. )
  190. async def remove_warns(chat_id: int, name: str) -> bool:
  191. warnsd = await get_warns(chat_id)
  192. name = name.lower().strip()
  193. if name in warnsd:
  194. del warnsd[name]
  195. await warnsdb.update_one(
  196. {"chat_id": chat_id},
  197. {
  198. "$set": {
  199. "warns": warnsd
  200. }
  201. },
  202. upsert=True
  203. )
  204. return True
  205. return False
  206. """ Karma functions """
  207. async def get_karmas_count() -> dict:
  208. chats = karmadb.find({"chat_id": {"$lt": 0}})
  209. if not chats:
  210. return {}
  211. chats_count = 0
  212. karmas_count = 0
  213. for chat in await chats.to_list(length=1000000):
  214. for i in chat['karma']:
  215. karmas_count += chat['karma'][i]['karma']
  216. chats_count += 1
  217. return {
  218. "chats_count": chats_count,
  219. "karmas_count": karmas_count
  220. }
  221. async def get_karmas(chat_id: int) -> Dict[str, int]:
  222. karma = await karmadb.find_one({"chat_id": chat_id})
  223. if karma:
  224. karma = karma['karma']
  225. else:
  226. karma = {}
  227. return karma
  228. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  229. name = name.lower().strip()
  230. karmas = await get_karmas(chat_id)
  231. if name in karmas:
  232. return karmas[name]
  233. async def update_karma(chat_id: int, name: str, karma: dict):
  234. name = name.lower().strip()
  235. karmas = await get_karmas(chat_id)
  236. karmas[name] = karma
  237. await karmadb.update_one(
  238. {"chat_id": chat_id},
  239. {
  240. "$set": {
  241. "karma": karmas
  242. }
  243. },
  244. upsert=True
  245. )
  246. """ Chats log functions """
  247. async def is_served_chat(chat_id: int) -> bool:
  248. chat = await chatsdb.find_one({"chat_id": chat_id})
  249. if not chat:
  250. return False
  251. return True
  252. async def get_served_chats() -> list:
  253. chats = chatsdb.find({"chat_id": {'$lt': 0}})
  254. if not chats:
  255. return []
  256. chats_list = []
  257. for chat in await chats.to_list(length=1000000000):
  258. chats_list.append(chat)
  259. return chats_list
  260. async def add_served_chat(chat_id: int):
  261. is_served = await is_served_chat(chat_id)
  262. if is_served:
  263. return
  264. return await chatsdb.insert_one({"chat_id": chat_id})
  265. async def remove_served_chat(chat_id: int):
  266. is_served = await is_served_chat(chat_id)
  267. if not is_served:
  268. return
  269. return await chatsdb.delete_one({"chat_id": chat_id})
  270. """ Gban functions """
  271. async def get_gbans_count() -> int:
  272. users = gbansdb.find({"user_id": {"$gt": 0}})
  273. users = await users.to_list(length=100000)
  274. return len(users)
  275. async def is_gbanned_user(user_id: int) -> bool:
  276. user = await gbansdb.find_one({"user_id": user_id})
  277. if not user:
  278. return False
  279. return True
  280. async def add_gban_user(user_id: int):
  281. is_gbanned = await is_gbanned_user(user_id)
  282. if is_gbanned:
  283. return
  284. return await gbansdb.insert_one({"user_id": user_id})
  285. async def remove_gban_user(user_id: int):
  286. is_gbanned = await is_gbanned_user(user_id)
  287. if not is_gbanned:
  288. return
  289. return await gbansdb.delete_one({"user_id": user_id})