python_bot/handlers/post_cleaner.py
2024-01-23 06:33:53 +10:00

70 lines
2.3 KiB
Python

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
async def cleanup_sequence(userID: int, messageID: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(
{
'_key': userID,
'chat_id': chatID
},
skip=0, limit=1000)
prev_enl_message_id = clean_candidate["messageID"]
await bot.delete_message(prev_enl_message_id) #delete last reply to this user
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"]
to_delete_UserId = old_Post['_key']
await bot.delete_message(to_delete_ID) #delete old message
old_posts.delete(to_delete_UserId) #delete old message DB entry
except Exception as autoCleanErr:
print(autoCleanErr)
#write last reply post ID to db
remove_collection = get_remove_collection
message_time = datetime.datetime.now()
try:
lastReply = {
'_key': userID,
'datetimes': message_time,
'chat_id': chatID,
'messageID': messageID
}
metadata = remove_collection.insert(lastReply, overwrite_mode='update') #write last reply time
except Exception as lastWriteErr:
print(lastWriteErr)
return 0