python_bot/handlers/post_cleaner.py

70 lines
2.3 KiB
Python
Raw Normal View History

2024-01-22 05:10:13 +10:00
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
2024-01-23 06:33:53 +10:00
async def cleanup_sequence(userID: int, messageID:int, chatID:int):
2024-01-22 05:10:13 +10:00
#remove last reply and check posts older than 5 minutes to delete
remove_collection = get_remove_collection
try:
clean_candidate = remove_collection.find(
{
2024-01-23 06:33:53 +10:00
'_key': userID,
2024-01-22 05:10:13 +10:00
'chat_id': chatID
},
skip=0, limit=1000)
prev_enl_message_id = clean_candidate["messageID"]
2024-01-23 06:33:53 +10:00
await bot.delete_message(prev_enl_message_id) #delete last reply to this user
2024-01-22 05:10:13 +10:00
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"]
2024-01-23 06:33:53 +10:00
to_delete_UserId = old_Post['_key']
2024-01-22 05:10:13 +10:00
2024-01-23 06:33:53 +10:00
await bot.delete_message(to_delete_ID) #delete old message
old_posts.delete(to_delete_UserId) #delete old message DB entry
2024-01-22 05:10:13 +10:00
except Exception as autoCleanErr:
print(autoCleanErr)
2024-01-23 06:33:53 +10:00
#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)
2024-01-22 05:10:13 +10:00
return 0