Skip to content

Commit 606f445

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent d9567f8 commit 606f445

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+172
-265
lines changed

dbplugins/blacklist.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ async def on_delete_blacklist(event):
7575
to_unblacklist = list(
7676
{trigger.strip() for trigger in text.split("\n") if trigger.strip()}
7777
)
78-
successful = 0
79-
for trigger in to_unblacklist:
80-
if sql.rm_from_blacklist(event.chat_id, trigger.lower()):
81-
successful += 1
78+
successful = sum(
79+
bool(sql.rm_from_blacklist(event.chat_id, trigger.lower()))
80+
for trigger in to_unblacklist
81+
)
82+
8283
await event.edit(f"Removed {successful} / {len(to_unblacklist)} from the blacklist")

dbplugins/filters.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ async def on_snip(event):
3333
# avoid userbot spam
3434
# "I demand rights for us bots, we are equal to you humans." -Henri Koivuneva (t.me/UserbotTesting/2698)
3535
return False
36-
snips = get_all_filters(event.chat_id)
37-
if snips:
36+
if snips := get_all_filters(event.chat_id):
3837
for snip in snips:
3938
pattern = r"( |^|[^\w])" + re.escape(snip.keyword) + r"( |$|[^\w])"
4039
if re.search(pattern, name, flags=re.IGNORECASE):
4140
msg_o = await event.client.get_messages(
4241
entity=Config.PRIVATE_CHANNEL_BOT_API_ID,
4342
ids=int(snip.f_mesg_id)
4443
)
45-
message_id = event.message.id
46-
if event.reply_to_msg_id:
47-
message_id = event.reply_to_msg_id
44+
message_id = event.reply_to_msg_id or event.message.id
4845
await event.client.send_message(
4946
event.chat_id,
5047
msg_o.message,
@@ -110,4 +107,4 @@ async def on_snip_delete(event):
110107
@borg.on(slitu.admin_cmd(pattern="clearallfilters"))
111108
async def on_all_snip_delete(event):
112109
remove_all_filters(event.chat_id)
113-
await event.edit(f"filters **in current chat** deleted successfully")
110+
await event.edit('filters **in current chat** deleted successfully')

dbplugins/locks.py

+33-40
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,15 @@ async def _(event):
120120
logger.info(str(e))
121121
return False
122122
res = ""
123-
current_db_locks = get_locks(event.chat_id)
124-
if not current_db_locks:
125-
res = "There are no DataBase locks in this chat"
126-
else:
123+
if current_db_locks := get_locks(event.chat_id):
127124
res = "Following are the DataBase locks in this chat: \n"
128125
res += "👉 `bots`: `{}`\n".format(current_db_locks.bots)
129126
res += "👉 `commands`: `{}`\n".format(current_db_locks.commands)
130127
res += "👉 `email`: `{}`\n".format(current_db_locks.email)
131128
res += "👉 `forward`: `{}`\n".format(current_db_locks.forward)
132129
res += "👉 `url`: `{}`\n".format(current_db_locks.url)
130+
else:
131+
res = "There are no DataBase locks in this chat"
133132
current_chat = await event.get_chat()
134133
try:
135134
current_api_locks = current_chat.default_banned_rights
@@ -163,9 +162,8 @@ async def check_incoming_messages(event):
163162
return
164163
peer_id = event.chat_id
165164
if is_locked(peer_id, "commands"):
166-
entities = event.message.entities
167165
is_command = False
168-
if entities:
166+
if entities := event.message.entities:
169167
for entity in entities:
170168
if isinstance(entity, types.MessageEntityBotCommand):
171169
is_command = True
@@ -186,9 +184,8 @@ async def check_incoming_messages(event):
186184
)
187185
update_lock(peer_id, "forward", False)
188186
if is_locked(peer_id, "email"):
189-
entities = event.message.entities
190187
is_email = False
191-
if entities:
188+
if entities := event.message.entities:
192189
for entity in entities:
193190
if isinstance(entity, types.MessageEntityEmail):
194191
is_email = True
@@ -201,9 +198,8 @@ async def check_incoming_messages(event):
201198
)
202199
update_lock(peer_id, "email", False)
203200
if is_locked(peer_id, "url"):
204-
entities = event.message.entities
205201
is_url = False
206-
if entities:
202+
if entities := event.message.entities:
207203
for entity in entities:
208204
if isinstance(entity, (types.MessageEntityTextUrl, types.MessageEntityUrl)):
209205
is_url = True
@@ -227,34 +223,31 @@ async def _(event):
227223
return False
228224
if await slitu.is_admin(event.client, event.chat_id, event.action_message.sender_id):
229225
return
230-
if is_locked(event.chat_id, "bots"):
231-
# bots are limited Telegram accounts,
232-
# and cannot join by themselves
233-
if event.user_added:
234-
users_added_by = event.action_message.sender_id
235-
is_ban_able = False
236-
rights = types.ChatBannedRights(
237-
until_date=None,
238-
view_messages=True
226+
if is_locked(event.chat_id, "bots") and event.user_added:
227+
users_added_by = event.action_message.sender_id
228+
is_ban_able = False
229+
rights = types.ChatBannedRights(
230+
until_date=None,
231+
view_messages=True
232+
)
233+
added_users = event.action_message.action.users
234+
for user_id in added_users:
235+
user_obj = await event.client.get_entity(user_id)
236+
if user_obj.bot:
237+
is_ban_able = True
238+
try:
239+
await event.client(functions.channels.EditBannedRequest(
240+
event.chat_id,
241+
user_obj,
242+
rights
243+
))
244+
except Exception as e:
245+
await event.reply(
246+
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
247+
)
248+
update_lock(event.chat_id, "bots", False)
249+
break
250+
if Config.G_BAN_LOGGER_GROUP is not None and is_ban_able:
251+
ban_reason_msg = await event.reply(
252+
"!warn [user](tg://user?id={}) Please Do Not Add BOTs to this chat.".format(users_added_by)
239253
)
240-
added_users = event.action_message.action.users
241-
for user_id in added_users:
242-
user_obj = await event.client.get_entity(user_id)
243-
if user_obj.bot:
244-
is_ban_able = True
245-
try:
246-
await event.client(functions.channels.EditBannedRequest(
247-
event.chat_id,
248-
user_obj,
249-
rights
250-
))
251-
except Exception as e:
252-
await event.reply(
253-
"I don't seem to have ADMIN permission here. \n`{}`".format(str(e))
254-
)
255-
update_lock(event.chat_id, "bots", False)
256-
break
257-
if Config.G_BAN_LOGGER_GROUP is not None and is_ban_able:
258-
ban_reason_msg = await event.reply(
259-
"!warn [user](tg://user?id={}) Please Do Not Add BOTs to this chat.".format(users_added_by)
260-
)

dbplugins/snip.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
@borg.on(slitu.admin_cmd(pattern=r'\#(\S+)', outgoing=True))
1515
async def on_snip(event):
1616
name = event.pattern_match.group(1)
17-
snip = get_snips(name)
18-
if snip:
17+
if snip := get_snips(name):
1918
msg_o = await event.client.get_messages(
2019
entity=Config.PRIVATE_CHANNEL_BOT_API_ID,
2120
ids=int(snip.f_mesg_id)
2221
)
23-
message_id = event.message.id
24-
if event.reply_to_msg_id:
25-
message_id = event.reply_to_msg_id
22+
message_id = event.reply_to_msg_id or event.message.id
2623
media_message = msg_o.media
2724
if isinstance(media_message, types.MessageMediaWebPage):
2825
media_message = None

dbplugins/welcome.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
@borg.on(events.ChatAction()) # pylint:disable=E0602
1212
async def _(event):
13-
cws = get_current_welcome_settings(event.chat_id)
14-
if cws:
13+
if cws := get_current_welcome_settings(event.chat_id):
1514
# logger.info(event.stringify())
1615
"""user_added=False,
1716
user_joined=True,

sql_helpers/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def start() -> scoped_session:
2121
except AttributeError as e:
2222
# this is a dirty way for the work-around required for #23
2323
print("DB_URI is not configured. Features depending on the database might have issues.")
24-
print(str(e))
24+
print(e)
2525

sql_helpers/antiflood_sql.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def get_flood_limit(chat_id):
7474

7575
def migrate_chat(old_chat_id, new_chat_id):
7676
with INSERTION_LOCK:
77-
flood = SESSION.query(FloodControl).get(str(old_chat_id))
78-
if flood:
77+
if flood := SESSION.query(FloodControl).get(str(old_chat_id)):
7978
CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get(str(old_chat_id), DEF_OBJ)
8079
flood.chat_id = str(new_chat_id)
8180
SESSION.commit()

stdplugins/_help.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ async def _(event):
4040
help_string += f"<b>Total Disk Space</b>: <code>{total}</code>\n"
4141
help_string += f"<b>Used Disk Space</b>: <code>{used}</code>\n"
4242
help_string += f"<b>Free Disk Space</b>: <code>{free}</code>\n\n"
43-
help_string += f"UserBot Forked from https://github.com/udf/uniborg"
44-
borg._iiqsixfourstore[str(event.chat_id)] = {}
45-
borg._iiqsixfourstore[
46-
str(event.chat_id)
47-
][
48-
str(event.id)
49-
] = help_string + "\n\n" + s_help_string
43+
help_string += 'UserBot Forked from https://github.com/udf/uniborg'
44+
borg._iiqsixfourstore[str(event.chat_id)] = {
45+
str(event.id): help_string + "\n\n" + s_help_string
46+
}
47+
5048
if borg.tgbot:
5149
tgbot_username = await tgbot.get_me()
5250
results = await borg.inline_query( # pylint:disable=E0602
@@ -138,7 +136,7 @@ def check_data_base_heal_th():
138136
# to check database we will execute raw query
139137
SESSION.execute("SELECT 1")
140138
except Exception as e:
141-
output = f"{str(e)}"
139+
output = f'{e}'
142140
is_database_working = False
143141
else:
144142
output = "✅"

stdplugins/barcode.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ async def _(event):
3333
m_list = None
3434
with open(downloaded_file_name, "rb") as fd:
3535
m_list = fd.readlines()
36-
message = ""
37-
for m in m_list:
38-
message += m.decode("UTF-8") + "\r\n"
36+
message = "".join(m.decode("UTF-8") + "\r\n" for m in m_list)
3937
os.remove(downloaded_file_name)
4038
else:
4139
message = previous_message.message

stdplugins/chat_file_s_count_er.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ async def _(event):
77
if event.fwd_from:
88
return
99
entity = event.chat_id
10-
input_str = event.pattern_match.group(1)
11-
if input_str:
10+
if input_str := event.pattern_match.group(1):
1211
entity = input_str
1312
status_message = await event.reply(
1413
"... this might take some time "
@@ -25,9 +24,7 @@ async def _(event):
2524
if message.file.mime_type not in hmm:
2625
hmm[message.file.mime_type] = 0
2726
hmm[message.file.mime_type] += message.file.size
28-
hnm = {}
29-
for key in hmm:
30-
hnm[key] = slitu.humanbytes(hmm[key])
27+
hnm = {key: slitu.humanbytes(hmm[key]) for key in hmm}
3128
await status_message.edit(
3229
slitu.yaml_format(hnm),
3330
parse_mode=slitu.parse_pre

stdplugins/checkrestrictionsbot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_restriction_string(a) -> str:
3434
b = ""
3535
c = ""
3636
if isinstance(a, Channel):
37-
c = f"[{a.title}](https://t.me/c/{a.id}/{2})"
37+
c = f'[{a.title}](https://t.me/c/{a.id}/2)'
3838
elif isinstance(a, User):
3939
c = f"[{a.first_name}](tg://user?id={a.id})"
4040
elif isinstance(a, Chat):

stdplugins/colors.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ async def _(event):
99
if event.fwd_from:
1010
return
1111
input_str = event.pattern_match.group(1)
12-
message_id = event.message.id
13-
if event.reply_to_msg_id:
14-
message_id = event.reply_to_msg_id
12+
message_id = event.reply_to_msg_id or event.message.id
1513
if input_str.startswith("#"):
1614
try:
1715
usercolor = ImageColor.getrgb(input_str)

stdplugins/count.py

+25-31
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""Type `.count` and see Magic."""
1111

1212
@borg.on(slitu.admin_cmd(pattern='count'))
13-
async def stats(event: NewMessage.Event) -> None: # pylint: disable = R0912, R0914, R0915
13+
async def stats(event: NewMessage.Event) -> None: # pylint: disable = R0912, R0914, R0915
1414
"""Command to get stats about the account"""
1515
waiting_message = await event.edit('`Collecting stats, Wait Nibba`')
1616
start_time = time.time()
@@ -30,38 +30,35 @@ async def stats(event: NewMessage.Event) -> None: # pylint: disable = R0912, R0
3030
async for dialog in event.client.iter_dialogs():
3131
entity = dialog.entity
3232

33-
if isinstance(entity, Channel):
34-
# participants_count = (await event.get_participants(dialog, limit=0)).total
35-
if entity.broadcast:
36-
broadcast_channels += 1
37-
if entity.creator or entity.admin_rights:
38-
admin_in_broadcast_channels += 1
39-
if entity.creator:
40-
creator_in_channels += 1
41-
42-
elif entity.megagroup:
43-
groups += 1
44-
# if participants_count > largest_group_member_count:
45-
# largest_group_member_count = participants_count
46-
if entity.creator or entity.admin_rights:
47-
# if participants_count > largest_group_with_admin:
48-
# largest_group_with_admin = participants_count
49-
admin_in_groups += 1
50-
if entity.creator:
51-
creator_in_groups += 1
52-
53-
elif isinstance(entity, User):
54-
private_chats += 1
55-
if entity.bot:
56-
bots += 1
57-
58-
elif isinstance(entity, Chat):
33+
if isinstance(entity, Channel) and entity.broadcast:
34+
broadcast_channels += 1
35+
if entity.creator or entity.admin_rights:
36+
admin_in_broadcast_channels += 1
37+
if entity.creator:
38+
creator_in_channels += 1
39+
40+
elif (
41+
isinstance(entity, Channel)
42+
and entity.megagroup
43+
or not isinstance(entity, Channel)
44+
and not isinstance(entity, User)
45+
and isinstance(entity, Chat)
46+
):
5947
groups += 1
48+
# if participants_count > largest_group_member_count:
49+
# largest_group_member_count = participants_count
6050
if entity.creator or entity.admin_rights:
51+
# if participants_count > largest_group_with_admin:
52+
# largest_group_with_admin = participants_count
6153
admin_in_groups += 1
6254
if entity.creator:
6355
creator_in_groups += 1
6456

57+
elif not isinstance(entity, Channel) and isinstance(entity, User):
58+
private_chats += 1
59+
if entity.bot:
60+
bots += 1
61+
6562
unread_mentions += dialog.unread_mentions_count
6663
unread += dialog.unread_count
6764
stop_time = time.time() - start_time
@@ -87,10 +84,7 @@ async def stats(event: NewMessage.Event) -> None: # pylint: disable = R0912, R0
8784

8885

8986
def make_mention(user):
90-
if user.username:
91-
return f"@{user.username}"
92-
else:
93-
return inline_mention(user)
87+
return f"@{user.username}" if user.username else inline_mention(user)
9488

9589

9690
def inline_mention(user):

stdplugins/dagd.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ async def _(event):
1616
return
1717
input_str = event.pattern_match.group(1)
1818
sample_url = "https://da.gd/dns/{}".format(input_str)
19-
response_api = requests.get(sample_url).text
20-
if response_api:
19+
if response_api := requests.get(sample_url).text:
2120
await event.edit("DNS records of {} are \n{}".format(input_str, response_api))
2221
else:
2322
await event.edit("i can't seem to find {} on the internet".format(input_str))
@@ -29,8 +28,7 @@ async def _(event):
2928
return
3029
input_str = event.pattern_match.group(1)
3130
sample_url = "https://da.gd/s?url={}".format(input_str)
32-
response_api = requests.get(sample_url).text
33-
if response_api:
31+
if response_api := requests.get(sample_url).text:
3432
await event.edit("Generated {} for {}.".format(response_api, input_str))
3533
else:
3634
await event.edit("something is wrong. please try again later.")

stdplugins/decide.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
async def _(event):
88
if event.fwd_from:
99
return
10-
message_id = event.message.id
11-
if event.reply_to_msg_id:
12-
message_id = event.reply_to_msg_id
10+
message_id = event.reply_to_msg_id or event.message.id
1311
r = requests.get("https://yesno.wtf/api").json()
1412
await borg.send_message(
1513
event.chat_id,

0 commit comments

Comments
 (0)