Spaces:
Running
Running
File size: 23,541 Bytes
85445bc 3cbcb98 85445bc 3cbcb98 85445bc 3cbcb98 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
import os
from os import getenv
from dotenv import load_dotenv
import datetime
import time
from motor import motor_asyncio
from motor.core import AgnosticClient
from logger import LOGS
load_dotenv()
MONGO_URL = os.environ["MONGO_URL"]
class Database:
def __init__(self, uri: str) -> None:
self.client: AgnosticClient = motor_asyncio.AsyncIOMotorClient(uri)
self.db = self.client["Akeno"]
self.afk = self.db["afk"]
self.antiflood = self.db["antiflood"]
self.autopost = self.db["autopost"]
self.blacklist = self.db["blacklist"]
self.echo = self.db["echo"]
self.env = self.db["env"]
self.filter = self.db["filter"]
self.forcesub = self.db["forcesub"]
self.gachabots = self.db["gachabots"]
self.cohere = self.db["cohere"]
self.chatbot = self.db["chatbot"]
self.backup_chatbot = self.db["backupchatbot"]
self.antiarabic = self.db["antiarabic"]
self.antinsfw = self.db["antinsfw"]
self.gban = self.db["gban"]
self.gmute = self.db["gmute"]
self.greetings = self.db["greetings"]
self.mute = self.db["mute"]
self.pmpermit = self.db["pmpermit"]
self.session = self.db["session"]
self.snips = self.db["snips"]
self.stan_users = self.db["stan_users"]
async def connect(self):
try:
await self.client.admin.command("ping")
LOGS.info(f"Database Connection Established!")
except Exception as e:
LOGS.info(f"DatabaseErr: {e} ")
quit(1)
async def _close(self):
await self.client.close()
def get_datetime(self) -> str:
return datetime.datetime.now().strftime("%d/%m/%Y - %H:%M")
async def set_env(self, name: str, value: str) -> None:
await self.env.update_one(
{"name": name}, {"$set": {"value": value}}, upsert=True
)
async def get_env(self, name: str) -> str | None:
if await self.is_env(name):
data = await self.env.find_one({"name": name})
return data["value"]
return None
async def rm_env(self, name: str) -> None:
await self.env.delete_one({"name": name})
async def is_env(self, name: str) -> bool:
if await self.env.find_one({"name": name}):
return True
return False
async def get_all_env(self) -> list:
return [i async for i in self.env.find({})]
async def is_stan(self, client: int, user_id: int) -> bool:
if await self.stan_users.find_one({"client": client, "user_id": user_id}):
return True
return False
async def add_stan(self, client: int, user_id: int) -> bool:
if await self.is_stan(client, user_id):
return False
await self.stan_users.insert_one(
{"client": client, "user_id": user_id, "date": self.get_datetime()}
)
return True
async def rm_stan(self, client: int, user_id: int) -> bool:
if not await self.is_stan(client, user_id):
return False
await self.stan_users.delete_one({"client": client, "user_id": user_id})
return True
async def get_stans(self, client: int) -> list:
return [i async for i in self.stan_users.find({"client": client})]
async def get_all_stans(self) -> list:
return [i async for i in self.stan_users.find({})]
async def is_session(self, user_id: int) -> bool:
if await self.session.find_one({"user_id": user_id}):
return True
return False
async def update_session(self, user_id: int, session: str) -> None:
await self.session.update_one(
{"user_id": user_id},
{"$set": {"session": session, "date": self.get_datetime()}},
upsert=True,
)
async def rm_session(self, user_id: int) -> None:
await self.session.delete_one({"user_id": user_id})
async def get_session(self, user_id: int):
if not await self.is_session(user_id):
return False
data = await self.session.find_one({"user_id": user_id})
return data
async def get_all_sessions(self) -> list:
return [i async for i in self.session.find({})]
async def is_gbanned(self, user_id: int) -> bool:
if await self.gban.find_one({"user_id": user_id}):
return True
return False
async def add_gban(self, user_id: int, reason: str) -> bool:
if await self.is_gbanned(user_id):
return False
await self.gban.insert_one(
{"user_id": user_id, "reason": reason, "date": self.get_datetime()}
)
return True
async def rm_gban(self, user_id: int):
if not await self.is_gbanned(user_id):
return None
reason = (await self.gban.find_one({"user_id": user_id}))["reason"]
await self.gban.delete_one({"user_id": user_id})
return reason
async def get_gban(self) -> list:
return [i async for i in self.gban.find({})]
async def get_gban_user(self, user_id: int) -> dict | None:
if not await self.is_gbanned(user_id):
return None
return await self.gban.find_one({"user_id": user_id})
async def is_gmuted(self, user_id: int) -> bool:
if await self.gmute.find_one({"user_id": user_id}):
return True
return False
async def add_gmute(self, user_id: int, reason: str) -> bool:
if await self.is_gmuted(user_id):
return False
await self.gmute.insert_one(
{"user_id": user_id, "reason": reason, "date": self.get_datetime()}
)
return True
async def rm_gmute(self, user_id: int):
if not await self.is_gmuted(user_id):
return None
reason = (await self.gmute.find_one({"user_id": user_id}))["reason"]
await self.gmute.delete_one({"user_id": user_id})
return reason
async def get_gmute(self) -> list:
return [i async for i in self.gmute.find({})]
async def add_mute(self, client: int, user_id: int, chat_id: int, reason: str):
await self.mute.update_one(
{"client": client, "user_id": user_id, "chat_id": chat_id},
{"$set": {"reason": reason, "date": self.get_datetime()}},
upsert=True,
)
async def rm_mute(self, client: int, user_id: int, chat_id: int) -> str:
reason = (await self.get_mute(client, user_id, chat_id))["reason"]
await self.mute.delete_one({"client": client, "user_id": user_id, "chat_id": chat_id})
return reason
async def is_muted(self, client: int, user_id: int, chat_id: int) -> bool:
if await self.get_mute(client, user_id, chat_id):
return True
return False
async def get_mute(self, client: int, user_id: int, chat_id: int):
data = await self.mute.find_one({"client": client, "user_id": user_id, "chat_id": chat_id})
return data
async def set_afk(
self, user_id: int, reason: str, media: int, media_type: str
) -> None:
await self.afk.update_one(
{"user_id": user_id},
{
"$set": {
"reason": reason,
"time": time.time(),
"media": media,
"media_type": media_type,
}
},
upsert=True,
)
async def get_afk(self, user_id: int):
data = await self.afk.find_one({"user_id": user_id})
return data
async def is_afk(self, user_id: int) -> bool:
if await self.afk.find_one({"user_id": user_id}):
return True
return False
async def rm_afk(self, user_id: int) -> None:
await self.afk.delete_one({"user_id": user_id})
async def set_flood(self, client_chat: tuple[int, int], settings: dict):
await self.antiflood.update_one(
{"client": client_chat[0], "chat": client_chat[1]},
{"$set": settings},
upsert=True,
)
async def get_flood(self, client_chat: tuple[int, int]):
data = await self.antiflood.find_one(
{"client": client_chat[0], "chat": client_chat[1]}
)
return data or {}
async def is_flood(self, client_chat: tuple[int, int]) -> bool:
data = await self.get_flood(client_chat)
if not data:
return False
if data["limit"] == 0:
return False
return True
async def get_all_floods(self) -> list:
return [i async for i in self.antiflood.find({})]
async def set_autopost(self, client: int, from_channel: int, to_channel: int):
await self.autopost.update_one(
{"client": client},
{
"$push": {
"autopost": {
"from_channel": from_channel,
"to_channel": to_channel,
"date": self.get_datetime(),
}
}
},
upsert=True,
)
async def get_autopost(self, client: int, from_channel: int):
data = await self.autopost.find_one(
{
"client": client,
"autopost": {"$elemMatch": {"from_channel": from_channel}},
}
)
return data
async def is_autopost(
self, client: int, from_channel: int, to_channel: int = None
) -> bool:
if to_channel:
data = await self.autopost.find_one(
{
"client": client,
"autopost": {
"$elemMatch": {
"from_channel": from_channel,
"to_channel": to_channel,
}
},
}
)
else:
data = await self.autopost.find_one(
{
"client": client,
"autopost": {"$elemMatch": {"from_channel": from_channel}},
}
)
return True if data else False
async def rm_autopost(self, client: int, from_channel: int, to_channel: int):
await self.autopost.update_one(
{"client": client},
{
"$pull": {
"autopost": {
"from_channel": from_channel,
"to_channel": to_channel,
}
}
},
)
async def get_all_autoposts(self, client: int) -> list:
return [i async for i in self.autopost.find({"client": client})]
async def add_blacklist(self, client: int, chat: int, blacklist: str):
await self.blacklist.update_one(
{"client": client, "chat": chat},
{"$push": {"blacklist": blacklist}},
upsert=True,
)
async def rm_blacklist(self, client: int, chat: int, blacklist: str):
await self.blacklist.update_one(
{"client": client, "chat": chat},
{"$pull": {"blacklist": blacklist}},
)
async def is_blacklist(self, client: int, chat: int, blacklist: str) -> bool:
blacklists = await self.get_all_blacklists(client, chat)
if blacklist in blacklists:
return True
return False
async def get_all_blacklists(self, client: int, chat: int) -> list:
data = await self.blacklist.find_one({"client": client, "chat": chat})
if not data:
return []
return data["blacklist"]
async def get_blacklist_clients(self) -> list:
return [i async for i in self.blacklist.find({})]
async def set_echo(self, client: int, chat: int, user: int):
await self.echo.update_one(
{"client": client, "chat": chat},
{"$push": {"echo": user}},
upsert=True,
)
async def rm_echo(self, client: int, chat: int, user: int):
await self.echo.update_one(
{"client": client, "chat": chat},
{"$pull": {"echo": user}},
)
async def is_echo(self, client: int, chat: int, user: int) -> bool:
data = await self.get_all_echo(client, chat)
if user in data:
return True
return False
async def get_all_echo(self, client: int, chat: int) -> list:
data = await self.echo.find_one({"client": client, "chat": chat})
if not data:
return []
return data["echo"]
async def set_filter(self, client: int, chat: int, keyword: str, msgid: int):
await self.filter.update_one(
{"client": client, "chat": chat},
{"$push": {"filter": {"keyword": keyword, "msgid": msgid}}},
upsert=True,
)
async def rm_filter(self, client: int, chat: int, keyword: str):
await self.filter.update_one(
{"client": client, "chat": chat},
{"$pull": {"filter": {"keyword": keyword}}},
)
async def rm_all_filters(self, client: int, chat: int):
await self.filter.delete_one({"client": client, "chat": chat})
async def is_filter(self, client: int, chat: int, keyword: str) -> bool:
data = await self.get_filter(client, chat, keyword)
return True if data else False
async def get_filter(self, client: int, chat: int, keyword: str):
data = await self.filter.find_one(
{
"client": client,
"chat": chat,
"filter": {"$elemMatch": {"keyword": keyword}},
}
)
return data
async def get_all_filters(self, client: int, chat: int) -> list:
data = await self.filter.find_one({"client": client, "chat": chat})
if not data:
return []
return data["filter"]
async def set_snip(self, client: int, chat: int, keyword: str, msgid: int):
await self.snips.update_one(
{"client": client, "chat": chat},
{"$push": {"snips": {"keyword": keyword, "msgid": msgid}}},
upsert=True,
)
async def rm_snip(self, client: int, chat: int, keyword: str):
await self.snips.update_one(
{"client": client, "chat": chat},
{"$pull": {"snips": {"keyword": keyword}}},
)
async def rm_all_snips(self, client: int, chat: int):
await self.snips.delete_one({"client": client, "chat": chat})
async def is_snip(self, client: int, chat: int, keyword: str) -> bool:
data = await self.get_snip(client, chat, keyword)
return True if data else False
async def get_snip(self, client: int, chat: int, keyword: str):
data = await self.snips.find_one(
{
"client": client,
"chat": chat,
"snips": {"$elemMatch": {"keyword": keyword}},
}
)
return data
async def get_all_snips(self, client: int, chat: int) -> list:
data = await self.snips.find_one({"client": client, "chat": chat})
if not data:
return []
return data["snips"]
async def add_pmpermit(self, client: int, user: int):
await self.pmpermit.update_one(
{"client": client, "user": user},
{"$set": {"date": self.get_datetime()}},
upsert=True,
)
async def rm_pmpermit(self, client: int, user: int):
await self.pmpermit.delete_one({"client": client, "user": user})
async def is_pmpermit(self, client: int, user: int) -> bool:
data = await self.get_pmpermit(client, user)
return True if data else False
async def get_pmpermit(self, client: int, user: int):
data = await self.pmpermit.find_one({"client": client, "user": user})
return data
async def get_all_pmpermits(self, client: int) -> list:
return [i async for i in self.pmpermit.find({"client": client})]
async def set_welcome(self, client: int, chat: int, message: int):
await self.greetings.update_one(
{"client": client, "chat": chat, "welcome": True},
{"$set": {"message": message}},
upsert=True,
)
async def rm_welcome(self, client: int, chat: int):
await self.greetings.delete_one(
{"client": client, "chat": chat, "welcome": True}
)
async def is_welcome(self, client: int, chat: int) -> bool:
data = await self.get_welcome(client, chat)
return True if data else False
async def get_welcome(self, client: int, chat: int):
data = await self.greetings.find_one(
{"client": client, "chat": chat, "welcome": True}
)
return data
async def set_goodbye(self, client: int, chat: int, message: int):
await self.greetings.update_one(
{"client": client, "chat": chat, "welcome": False},
{"$set": {"message": message}},
upsert=True,
)
async def rm_goodbye(self, client: int, chat: int):
await self.greetings.delete_one(
{"client": client, "chat": chat, "welcome": False}
)
async def is_goodbye(self, client: int, chat: int) -> bool:
data = await self.get_goodbye(client, chat)
return True if data else False
async def get_goodbye(self, client: int, chat: int):
data = await self.greetings.find_one(
{"client": client, "chat": chat, "welcome": False}
)
return data
async def get_all_greetings(self, client: int) -> list:
return [i async for i in self.greetings.find({"client": client})]
async def add_forcesub(self, chat: int, must_join: int):
await self.forcesub.update_one(
{"chat": chat},
{"$push": {"must_join": must_join}},
upsert=True,
)
async def rm_forcesub(self, chat: int, must_join: int) -> int:
await self.forcesub.update_one(
{"chat": chat},
{"$pull": {"must_join": must_join}},
)
data = await self.forcesub.find_one({"chat": chat})
return len(data["must_join"])
async def rm_all_forcesub(self, in_chat: int):
await self.forcesub.delete_one({"chat": in_chat})
async def is_forcesub(self, chat: int, must_join: int) -> bool:
data = await self.get_forcesub(chat)
if must_join in data["must_join"]:
return True
return False
async def get_forcesub(self, in_chat: int):
data = await self.forcesub.find_one({"chat": in_chat})
return data
async def get_all_forcesubs(self) -> list:
return [i async for i in self.forcesub.find({})]
async def add_gachabot(
self, client: int, bot: tuple[int, str], catch_command: str, chat_id: int
):
await self.gachabots.update_one(
{"client": client, "bot": bot[0]},
{
"$set": {
"username": bot[1],
"catch_command": catch_command,
"chat_id": chat_id,
"date": self.get_datetime(),
}
},
upsert=True,
)
async def rm_gachabot(self, client: int, bot: int, chat_id: int = None):
if chat_id:
await self.gachabots.delete_one(
{"client": client, "bot": bot, "chat_id": chat_id}
)
else:
await self.gachabots.delete_one({"client": client, "bot": bot})
async def is_gachabot(self, client: int, bot: int, chat_id: int) -> bool:
data = await self.get_gachabot(client, bot, chat_id)
return True if data else False
async def get_gachabot(self, client: int, bot: int, chat_id: int):
data = await self.gachabots.find_one(
{"client": client, "bot": bot, "chat_id": chat_id}
)
return data
async def get_all_gachabots(self, client: int) -> list:
return [i async for i in self.gachabots.find({"client": client})]
async def get_all_gachabots_id(self) -> list:
data = await self.gachabots.distinct("bot")
return data
async def _get_cohere_chat_from_db(self, user_id):
user_data = await self.cohere.find_one({"user_id": user_id})
return user_data.get("cohere_chat", []) if user_data else []
async def _update_cohere_chat_in_db(self, user_id, cohere_chat):
await self.cohere.update_one(
{"user_id": user_id},
{"$set": {"cohere_chat": cohere_chat}},
upsert=True
)
async def _clear_history_in_db(self, user_id):
unset_clear = {"cohere_chat": None}
return await self.cohere.update_one({"user_id": user_id}, {"$unset": unset_clear})
async def clear_database(self, user_id):
"""Clear the cohere history for the current user."""
result = await self._clear_history_in_db(user_id)
if result.modified_count > 0:
return "Chat history cleared successfully."
else:
return "No chat history found to clear."
async def set_chat_setting(self, chat_id, isboolean):
await self.antiarabic.update_one(
{"chat_id": chat_id},
{"$set": {"arabic": isboolean}},
upsert=True
)
async def chat_antiarabic(self, chat_id):
user_data = await self.antiarabic.find_one({"chat_id": chat_id})
if user_data:
return user_data.get("arabic", False)
return False
async def add_chatbot(self, chat_id, user_id):
await self.chatbot.update_one(
{"chat_id": chat_id},
{"$set": {"user_id": user_id}},
upsert=True
)
async def get_chatbot(self, chat_id):
user_data = await self.chatbot.find_one({"chat_id": chat_id})
return user_data.get("user_id") if user_data else None
async def remove_chatbot(self, chat_id):
unset_data = {"user_id": None}
return await self.chatbot.update_one({"chat_id": chat_id}, {"$unset": unset_data})
async def _update_chatbot_chat_in_db(self, user_id, chatbot_chat):
await self.backup_chatbot.update_one(
{"user_id": user_id},
{"$set": {"chatbot_chat": chatbot_chat}},
upsert=True
)
async def _get_chatbot_chat_from_db(self, user_id):
user_data = await self.backup_chatbot.find_one({"user_id": user_id})
return user_data.get("chatbot_chat", []) if user_data else []
async def _clear_chatbot_history_in_db(self, user_id):
unset_clear = {"chatbot_chat": None}
return await self.backup_chatbot.update_one({"user_id": user_id}, {"$unset": unset_clear})
async def _clear_chatbot_database(self, user_id):
result = await self._clear_chatbot_history_in_db(user_id)
if result.modified_count > 0:
return "Chat history cleared successfully."
else:
return "No chat history found to clear."
async def set_chat_setting_antinsfw(self, chat_id, isboolean):
await self.antinsfw.update_one(
{"chat_id": chat_id},
{"$set": {"antinsfw": isboolean}},
upsert=True
)
async def chat_antinsfw(self, chat_id):
user_data = await self.antinsfw.find_one({"chat_id": chat_id})
if user_data:
return user_data.get("antinsfw", False)
return False
db = Database(MONGO_URL) |