MissKatyPyro/misskaty/plugins/afk.py
sourcery-ai[bot] 983f3b50ca
Sourcery refactored master branch (#3)
* 'Refactored by Sourcery'

* reformating: code

Co-authored-by: Sourcery AI <>
Co-authored-by: yasirarism <mail@yasir.eu.org>
2022-12-08 12:11:49 +07:00

203 lines
7.7 KiB
Python

#
# Copyright (C) 2021-2022 by TeamYukki@Github, < https://github.com/TeamYukki >.
#
# This file is part of < https://github.com/TeamYukki/YukkiAFKBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/TeamYukki/YukkiAFKBot/blob/master/LICENSE >
#
# All rights reserved.
#
# Modified plugin by me from https://github.com/TeamYukki/YukkiAFKBot to make compatible with pyrogram v2
import time
from misskaty import app
from utils import put_cleanmode
from pyrogram import filters
from misskaty.vars import COMMAND_HANDLER
from database.afk_db import (
remove_afk,
is_afk,
add_afk,
cleanmode_off,
cleanmode_on,
)
from misskaty.helper.human_read import get_readable_time2
from misskaty.core.decorator.errors import capture_err
from misskaty.core.decorator.permissions import adminsOnly
__MODULE__ = "AFK"
__HELP__ = """/afk [Reason > Optional] - Tell others that you are AFK (Away From Keyboard), so that your boyfriend or girlfriend won't look for you 💔.
/afk [reply to media] - AFK with media.
/afkdel - Enable auto delete AFK message in group (Only for group admin).
Just type something in group to remove AFK Status."""
# Handle set AFK Command
@capture_err
@app.on_message(filters.command(["afk"], COMMAND_HANDLER))
async def active_afk(_, message):
if message.sender_chat:
return
user_id = message.from_user.id
verifier, reasondb = await is_afk(user_id)
if verifier:
await remove_afk(user_id)
try:
afktype = reasondb["type"]
timeafk = reasondb["time"]
data = reasondb["data"]
reasonafk = reasondb["reason"]
seenago = get_readable_time2((int(time.time() - timeafk)))
if afktype == "animation":
send = (
await message.reply_animation(
data,
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}",
)
if str(reasonafk) == "None"
else await message.reply_animation(
data,
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
)
)
elif afktype == "photo":
send = (
await message.reply_photo(
photo=f"downloads/{user_id}.jpg",
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}",
)
if str(reasonafk) == "None"
else await message.reply_photo(
photo=f"downloads/{user_id}.jpg",
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
)
)
elif afktype == "text":
send = await message.reply_text(
f"**{message.from_user.first_name}** is back online and was away for {seenago}",
disable_web_page_preview=True,
)
elif afktype == "text_reason":
send = await message.reply_text(
f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
disable_web_page_preview=True,
)
except Exception:
send = await message.reply_text(
f"**{message.from_user.first_name}** is back online",
disable_web_page_preview=True,
)
await put_cleanmode(message.chat.id, send.id)
return
if len(message.command) == 1 and not message.reply_to_message:
details = {
"type": "text",
"time": time.time(),
"data": None,
"reason": None,
}
elif len(message.command) > 1 and not message.reply_to_message:
_reason = (message.text.split(None, 1)[1].strip())[:100]
details = {
"type": "text_reason",
"time": time.time(),
"data": None,
"reason": _reason,
}
elif len(message.command) == 1 and message.reply_to_message.animation:
_data = message.reply_to_message.animation.file_id
details = {
"type": "animation",
"time": time.time(),
"data": _data,
"reason": None,
}
elif len(message.command) > 1 and message.reply_to_message.animation:
_data = message.reply_to_message.animation.file_id
_reason = (message.text.split(None, 1)[1].strip())[:100]
details = {
"type": "animation",
"time": time.time(),
"data": _data,
"reason": _reason,
}
elif len(message.command) == 1 and message.reply_to_message.photo:
await app.download_media(message.reply_to_message, file_name=f"{user_id}.jpg")
details = {
"type": "photo",
"time": time.time(),
"data": None,
"reason": None,
}
elif len(message.command) > 1 and message.reply_to_message.photo:
await app.download_media(message.reply_to_message, file_name=f"{user_id}.jpg")
_reason = message.text.split(None, 1)[1].strip()
details = {
"type": "photo",
"time": time.time(),
"data": None,
"reason": _reason,
}
elif len(message.command) == 1 and message.reply_to_message.sticker:
if message.reply_to_message.sticker.is_animated:
details = {
"type": "text",
"time": time.time(),
"data": None,
"reason": None,
}
else:
await app.download_media(message.reply_to_message, file_name=f"{user_id}.jpg")
details = {
"type": "photo",
"time": time.time(),
"data": None,
"reason": None,
}
elif len(message.command) > 1 and message.reply_to_message.sticker:
_reason = (message.text.split(None, 1)[1].strip())[:100]
if message.reply_to_message.sticker.is_animated:
details = {
"type": "text_reason",
"time": time.time(),
"data": None,
"reason": _reason,
}
else:
await app.download_media(message.reply_to_message, file_name=f"{user_id}.jpg")
details = {
"type": "photo",
"time": time.time(),
"data": None,
"reason": _reason,
}
else:
details = {
"type": "text",
"time": time.time(),
"data": None,
"reason": None,
}
await add_afk(user_id, details)
send = await message.reply_text(f"{message.from_user.mention} [<code>{message.from_user.id}</code>] is now AFK!.")
await put_cleanmode(message.chat.id, send.id)
@app.on_message(filters.command("afkdel") & ~filters.private)
@adminsOnly("can_change_info")
async def afk_state(_, message):
usage = "**Usage:**\n/afkdel [ENABLE|DISABLE]"
if len(message.command) == 1:
return await message.reply_text(usage)
chat_id = message.chat.id
state = message.text.split(None, 1)[1].strip()
state = state.lower()
if state == "enable":
await cleanmode_on(chat_id)
await message.reply_text("Enabled auto delete AFK message.")
elif state == "disable":
await cleanmode_off(chat_id)
await message.reply_text("Disabled auto delete AFK message.")
else:
await message.reply_text(usage)