Tes cleanmode AFK

This commit is contained in:
yasir 2022-12-05 11:24:53 +07:00
parent 8068ea623c
commit 76e696edda
5 changed files with 173 additions and 63 deletions

View file

@ -1,6 +1,44 @@
#
# 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.
#
from database import dbname from database import dbname
usersdb = dbname.users usersdb = dbname.users
cleandb = dbname.cleanmode
cleanmode = {}
async def is_cleanmode_on(chat_id: int) -> bool:
mode = cleanmode.get(chat_id)
if not mode:
user = await cleandb.find_one({"chat_id": chat_id})
if not user:
cleanmode[chat_id] = True
return True
cleanmode[chat_id] = False
return False
return mode
async def cleanmode_on(chat_id: int):
cleanmode[chat_id] = True
user = await cleandb.find_one({"chat_id": chat_id})
if user:
return await cleandb.delete_one({"chat_id": chat_id})
async def cleanmode_off(chat_id: int):
cleanmode[chat_id] = False
user = await cleandb.find_one({"chat_id": chat_id})
if not user:
return await cleandb.insert_one({"chat_id": chat_id})
async def is_afk(user_id: int) -> bool: async def is_afk(user_id: int) -> bool:
@ -9,10 +47,9 @@ async def is_afk(user_id: int) -> bool:
async def add_afk(user_id: int, mode): async def add_afk(user_id: int, mode):
await usersdb.update_one({"user_id": user_id}, {"$set": { await usersdb.update_one(
"reason": mode {"user_id": user_id}, {"$set": {"reason": mode}}, upsert=True
}}, )
upsert=True)
async def remove_afk(user_id: int): async def remove_afk(user_id: int):

View file

@ -12,7 +12,7 @@ from misskaty.plugins import ALL_MODULES
from misskaty.helper import paginate_modules from misskaty.helper import paginate_modules
from misskaty.helper.tools import bot_sys_stats from misskaty.helper.tools import bot_sys_stats
from database.users_chats_db import db from database.users_chats_db import db
from misskaty.vars import LOG_CHANNEL from misskaty.vars import LOG_CHANNEL, SUDO
from utils import temp from utils import temp
from pyrogram.raw.all import layer from pyrogram.raw.all import layer
from pyrogram import idle, __version__, filters from pyrogram import idle, __version__, filters
@ -53,10 +53,11 @@ async def start_bot():
try: try:
LOGGER.info("[INFO]: SENDING ONLINE STATUS") LOGGER.info("[INFO]: SENDING ONLINE STATUS")
await app.send_message( for i in SUDO:
617426792, await app.send_message(
f"USERBOT AND BOT STARTED with Pyrogram v{__version__}..\nUserBot: {ubot.first_name}\nBot: {me.first_name}\n\nwith Pyrogram v{__version__} (Layer {layer}) started on @{me.username}.", SUDO,
) f"USERBOT AND BOT STARTED with Pyrogram v{__version__}..\nUserBot: {ubot.first_name}\nBot: {me.first_name}\n\nwith Pyrogram v{__version__} (Layer {layer}) started on @{me.username}.",
)
except Exception: except Exception:
pass pass
@ -336,7 +337,4 @@ General command are:
if __name__ == "__main__": if __name__ == "__main__":
try: loop.run_until_complete(start_bot())
loop.run_until_complete(start_bot())
except KeyboardInterrupt:
LOGGER.info("----------------------- Service Stopped -----------------------")

View file

@ -1,14 +1,28 @@
# Sample menggunakan modul motor mongodb #
# 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, asyncio import time, asyncio
from misskaty import app from misskaty import app
from utils import put_cleanmode
from pyrogram import filters from pyrogram import filters
from misskaty.vars import COMMAND_HANDLER from misskaty.vars import COMMAND_HANDLER
from database.afk_db import remove_afk, is_afk, add_afk from database.afk_db import remove_afk, is_afk, add_afk, is_cleanmode_on, cleanmode_off, cleanmode_on
from misskaty.helper.human_read import get_readable_time2 from misskaty.helper.human_read import get_readable_time2
from misskaty.core.decorator.errors import capture_err from misskaty.core.decorator.errors import capture_err
from misskaty.core.decorator.permissions import adminsOnly
__MODULE__ = "AFK" __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 💔. __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.""" Just type something in group to remove AFK Status."""
@ -29,46 +43,44 @@ async def active_afk(_, message):
reasonafk = reasondb["reason"] reasonafk = reasondb["reason"]
seenago = get_readable_time2((int(time.time() - timeafk))) seenago = get_readable_time2((int(time.time() - timeafk)))
if afktype == "text": if afktype == "text":
return await message.reply_text( send = await message.reply_text(
f"**{message.from_user.first_name}** is back online and was away for {seenago}", f"**{message.from_user.first_name}** is back online and was away for {seenago}",
disable_web_page_preview=True, disable_web_page_preview=True,
) )
if afktype == "text_reason": if afktype == "text_reason":
return await message.reply_text( send = await message.reply_text(
f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}", f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
disable_web_page_preview=True, disable_web_page_preview=True,
) )
if afktype == "animation": if afktype == "animation":
return ( if str(reasonafk) == "None":
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}", caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}",
) )
if str(reasonafk) == "None" else:
else await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}", caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
) )
) if afktype == "photo":
if str(reasonafk) == "None":
elif afktype == "photo": send = await message.reply_photo(
return (
await message.reply_photo(
photo=f"downloads/{user_id}.jpg", photo=f"downloads/{user_id}.jpg",
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}", caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}",
) )
if str(reasonafk) == "None" else:
else await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{user_id}.jpg", photo=f"downloads/{user_id}.jpg",
caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}", caption=f"**{message.from_user.first_name}** is back online and was away for {seenago}\n\nReason: `{reasonafk}`",
) )
) except Exception as e:
send = await message.reply_text(
except Exception: f"**{message.from_user.first_name}** is back online",
return await message.reply_text(
f"**{message.from_user.first_name}** is back online.",
disable_web_page_preview=True, disable_web_page_preview=True,
) )
await put_cleanmode(message.chat.id, send.message_id)
return
if len(message.command) == 1 and not message.reply_to_message: if len(message.command) == 1 and not message.reply_to_message:
details = { details = {
"type": "text", "type": "text",
@ -164,12 +176,26 @@ async def active_afk(_, message):
} }
await add_afk(user_id, details) await add_afk(user_id, details)
pesan = await message.reply_text( send = await message.reply_text(
f"{message.from_user.mention} [<code>{message.from_user.id}</code>] is now AFK! This message will be deleted in 10s." f"{message.from_user.mention} [<code>{message.from_user.id}</code>] is now AFK! This message will be deleted in 10s."
) )
await asyncio.sleep(10) await put_cleanmode(message.chat.id, send.message_id)
await pesan.delete()
try:
await message.delete() @app.on_message(filters.command("afkdel") & ~filters.private)
except: @adminsOnly
pass async def captcha_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)

View file

@ -1,6 +1,17 @@
#
# 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 re import re
import time import time
import asyncio from utils import put_cleanmode
from misskaty import app from misskaty import app
from pyrogram import filters, enums from pyrogram import filters, enums
from database.afk_db import remove_afk, is_afk from database.afk_db import remove_afk, is_afk
@ -43,23 +54,23 @@ async def chat_watcher_func(_, message):
msg += f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n" msg += f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n"
if afktype == "animation": if afktype == "animation":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n", caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n",
) )
else: else:
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n", caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n",
) )
if afktype == "photo": if afktype == "photo":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{userid}.jpg", photo=f"downloads/{userid}.jpg",
caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n", caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n",
) )
else: else:
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{userid}.jpg", photo=f"downloads/{userid}.jpg",
caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n", caption=f"**{user_name[:25]}** is back online and was away for {seenago}\n\n**Reason:** {reasonafk}\n\n",
) )
@ -85,23 +96,23 @@ async def chat_watcher_func(_, message):
msg += f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n" msg += f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n"
if afktype == "animation": if afktype == "animation":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n", caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n",
) )
if afktype == "photo": if afktype == "photo":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{replied_user_id}.jpg", photo=f"downloads/{replied_user_id}.jpg",
caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{replied_user_id}.jpg", photo=f"downloads/{replied_user_id}.jpg",
caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n", caption=f"**{replied_first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n",
) )
@ -140,23 +151,23 @@ async def chat_watcher_func(_, message):
msg += f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n" msg += f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n"
if afktype == "animation": if afktype == "animation":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason**: {reasonafk}\n\n", caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason**: {reasonafk}\n\n",
) )
if afktype == "photo": if afktype == "photo":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{user.id}.jpg", photo=f"downloads/{user.id}.jpg",
caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{user.id}.jpg", photo=f"downloads/{user.id}.jpg",
caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n", caption=f"**{user.first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n",
) )
@ -188,23 +199,23 @@ async def chat_watcher_func(_, message):
msg += f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n" msg += f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n"
if afktype == "animation": if afktype == "animation":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_animation( send = await message.reply_animation(
data, data,
caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n", caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n",
) )
if afktype == "photo": if afktype == "photo":
if str(reasonafk) == "None": if str(reasonafk) == "None":
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{user_id}.jpg", photo=f"downloads/{user_id}.jpg",
caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n", caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n",
) )
else: else:
await message.reply_photo( send = await message.reply_photo(
photo=f"downloads/{user_id}.jpg", photo=f"downloads/{user_id}.jpg",
caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n", caption=f"**{first_name[:25]}** is AFK since {seenago} ago.\n\n**Reason:** {reasonafk}\n\n",
) )
@ -213,8 +224,10 @@ async def chat_watcher_func(_, message):
j += 1 j += 1
if msg != "": if msg != "":
try: try:
pesan = await message.reply_text(msg, disable_web_page_preview=True) send = await message.reply_text(msg, disable_web_page_preview=True)
await asyncio.sleep(20)
await pesan.delete()
except: except:
return return
try:
await put_cleanmode(message.chat.id, send.message_id)
except:
return

View file

@ -11,11 +11,44 @@ from typing import Union
import os import os
import emoji import emoji
from database.users_chats_db import db from database.users_chats_db import db
from database.afk_db import is_cleanmode_on
from misskaty import app
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
BANNED = {} BANNED = {}
async def put_cleanmode(chat_id, message_id):
if chat_id not in cleanmode:
cleanmode[chat_id] = []
time_now = datetime.now()
put = {
"msg_id": message_id,
"timer_after": time_now + timedelta(minutes=1),
}
cleanmode[chat_id].append(put)
async def auto_clean():
while not await asyncio.sleep(30):
try:
for chat_id in cleanmode:
if not await is_cleanmode_on(chat_id):
continue
for x in cleanmode[chat_id]:
if datetime.now() > x["timer_after"]:
try:
await app.delete_messages(chat_id, x["msg_id"])
except FloodWait as e:
await asyncio.sleep(e.x)
except:
continue
else:
continue
except:
continue
# temp db for banned # temp db for banned
class temp(object): class temp(object):
BANNED_USERS = [] BANNED_USERS = []
@ -110,3 +143,6 @@ def extract_user(message: Message) -> Union[int, str]:
user_id = message.from_user.id user_id = message.from_user.id
user_first_name = message.from_user.first_name user_first_name = message.from_user.first_name
return (user_id, user_first_name) return (user_id, user_first_name)
asyncio.create_task(auto_clean())