Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit 08b7d27

Browse files
authored
Merge pull request #77 from code-rgb/release
ROGUE v2
2 parents 8c8a6a9 + 4cf3a05 commit 08b7d27

File tree

11 files changed

+180
-110
lines changed

11 files changed

+180
-110
lines changed

userge/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Config:
3434
HU_STRING_SESSION = os.environ.get("HU_STRING_SESSION", None)
3535
OWNER_ID = tuple(filter(lambda x: x, map(int, os.environ.get("OWNER_ID", "0").split())))
3636
LOG_CHANNEL_ID = int(os.environ.get("LOG_CHANNEL_ID"))
37+
AUTH_CHATS = (OWNER_ID[0], LOG_CHANNEL_ID) if OWNER_ID else (LOG_CHANNEL_ID,)
3738
DB_URI = os.environ.get("DATABASE_URL")
3839
LANG = os.environ.get("PREFERRED_LANGUAGE")
3940
DOWN_PATH = os.environ.get("DOWN_PATH")

userge/core/methods/messages/edit_message_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def edit_message_text(self, # pylint: disable=arguments-differ
8383
Raises:
8484
RPCError: In case of a Telegram RPC error.
8585
"""
86-
if text and chat_id != Config.LOG_CHANNEL_ID:
86+
if text and chat_id not in Config.AUTH_CHATS:
8787
text = secure_text(str(text))
8888
msg = await super().edit_message_text(chat_id=chat_id,
8989
message_id=message_id,

userge/core/methods/messages/send_as_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def send_as_file(self,
7070
Returns:
7171
On success, the sent Message is returned.
7272
"""
73-
if text and chat_id != Config.LOG_CHANNEL_ID:
73+
if text and chat_id not in Config.AUTH_CHATS:
7474
text = secure_text(str(text))
7575
async with aiofiles.open(filename, "w+", encoding="utf8") as out_file:
7676
await out_file.write(text)

userge/core/methods/messages/send_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def send_message(self, # pylint: disable=arguments-differ
9292
Returns:
9393
:obj:`Message`: On success, the sent text message or True is returned.
9494
"""
95-
if text and chat_id != Config.LOG_CHANNEL_ID:
95+
if text and chat_id not in Config.AUTH_CHATS:
9696
text = secure_text(str(text))
9797
msg = await super().send_message(chat_id=chat_id,
9898
text=text,

userge/plugins/bot/bot_forwards.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ async def broadcast_(_, message: Message):
215215
if len(blocked_users) != 0:
216216
b_info += f"\n\n😕 {len(blocked_users)} users blocked your bot recently"
217217
await br_cast.edit(b_info)
218+
await CHANNEL.log(b_info)
218219
if blocked_users:
219220
for buser in blocked_users:
220221
await BOT_START.find_one_and_delete({"user_id": buser})

userge/plugins/bot/buttons.py

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
""" Create Buttons Through Bots """
22

33
# IMPROVED BY code-rgb
4-
# By @krishna_singhal
54

5+
import json
66
import os
77
import re
88

9-
from html_telegraph_poster.upload_images import upload_image
109
from pyrogram.errors import BadRequest, MessageEmpty, UserIsBot
1110
from pyrogram.types import ReplyKeyboardRemove
1211

13-
from userge import Config, Message, get_collection, userge
12+
from userge import Config, Message, userge
1413
from userge.utils import get_file_id_and_ref
1514
from userge.utils import parse_buttons as pb
1615

17-
BUTTON_BASE = get_collection("TEMP_BUTTON")
1816
BTN = r"\[([^\[]+?)\](\[buttonurl:(?:/{0,2})(.+?)(:same)?\])|\[([^\[]+?)\](\(buttonurl:(?:/{0,2})(.+?)(:same)?\))"
1917
BTNX = re.compile(BTN)
18+
PATH = "./userge/xcache/inline_db.json"
19+
CHANNEL = userge.getCLogger(__name__)
20+
21+
22+
class Inline_DB:
23+
def __init__(self):
24+
if not os.path.exists(PATH):
25+
d = {}
26+
json.dump(d, open(PATH, "w"))
27+
self.db = json.load(open(PATH))
28+
29+
def save_msg(self, rnd_id: int, msg_content: str, media_valid: bool, media_id: int):
30+
self.db[rnd_id] = {
31+
"msg_content": msg_content,
32+
"media_valid": media_valid,
33+
"media_id": media_id,
34+
}
35+
self.save()
36+
37+
def save(self):
38+
with open(PATH, "w") as outfile:
39+
json.dump(self.db, outfile, indent=4)
40+
41+
42+
InlineDB = Inline_DB()
2043

2144

2245
@userge.on_cmd(
@@ -74,34 +97,41 @@ async def create_button(msg: Message):
7497
},
7598
)
7699
async def inline_buttons(message: Message):
77-
""" Create Buttons Through Inline Bots """
78-
if Config.BOT_TOKEN is None:
79-
await message.err(
80-
"First Create a Inline Bot via @Botfather to Create Buttons..."
81-
)
82-
return
83-
replied = message.reply_to_message
84-
if not (replied and (replied.text or replied.caption)):
85-
await message.err("Reply a text Msg")
86-
return
87-
await message.edit("<code>Creating an inline button...</code>")
88-
if replied.caption:
89-
text = replied.caption
90-
text = check_brackets(text)
91-
dls_loc = await down_image(message)
92-
photo_url = str(upload_image(dls_loc))
93-
BUTTON_BASE.insert_one({"msg_data": text, "photo_url": photo_url})
94-
os.remove(dls_loc)
95-
else:
96-
text = replied.text
97-
text = check_brackets(text)
98-
BUTTON_BASE.insert_one({"msg_data": text})
100+
await message.edit("<code>Creating an Inline Button...</code>")
101+
reply = message.reply_to_message
102+
msg_content = None
103+
media_valid = False
104+
media_id = 0
105+
if reply:
106+
media_valid = bool(get_file_id_and_ref(reply)[0])
107+
108+
if message.input_str:
109+
msg_content = message.input_str
110+
if media_valid:
111+
media_id = (await reply.forward(Config.LOG_CHANNEL_ID)).message_id
112+
113+
elif reply:
114+
if media_valid:
115+
media_id = (await reply.forward(Config.LOG_CHANNEL_ID)).message_id
116+
msg_content = reply.caption.html if reply.caption else None
117+
elif reply.text:
118+
msg_content = reply.text.html
119+
120+
if not msg_content:
121+
return await message.err("Content not found", del_in=5)
122+
123+
rnd_id = userge.rnd_id()
124+
msg_content = check_brackets(msg_content)
125+
InlineDB.save_msg(rnd_id, msg_content, media_valid, media_id)
126+
99127
bot = await userge.bot.get_me()
100-
x = await userge.get_inline_bot_results(bot.username, "buttonnn")
128+
129+
x = await userge.get_inline_bot_results(bot.username, f"btn_{rnd_id}")
101130
await userge.send_inline_bot_result(
102-
chat_id=message.chat.id, query_id=x.query_id, result_id=x.results[0].id
131+
chat_id=message.chat.id,
132+
query_id=x.query_id,
133+
result_id=x.results[0].id,
103134
)
104-
await BUTTON_BASE.drop()
105135
await message.delete()
106136

107137

@@ -120,16 +150,6 @@ def check_brackets(text):
120150
return text
121151

122152

123-
async def down_image(message):
124-
message.reply_to_message
125-
if not os.path.isdir(Config.DOWN_PATH):
126-
os.makedirs(Config.DOWN_PATH)
127-
dls = await userge.download_media(
128-
message=message.reply_to_message, file_name=Config.DOWN_PATH
129-
)
130-
return os.path.join(Config.DOWN_PATH, os.path.basename(dls))
131-
132-
133153
@userge.on_cmd(
134154
"noformat",
135155
about={

userge/plugins/bot/inline_help.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
"help_txt": "**Send Saved Spoiler Via Inline**\n For more info see `.help spoiler`\n\n>>> `spoiler [ID]`",
5757
"i_q": "spoiler",
5858
},
59+
"btn": {
60+
"help_txt": "**Get your 15 recently creeated inline message in the inline query, so you can post it in any channel or group effortlessly**\n For Creating inline messages see `.help .ibutton`\n\n>>> `btn`",
61+
"i_q": "btn",
62+
},
5963
}
6064

6165

userge/plugins/help.py

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
from .fun.stylish import font_gen
3535
from .misc.redditdl import reddit_thumb_link
3636

37+
INLINE_DB = {}
38+
CHANNEL = userge.getCLogger(__name__)
3739
MEDIA_TYPE, MEDIA_URL = None, None
3840
PATH = "userge/xcache"
3941
_CATEGORY = {
@@ -49,7 +51,6 @@
4951
}
5052
# Database
5153
SAVED_SETTINGS = get_collection("CONFIGS")
52-
BUTTON_BASE = get_collection("TEMP_BUTTON") # TODO use json cache
5354
REPO_X = InlineQueryResultArticle(
5455
title="Repo",
5556
input_message_content=InputTextMessageContent("**Here's how to setup USERGE-X** "),
@@ -939,28 +940,78 @@ async def inline_answer(_, inline_query: InlineQuery):
939940
)
940941
)
941942

942-
if string == "buttonnn":
943-
async for data in BUTTON_BASE.find():
944-
button_data = data["msg_data"]
945-
text, buttons = pb(button_data)
946-
try:
947-
photo_url = data["photo_url"]
948-
except KeyError:
949-
photo_url = None
950-
if photo_url:
951-
results.append(
952-
InlineQueryResultPhoto(
953-
photo_url=photo_url, caption=text, reply_markup=buttons
954-
)
955-
)
956-
else:
957-
results.append(
958-
InlineQueryResultArticle(
959-
title=text,
960-
input_message_content=InputTextMessageContent(text),
961-
reply_markup=buttons,
962-
)
963-
)
943+
if "btn_" in str_y[0] or str_y[0] == "btn":
944+
945+
inline_db_path = "./userge/xcache/inline_db.json"
946+
if os.path.exists(inline_db_path):
947+
with open(inline_db_path, "r") as data_file:
948+
view_db = json.load(data_file)
949+
950+
data_count_n = 1
951+
reverse_list = list(view_db)
952+
reverse_list.reverse()
953+
for butt_ons in reverse_list:
954+
if data_count_n > 15:
955+
view_db.pop(butt_ons, None)
956+
data_count_n += 1
957+
958+
with open(inline_db_path, "w") as data_file:
959+
json.dump(view_db, data_file)
960+
961+
if str_y[0] == "btn":
962+
inline_storage = list(view_db)
963+
else:
964+
rnd_id = (str_y[0].split("_", 1))[1]
965+
inline_storage = [rnd_id]
966+
967+
if len(inline_storage) == 0:
968+
return
969+
970+
for inline_content in inline_storage:
971+
inline_db = view_db.get(inline_content, None)
972+
if inline_db:
973+
if (
974+
inline_db["media_valid"]
975+
and int(inline_db["media_id"]) != 0
976+
):
977+
saved_msg = await userge.bot.get_messages(
978+
Config.LOG_CHANNEL_ID, int(inline_db["media_id"])
979+
)
980+
media_data = get_file_id_and_ref(saved_msg)
981+
982+
textx, buttonsx = pb(inline_db["msg_content"])
983+
984+
if inline_db["media_valid"]:
985+
if saved_msg.photo:
986+
results.append(
987+
InlineQueryResultCachedPhoto(
988+
file_id=media_data[0],
989+
file_ref=media_data[1],
990+
caption=textx,
991+
reply_markup=buttonsx,
992+
)
993+
)
994+
else:
995+
results.append(
996+
InlineQueryResultCachedDocument(
997+
title=textx,
998+
file_id=media_data[0],
999+
file_ref=media_data[1],
1000+
caption=textx,
1001+
description="Inline Button",
1002+
reply_markup=buttonsx,
1003+
)
1004+
)
1005+
else:
1006+
results.append(
1007+
InlineQueryResultArticle(
1008+
title=textx,
1009+
input_message_content=InputTextMessageContent(
1010+
textx
1011+
),
1012+
reply_markup=buttonsx,
1013+
)
1014+
)
9641015

9651016
if str_y[0].lower() == "stylish":
9661017
if len(str_y) == 2:

userge/plugins/misc/gdrive.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def _download_file(self, path: str, name: str, **kwargs) -> None:
396396
d_file_obj = MediaIoBaseDownload(d_f, request, chunksize=50 * 1024 * 1024)
397397
c_time = time.time()
398398
done = False
399-
while done is False:
399+
while not done:
400400
status, done = d_file_obj.next_chunk(num_retries=5)
401401
if self._is_canceled:
402402
raise ProcessCanceled
@@ -756,12 +756,11 @@ def _get_file_id(self, filter_str: bool = False) -> tuple:
756756
link = self._message.filtered_input_str
757757
found = _GDRIVE_ID.search(link)
758758
if found and "folder" in link:
759-
out = (found.group(1), "folder")
759+
return found.group(1), "folder"
760760
elif found:
761-
out = (found.group(1), "file")
761+
return found.group(1), "file"
762762
else:
763-
out = (link, "unknown")
764-
return out
763+
return link, "unknown"
765764

766765
async def setup(self) -> None:
767766
""" Setup GDrive """
@@ -933,7 +932,7 @@ async def upload(self) -> None:
933932
except Exception as e_e:
934933
await self._message.err(e_e)
935934
return
936-
file_path = dl_loc if dl_loc else self._message.input_str
935+
file_path = dl_loc or self._message.input_str
937936
if not os.path.exists(file_path):
938937
await self._message.err("invalid file path provided?")
939938
return
@@ -964,7 +963,7 @@ async def upload(self) -> None:
964963
out = f"**ERROR** : `{self._output._get_reason()}`" # pylint: disable=protected-access
965964
elif self._output is not None and not self._is_canceled:
966965
out = f"**Uploaded Successfully** __in {m_s} seconds__\n\n{self._output}"
967-
elif self._output is not None and self._is_canceled:
966+
elif self._output is not None:
968967
out = self._output
969968
else:
970969
out = "`failed to upload.. check logs?`"
@@ -994,7 +993,7 @@ async def download(self) -> None:
994993
out = (
995994
f"**Downloaded Successfully** __in {m_s} seconds__\n\n`{self._output}`"
996995
)
997-
elif self._output is not None and self._is_canceled:
996+
elif self._output is not None:
998997
out = self._output
999998
else:
1000999
out = "`failed to download.. check logs?`"
@@ -1025,7 +1024,7 @@ async def copy(self) -> None:
10251024
out = f"**ERROR** : `{self._output._get_reason()}`" # pylint: disable=protected-access
10261025
elif self._output is not None and not self._is_canceled:
10271026
out = f"**Copied Successfully** __in {m_s} seconds__\n\n{self._output}"
1028-
elif self._output is not None and self._is_canceled:
1027+
elif self._output is not None:
10291028
out = self._output
10301029
else:
10311030
out = "`failed to copy.. check logs?`"
@@ -1159,13 +1158,7 @@ async def del_perms(self) -> None:
11591158
)
11601159

11611160

1162-
@userge.on_cmd(
1163-
"gsetup",
1164-
about={"header": "Setup GDrive Creds"},
1165-
allow_groups=False,
1166-
allow_private=False,
1167-
allow_bots=False,
1168-
)
1161+
@userge.on_cmd("gsetup", about={"header": "Setup GDrive Creds"})
11691162
async def gsetup_(message: Message):
11701163
""" setup creds """
11711164
await Worker(message).setup()

0 commit comments

Comments
 (0)