Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Mar 19, 2020
1 parent 69283aa commit 44853af
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 202 deletions.
34 changes: 34 additions & 0 deletions userge/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Callable
)

import os
from .utils import Config, logging

logging.getLogger("pyrogram").setLevel(logging.WARNING)
Expand Down Expand Up @@ -85,6 +86,39 @@ def on_left_member(
group=group
)

async def send_output_as_file(
self,
output: str,
message: Message,
filename: str = "output.txt",
caption: str = '',
delete_message: bool = True
) -> None:

with open(filename, "w+", encoding="utf8") as out_file:
out_file.write(output)

reply_to_id = message.reply_to_message.message_id \
if message.reply_to_message \
else message.message_id

self.log.info(
self.__USERGE_SUB_STRING.format(f"Uploading {filename} To Telegram")
)

await self.send_document(
chat_id=message.chat.id,
document=filename,
caption=caption,
disable_notification=True,
reply_to_message_id=reply_to_id
)

os.remove(filename)

if delete_message:
await message.delete()

def get_help(
self,
key: str = ''
Expand Down
4 changes: 2 additions & 2 deletions userge/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def find_one(
f"{self.name} :: Finding One {dict_}"
)

ret_val = list(self.db.reviews.find_one(dict_))
ret_val = dict(self.db.reviews.find_one(dict_))

self.log.info(
f"{self.name} :: Found {ret_val} For {dict_}"
)

return ret_val[0] if ret_val else None
return ret_val

def find_all(
self,
Expand Down
43 changes: 19 additions & 24 deletions userge/plugins/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ async def aexec(code, userge, message):
OUTPUT = "**EVAL**:\n```{}```\n\n**OUTPUT**:\n```{}```".format(cmd, evaluation.strip())

if len(OUTPUT) > Config.MAX_MESSAGE_LENGTH:
await send_as_file("eval.txt", OUTPUT, message, cmd)
await userge.send_output_as_file(
output=OUTPUT,
message=message,
filename="eval.txt",
caption=cmd
)

else:
await message.edit(OUTPUT)
Expand Down Expand Up @@ -83,7 +88,12 @@ async def exec_(_, message):
OUTPUT = f"**EXEC**:\n\n__Command:__\n`{cmd}`\n__PID:__\n`{process.pid}`\n\n**stderr:**\n`{err}`\n\n**stdout:**\n``{out}`` "

if len(OUTPUT) > Config.MAX_MESSAGE_LENGTH:
await send_as_file("exec.txt", OUTPUT, message, cmd)
await userge.send_output_as_file(
output=OUTPUT,
message=message,
filename="exec.txt",
caption=cmd
)

else:
await message.edit(OUTPUT)
Expand All @@ -105,7 +115,12 @@ async def term_(_, message):
OUTPUT = str(stdout.decode().strip()) + str(stderr.decode().strip())

if len(OUTPUT) > Config.MAX_MESSAGE_LENGTH:
await send_as_file("term.txt", OUTPUT, message, cmd)
await userge.send_output_as_file(
output=OUTPUT,
message=message,
filename="term.txt",
caption=cmd
)

else:
try:
Expand Down Expand Up @@ -136,24 +151,4 @@ async def init_func(message):
await message.edit("`That's a dangerous operation! Not Permitted!`")
return None

return cmd


async def send_as_file(name, OUTPUT, message, cmd):
with open(name, "w+", encoding="utf8") as out_file:
out_file.write(str(OUTPUT))

reply_to_id = message.reply_to_message.message_id \
if message.reply_to_message \
else message.message_id

await userge.send_document(
chat_id=message.chat.id,
document=name,
caption=cmd,
disable_notification=True,
reply_to_message_id=reply_to_id
)

os.remove(name)
await message.delete()
return cmd
128 changes: 77 additions & 51 deletions userge/plugins/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,28 @@ async def helpme(_, message):

else:
out = userge.get_help(cmd)
out_str += f"**.{cmd}**\n\n__{out}__" if out else "__command not found!__"
out_str += f"**.{cmd}**\n\n{out}" if out else "__command not found!__"

await message.edit(out_str)


@userge.on_cmd("json", about="replied msg to json")
async def jsonify(_, message):
if message.reply_to_message:
reply_to_id = message.reply_to_message.message_id
the_real_message = message.reply_to_message
the_real_message = str(message.reply_to_message) \
if message.reply_to_message \
else str(message)

if len(the_real_message) > Config.MAX_MESSAGE_LENGTH:
await userge.send_output_as_file(
output=the_real_message,
message=message,
filename="json.txt",
caption="Too Large"
)

else:
the_real_message = message
reply_to_id = message.message_id

try:
await message.edit(the_real_message)

except Exception as e:
with open("json.txt", "w+", encoding="utf8") as out_file:
out_file.write(str(the_real_message))
await userge.send_document(
chat_id=message.chat.id,
document="json.txt",
caption=str(e),
disable_notification=True,
reply_to_message_id=reply_to_id
)

os.remove("json.txt")
await message.delete()


@userge.on_cmd("all", about="to get all plugins")
async def getplugins(_, message):
Expand Down Expand Up @@ -122,26 +112,76 @@ async def getids(_, message):
await message.edit(out_str)


@userge.on_cmd("admins", about="to mention admins")
@userge.on_cmd("admins",
about="""view or mention admins in chat
Available Flags:
`--m` : mention all admins
`--mc` : only mention creator
`--id` : show ids""")
async def mentionadmins(client, message):
mentions = "πŸ›‘ **Admin List** πŸ›‘\n"
input_str = message.matches[0].group(1)
chat_id = [i for i in (message.matches[0].group(1) or '').split() if '--' not in i]
chat_id = chat_id[0] if chat_id else None
input_str = message.matches[0].group(0) or ''

if input_str is None:
input_str = message.chat.id
men_admins = '--m' in input_str
men_creator = '--mc' in input_str
show_id = '--id' in input_str

if chat_id is None:
chat_id = message.chat.id

try:
async for x in client.iter_chat_members(chat_id=input_str, filter="administrators"):
if x.status == "creator":
mentions += "\n πŸ‘‘ [{}](tg://user?id={}) `{}`".format(x.user.first_name, x.user.id, x.user.id)
async for x in client.iter_chat_members(chat_id=chat_id, filter="administrators"):
status = x.status
first_name = x.user.first_name or ''
last_name = x.user.last_name or ''
username = x.user.username or None
u_id = x.user.id

if first_name and last_name:
full_name = first_name + ' ' + last_name

elif first_name:
full_name = first_name

if x.status == "administrator":
mentions += "\n ⚜ [{}](tg://user?id={}) `{}`".format(x.user.first_name, x.user.id, x.user.id)
elif last_name:
full_name = last_name

else:
full_name = "user"

if status == "creator":
if men_admins or men_creator:
mentions += f"\n πŸ‘‘ [{full_name}](tg://user?id={u_id})"

elif username:
mentions += f"\n πŸ‘‘ [{full_name}](https://t.me/{username})"

else:
mentions += f"\n πŸ‘‘ {full_name}"

if show_id:
mentions += f" `{u_id}`"

elif status == "administrator":
if men_admins:
mentions += f"\n ⚜ [{full_name}](tg://user?id={u_id})"

elif username:
mentions += f"\n ⚜ [{full_name}](https://t.me/{username})"

else:
mentions += f"\n ⚜ {full_name}"

if show_id:
mentions += f" `{u_id}`"

except Exception as e:
mentions += " " + str(e) + "\n"
mentions += " " + str(e) + "\n"

await message.reply(mentions)
await message.reply(mentions, disable_web_page_preview=True)
await message.delete()


Expand Down Expand Up @@ -170,25 +210,11 @@ async def urban_dict(_, message):
OUTPUT = f"**Query:** `{query}`\n\n**Meaning:** __{mean[0]['def']}__\n\n**Example:**\n__{mean[0]['example']}__"

if len(OUTPUT) >= Config.MAX_MESSAGE_LENGTH:
with open("output.txt", "w+", encoding="utf8") as out_file:
out_file.write(str(OUTPUT))

reply_to_id = message.reply_to_message.message_id \
if message.reply_to_message \
else message.message_id

await userge.send_document(
chat_id=message.chat.id,
document="output.txt",
caption=query,
disable_notification=True,
reply_to_message_id=reply_to_id
await userge.send_output_as_file(
output=OUTPUT,
message=message,
caption=query
)

if os.path.exists("output.txt"):
os.remove("output.txt")

await message.delete()

else:
await message.edit(OUTPUT)
Loading

0 comments on commit 44853af

Please sign in to comment.