Captain Ezio
New update v2.1.0
d720c41
raw
history blame
8.86 kB
from datetime import date, datetime, time
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pyrogram import filters
from pyrogram.enums import ChatMemberStatus, ChatType
from pyrogram.types import CallbackQuery
from pyrogram.types import InlineKeyboardButton as IKB
from pyrogram.types import InlineKeyboardMarkup as IKM
from pyrogram.types import Message
from Powers import TIME_ZONE
from Powers.bot_class import Gojo
from Powers.database.chats_db import Chats
from Powers.plugins import bday_cinfo, bday_info
from Powers.utils.custom_filters import command
@Gojo.on_message(command("remember"))
async def remember_me(c: Gojo, m: Message):
splited = m.text.split()
if len(splited) != 2 and m.reply_to_message:
await m.reply_text("**USAGE**:\n/remember [username or user id or reply to user] [DOB]\nDOB should be in format of dd/mm/yyyy\nYear is optional it is not necessary to pass it")
return
elif len(splited) != 3 and not m.reply_to_message:
await m.reply_text("**USAGE**:\n/remember [username or user id or reply to user] [DOB]\nDOB should be in format of dd/mm/yyyy\nYear is optional it is not necessary to pass it")
return
DOB = splited[1] if len(splited) == 2 else splited[2]
if len(splited) == 2:
user = m.reply_to_message.from_user.id
else:
try:
u_id = int(splited[1])
except ValueError:
pass
try:
user = await c.get_users(u_id)
except Exception:
u_u = await c.resolve_peer(u_id)
try:
user = (await c.get_users(u_u.user_id)).id
except KeyError:
await m.reply_text("Unable to find the user")
return
DOB = DOB.split("/")
if len(DOB) != 3 or len(DOB) != 2:
await m.reply_text("DOB should be in format of dd/mm/yyyy\nYear is optional it is not necessary to pass it")
return
if len(DOB) == 3:
is_correct = (len(DOB[2]) == 3)
if len(DOB[0]) != 2 or len(DOB[1]) !=2 or not is_correct:
await m.reply_text("DOB should be in format of dd/mm/yyyy\nYear is optional it is not necessary to pass it")
return
try:
date = int(DOB[0])
month = int(DOB[1])
if is_correct:
year = int(DOB[2])
is_year = 1
else:
year = "1800"
is_year = 0
DOB = f"{str(date)}/{str(month)}/{year}"
form = "%d/%m/%Y"
datee = datetime.strptime(DOB,form).date()
except ValueError:
await m.reply_text("DOB should be numbers only")
return
data = {"user_id":user,"dob":datee,"is_year":is_year}
try:
result = bday_info.find_one({"user_id":user})
if result:
await m.reply_text("User is already in my database")
return
except Exception as e:
await m.reply_text(f"Got an error\n{e}")
try:
bday_info.insert_one(data)
except Exception as e:
await m.reply_text(f"Got an error\n{e}")
return
@Gojo.on_message(command(["removebday","rmbday"]))
async def who_are_you_again(c: Gojo, m: Message):
user = m.from_user.id
try:
result = bday_info.find_one({"user_id":user})
if not result:
await m.reply_text("User is not in my database")
return
elif result:
bday_info.delete_one({"user_id":user})
await m.reply_text("Removed your birthday")
return
except Exception as e:
await m.reply_text(f"Got an error\n{e}")
return
@Gojo.on_message(command(["nextbdays","nbdays"]))
async def who_is_next(c: Gojo, m: Message):
blist = list(bday_info.find())
curr = datetime.now(TIME_ZONE).date()
xx = await m.reply_text("📆")
if blist:
users = []
for i in blist:
if i["dob"].month >= curr.month:
if i["dob"].month == curr.month and i["dob"].days < curr.days:
continue
users.append(i)
elif i["dob"].month < curr.month:
continue
if len(users) == 10:
break
if not users:
xx.delete()
await m.reply_text("No birthdays found :/")
txt = "🎊 Upcomming Birthdays Are 🎊\n"
for i in users:
DOB = i["dob"]
dete = date(curr.year, DOB.month, DOB.day)
leff = (dete - curr).days
txt += f"`{i['user_id']}` : {leff} days left"
txt += "\nYou can use /info [user id] to get info about the user"
await xx.delete()
await m.reply_text(txt)
return
@Gojo.on_message(command(["getbday","gbday"]))
async def cant_recall_it(c: Gojo, m: Message):
user = m.from_user.id
if m.reply_to_message:
user = m.reply_to_message.from_user.id
try:
result = bday_info.find_one({"user_id":user})
if not result:
await m.reply_text("User is not in my database")
return
except Exception as e:
await m.reply_text(f"Got an error\n{e}")
return
curr = datetime.now(TIME_ZONE).date()
u_dob = result["dob"]
if u_dob.month < curr.month:
next_b = date(curr.year + 1, u_dob.month, u_dob.day)
days_left = (next_b - curr).days
txt = f"User's birthday is passed 🫤\nDays left until next one {days_left}"
else:
u_dobm = date(curr.year, u_dob.month, u_dob.day)
days_left = (u_dobm - curr).days
txt = f"User's birthday is coming🥳\nDays left until next one {days_left}"
await m.reply_text(txt)
return
@Gojo.on_message(command(["settingbday","sbday"]))
async def chat_birthday_settings(c: Gojo, m: Message):
if m.chat.type == ChatType.PRIVATE:
await m.reply_text("Use in groups")
return
chats = m.chat.id
c_in = bday_cinfo.find_one({"chat_id":chats})
kb = IKM(
[
[
IKB(f"{'Yes' if not c_in else 'No'}",f"switchh_{'yes' if not c_in else 'no'}"),
IKB("Close", "f_close")
]
]
)
await m.reply_text("Do you want to wish members for their birthday in the group?",reply_markup=kb)
return
@Gojo.on_callback_query(filters.regex("^switchh_"))
async def switch_on_off(c:Gojo, q: CallbackQuery):
user = (await q.message.chat.get_member(q.from_user.id)).status
await q.message.chat.get_member(q.from_user.id)
if user not in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
await q.answer("...")
return
data = q.data.split("_")[1]
chats = q.message.chat.id
xXx = {"chat_id":chats}
if data == "yes":
bday_cinfo.delete_one(xXx)
elif data == "no":
bday_cinfo.insert_one(xXx)
await q.edit_message_text(f"Done! I will {'wish' if data == 'yes' else 'not wish'}",reply_markup=IKM([[IKB("Close", "f_close")]]))
return
scheduler = AsyncIOScheduler()
scheduler.timezone = TIME_ZONE
scheduler_time = time(0,0,0)
async def send_wishish(client):
c_list = Chats.list_chats_by_id()
blist = list(bday_info.find())
curr = datetime.now(TIME_ZONE).date()
for i in blist:
if i["dob"].month == curr.month and i["dob"].day == curr.day:
for j in c_list:
try:
U = await Gojo.get_chat_member(j,i)
if U.status in [ChatMemberStatus.MEMBER,ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER]:
xXx = await Gojo.send_message(j,f"Happy birthday\n{U.user.mention}")
try:
await xXx.pin()
except Exception:
pass
except Exception:
pass
""""
from datetime import date, datetime
#form =
num = "18/05/2005"
st = "18 May 2005"
timm = datetime.strptime(num,"%d/%m/%Y").date()
x = datetime.now().date()
if timm.month < x.month:
next_b = date(x.year + 1, timm.month, timm.day)
days_left = (next_b - x).days
else:
timmm = date(x.year, timm.month, timm.day)
days_left = (timmm - x).days
print(days_left)
print(x.year - timm.year)
"""
scheduler.add_job(send_wishish,'cron',hour=0,minute=0,second=0)
scheduler.start()
__PLUGIN__ = "birthday"
__HELP__ = """
• /remember [username or user id or reply to user] [DOB] : To registers user date of birth in my database
• /nextbdays (/nbdays) : Return upcoming birthdays of 10 users
• /removebday (/rmbday) : To remove birthday from database (One can only remove their data from database not of others)
• /settingbday (/sbday) : To configure the settings for wishing and all for the chat
• /getbday (/gbday) [reply to user] : If replied to user get the replied user's birthday else returns your birthday
DOB should be in format of dd/mm/yyyy
Year is optional it is not necessary to pass it
"""