forked from Spyderzz/Userbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Shrimadhav U K <[email protected]>
- Loading branch information
Showing
10 changed files
with
243 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
try: | ||
from userbot.modules.sql_helper import SESSION, BASE | ||
except ImportError: | ||
raise AttributeError | ||
|
||
from sqlalchemy import BigInteger, Boolean, Column, String, UnicodeText | ||
|
||
class Welcome(BASE): | ||
__tablename__ = "welcome" | ||
chat_id = Column(String(14), primary_key=True) | ||
custom_welcome_message = Column(UnicodeText, nullable=False) | ||
media_file_id = Column(UnicodeText) | ||
should_clean_welcome = Column(Boolean, default=False) | ||
previous_welcome = Column(BigInteger) | ||
|
||
def __init__(self, chat_id, custom_welcome_message, should_clean_welcome, previous_welcome, media_file_id=None): | ||
self.chat_id = str(chat_id) | ||
self.custom_welcome_message = custom_welcome_message | ||
self.media_file_id = media_file_id | ||
self.should_clean_welcome = should_clean_welcome | ||
self.previous_welcome = previous_welcome | ||
|
||
|
||
Welcome.__table__.create(checkfirst=True) | ||
|
||
|
||
def get_current_welcome_settings(chat_id): | ||
try: | ||
return SESSION.query(Welcome).filter(Welcome.chat_id == str(chat_id)).one() | ||
except: | ||
return None | ||
finally: | ||
SESSION.close() | ||
|
||
|
||
def add_welcome_setting(chat_id, custom_welcome_message, should_clean_welcome, previous_welcome,media_file_id=None): | ||
try: | ||
adder = Welcome(chat_id, custom_welcome_message, should_clean_welcome, previous_welcome, media_file_id) | ||
SESSION.add(adder) | ||
SESSION.commit() | ||
return True | ||
except: | ||
return False | ||
|
||
|
||
def rm_welcome_setting(chat_id): | ||
try: | ||
rem = SESSION.query(Welcome).get(str(chat_id)) | ||
if rem: | ||
SESSION.delete(rem) | ||
SESSION.commit() | ||
return True | ||
except: | ||
return False | ||
|
||
|
||
def update_previous_welcome(chat_id, previous_welcome): | ||
row = SESSION.query(Welcome).get(chat_id) | ||
row.previous_welcome = previous_welcome | ||
# commit the changes to the DB | ||
SESSION.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from telethon.utils import pack_bot_file_id | ||
from userbot.modules.sql_helper.welcome_sql import get_current_welcome_settings, add_welcome_setting, rm_welcome_setting | ||
from userbot.events import register | ||
from userbot import CMD_HELP, bot, LOGS | ||
from telethon.events import ChatAction | ||
|
||
|
||
@bot.on(ChatAction) | ||
async def welcome_to_chat(event): | ||
cws = get_current_welcome_settings(event.chat_id) | ||
if cws: | ||
"""user_added=False, | ||
user_joined=True, | ||
user_left=False, | ||
user_kicked=False,""" | ||
if event.user_joined or event.user_added: | ||
if cws.should_clean_welcome: | ||
try: | ||
await event.client.delete_messages( | ||
event.chat_id, | ||
cws.previous_welcome | ||
) | ||
except Exception as e: | ||
LOGS.warn(str(e)) | ||
|
||
a_user = await event.get_user() | ||
chat = await event.get_chat() | ||
me = await event.client.get_me() | ||
|
||
title = chat.title if chat.title else "this chat" | ||
|
||
participants = await event.client.get_participants(chat) | ||
count = len(participants) | ||
|
||
current_saved_welcome_message = cws.custom_welcome_message | ||
|
||
mention = "[{}](tg://user?id={})".format(a_user.first_name, a_user.id) | ||
my_mention = "[{}](tg://user?id={})".format(me.first_name, me.id) | ||
|
||
first = a_user.first_name | ||
last = a_user.last_name | ||
if last: | ||
fullname = f"{first} {last}" | ||
else: | ||
fullname = first | ||
|
||
username = f"@{a_user.username}" if a_user.username else mention | ||
|
||
userid = a_user.id | ||
|
||
my_first = me.first_name | ||
my_last = me.last_name | ||
if my_last: | ||
my_fullname = f"{my_first} {my_last}" | ||
else: | ||
my_fullname = my_first | ||
|
||
my_username = f"@{me.username}" if me.username else my_mention | ||
|
||
current_message = await event.reply( | ||
current_saved_welcome_message.format(mention=mention, | ||
title=title, | ||
count=count, | ||
first=first, | ||
last=last, | ||
fullname=fullname, | ||
username=username, | ||
userid=userid, | ||
my_first=my_first, | ||
my_last=my_last, | ||
my_fullname=my_fullname, | ||
my_username=my_username, | ||
my_mention=my_mention), | ||
file=cws.media_file_id | ||
) | ||
|
||
|
||
@register(outgoing=True, pattern=r"^.welcome(?: |$)(.*)") | ||
async def save_welcome(event): | ||
if not event.text[0].isalpha() and event.text[0] not in ("/", "#", "@", "!"): | ||
if event.fwd_from: | ||
return | ||
msg = await event.get_reply_message() | ||
input_str = event.pattern_match.group(1) | ||
if input_str: | ||
if add_welcome_setting(event.chat_id, input_str, True, 0) is True: | ||
await event.edit("`Welcome note saved !!`") | ||
else: | ||
await event.edit("`I can only have one welcome note per chat !!`") | ||
elif msg and msg.media: | ||
bot_api_file_id = pack_bot_file_id(msg.media) | ||
if add_welcome_setting(event.chat_id, msg.message, True, 0, bot_api_file_id) is True: | ||
await event.edit("`Welcome note saved !!`") | ||
else: | ||
await event.edit("`I can only have one welcome note per chat !!`") | ||
elif msg.message is not None: | ||
if add_welcome_setting(event.chat_id, msg.message, True, 0) is True: | ||
await event.edit("`Welcome note saved !!`") | ||
else: | ||
await event.edit("`I can only have one welcome note per chat !!`") | ||
|
||
|
||
@register(outgoing=True, pattern="^.show welcome$") | ||
async def show_welcome(event): | ||
if not event.text[0].isalpha() and event.text[0] not in ("/", "#", "@", "!"): | ||
if event.fwd_from: | ||
return | ||
cws = get_current_welcome_settings(event.chat_id) | ||
if cws: | ||
await event.edit(f"`The current welcome message is:`\n{cws.custom_welcome_message}") | ||
else: | ||
await event.edit("`No welcome note saved here !!`") | ||
|
||
@register(outgoing=True, pattern="^.del welcome") | ||
async def del_welcome(event): | ||
if not event.text[0].isalpha() and event.text[0] not in ("/", "#", "@", "!"): | ||
if event.fwd_from: | ||
return | ||
if rm_welcome_setting(event.chat_id) is True: | ||
await event.edit("`Welcome note deleted for this chat.`") | ||
else: | ||
await event.edit("`Do I even have a welcome note here ?`") | ||
|
||
|
||
CMD_HELP.update({ | ||
"welcome": "\ | ||
.welcome <notedata/reply>\ | ||
\nUsage: Saves (or updates) notedata / replied message as a welcome note in the chat.\ | ||
\nAvailable variables for formatting welcome messages : {mention}, {title}, {count}, {first}, {last}, {fullname}, {userid}, {username}, {my_first}, {my_fullname}, {my_last}, {my_mention}, {my_username}\ | ||
\n\n.show welcome\ | ||
\nUsage: Gets your current welcome message in the chat.\ | ||
\n\n.del welcome\ | ||
\nUsage: Deletes the welcome note for the current chat.\ | ||
"}) |