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