dbfunctions.py 8.5 KB

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