python_bot_v2/handlers/post_cleaner
2024-01-22 05:16:36 +10:00

74 lines
2.2 KiB
Plaintext

import datetime
from arango import ArangoClient
from create_bot import bot
def get_remove_collection():
arango_client = ArangoClient(hosts=CONFIG['databaso']['host'] )
pipisa_db = arango_client.db(
CONFIG['databaso']['base'],
username=CONFIG['databaso']['user'],
password=CONFIG['databaso']['pass']
)
remove_collection = pipisa_db.collection(CONFIG['databaso']['deleteCollection'])
return remove_collection
def time_writer(userID: int, messageID:int, chatID:int):
#write last reply post ID to db
remove_collection = get_remove_collection
message_time = datetime.datetime.now()
try:
lastReply = {
'user_id': userID,
'datetimes': message_time,
'chat_id': chatID,
'messageID': messageID
}
metadata = remove_collection.insert(lastReply, overwrite_mode='update')
except Exception as lastWriteErr:
print(lastWriteErr)
return 0
async def cleaner(userID:int, chatID:int):
#remove last reply and check posts older than 5 minutes to delete
remove_collection = get_remove_collection
try:
clean_candidate = remove_collection.find(
{
'user_id': userID,
'chat_id': chatID
},
skip=0, limit=1000)
prev_enl_message_id = clean_candidate["messageID"]
await bot.delete_message(prev_enl_message_id)
current_time = datetime.datetime.now()
too_old = current_time-datetime.timedelta(min=5)
old_posts = remove_collection.find(
{
'chat_id': chatID,
},
skip=0, limit=1000)
for old_Post in old_posts:
if old_Post["datetimes"] <= too_old:
to_delete_ID = old_Post["messageID"]
await bot.delete_message(to_delete_ID)
#TODO: remove deleted posts ID's from DB
except Exception as autoCleanErr:
print(autoCleanErr)
return 0