Skip to content

Commit

Permalink
fix my bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Mar 17, 2020
1 parent 7a3e953 commit e49b222
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
13 changes: 6 additions & 7 deletions config.env.sample
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Remove this line first before doing anything else
_____REMOVE_____THIS_____LINE_____=True

#
# Get from https://my.telegram.org/
#

# Get them from https://my.telegram.org/
API_ID=""
API_HASH=""

#
# Get by running `bash genStr` command
#

# Get it by running `bash genStr` command
HU_STRING_SESSION=""


# Mongodb url from https://cloud.mongodb.com/
DATABASE_URL = ''
DATABASE_URL = ""
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

python3.8 -m userge
python3 -m userge
2 changes: 1 addition & 1 deletion userge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .utils import Config, logging
from .client import Userge
from .client import Userge, Filters

log = logging.getLogger(__name__)

Expand Down
14 changes: 12 additions & 2 deletions userge/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class Userge(Client):

HELP_DICT: Dict[str, str]
HELP_DICT: Dict[str, str] = {}
USERGE_MAIN_STRING = "<<<! ##### ___{}___ ##### !>>>"
USERGE_SUB_STRING = "<<<! {} !>>>"
MSG: Message
MSG = Message

def __init__(self) -> None:
self.log = logging.getLogger(__name__)
Expand Down Expand Up @@ -42,6 +42,16 @@ def decorator(func: PyroFunc) -> Any:

return decorator

def on_new_member(self, welcome_chats: Filters.chat) -> Callable[[PyroFunc], Any]:

def decorator(func: PyroFunc) -> Any:
self.log.info(self.USERGE_SUB_STRING.format(f"Loading => [ async def {func.__name__}(client, message) ] On New Member in {welcome_chats}"))
dec = self.on_message(Filters.new_chat_members & welcome_chats)

return dec(func)

return decorator

def __add_help(self, command: str, about: str) -> None:
self.log.info(self.USERGE_SUB_STRING.format(f"Updating Help Dict => [ {command} : {about} ]"))
self.HELP_DICT.update({command: about})
Expand Down
55 changes: 30 additions & 25 deletions userge/plugins/welcome.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
from userge import userge
from userge import userge, Filters
from userge.db import Database
from pyrogram import Filters

log = userge.getLogger(__name__)

welcome_db = Database("welcome")

welcome_list = welcome_db.filter({'on': True}, {'_id': 1})

welcome_chats = Filters.chat([])
welcome__list = Database("welcome").filter({'on': True}, {'_id': 1})
for i in welcome__list:

for i in welcome_list:
welcome_chats.add(i.get('_id'))


@userge.on_cmd("setwelcome", about="Creates a welcome message in current chat :)")
async def setwel(_, message: userge.MSG):
welcome_db = Database("welcome")
if message.chat.type in ["private", "bot", "channel"]:
await message.edit('Are you high XO\nSet welcome in a group chat')
return

try:
welcome_string = message.text.split(" ", maxsplit=1)[1]
except IndexError:
await message.edit("wrong syntax\n`.setwelcome <welcome message>`")
else:
new_entry = {'_id': message.chat.id, 'data': welcome_string, 'on': True}

if welcome_db.findone('_id', message.chat.id):
welcome_db.update({'_id': message.chat.id}, new_entry, 'set')
else:
welcome_db.addnew(new_entry)

welcome_chats.add(message.chat.id)
await message.edit(f"Welcome message has been set for the \n`{message.chat.title}`")

Expand All @@ -36,46 +42,45 @@ async def nowel(_, message: userge.MSG):
except KeyError as e:
await message.edit(e)
else:
welcome_db = Database("welcome")
welcome_db.update({'_id': message.chat.id}, {'on': False}, 'set')
await message.edit("Dissabled Successfully !")


@userge.on_cmd("dowelcome", about="Turns on welcome message in the current chat :)")
async def dowel(_, message: userge.MSG):
welcome_db = Database("welcome")
if welcome_db.findone('_id', message.chat.id):
welcome_chats.add(message.chat.id)
welcome_db.update({'_id': message.chat.id}, {'on': True}, 'set')
await message.edit('I will welcome new members XD')

else:
await message.edit('Please set the welcome message with `.setwelcome`')


@userge.on_message(Filters.new_chat_members & welcome_chats)
@userge.on_new_member(welcome_chats)
async def saywel(_, message: userge.MSG):
welcome_db = Database("welcome")
welcome_message = welcome_db.findone('_id', message.chat.id)['data']

user = message.from_user
fname = user.first_name
lname = user.last_name
uname = user.username
chat = message.chat.title
kwargs = {
'fname': message.from_user.first_name,
'lname': message.from_user.last_name,
'fullname': message.from_user.first_name + message.from_user.last_name,
'uname': message.from_user.username,
'chat': message.chat.title,
'mention': f'<a href="tg://user?id={message.from_user.id}">\
{message.from_user.username or message.from_user.first_name + message.from_user.last_name}</a>',
}

mention = f'<a href="tg://user?id={user.id}">{uname or fname}</a>'
await message.reply(welcome_message.format(chat=chat, fname=fname, lname=lname, uname=uname, mention=mention))
await message.reply(welcome_message.format(**kwargs))


@userge.on_cmd("listwelcome", about="Shows the activated chats for welcome")
async def lswel(_, message: userge.MSG):
liststr = ''
welcome_list = Database("welcome").filter({'on': True}, {'_id': 1, 'data': 1})
liststr = ""
welcome_list = welcome_db.filter({'on': True}, {'_id': 1, 'data': 1})

for j in welcome_list:
chatid = j.get('_id')
chatname = (await userge.get_chat(chatid)).title
welcome_msg = j.get("data")
print(chatname, welcome_msg)
# liststr.join(f'>--{chatname}--\n')
# liststr = liststr if liststr != '' else '`NO WELCOMES STARTED`'
# await message.edit(liststr)
liststr += f"**{(await userge.get_chat(j.get('_id'))).title}**\n"
liststr += f"`{j.get('data')}`\n\n"

await message.edit(liststr or '`NO WELCOMES STARTED`')

0 comments on commit e49b222

Please sign in to comment.