File size: 12,989 Bytes
631b7c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# Ultroid - UserBot
# Copyright (C) 2021-2022 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -

• `{i}fban <reply to user/userid/username>`
    FBan the person across all feds in which you are admin.

• `{i}unfban <reply to user/userid/username>`
    Un-FBan the person across all feds in which you are admin.

Specify FBan Group and Feds to exclude in the assistant.

• `{i}fstat <username/id/reply to user>`
    Collect fed stat of the person in Rose.

• `{i}fedinfo <(fedid)>`
    Collect federation info of the given fed id, or of the fed you own, from Rose.
"""
import asyncio
import os

from xteam.dB import DEVLIST
from telethon.errors.rpcerrorlist import YouBlockedUserError

from . import get_string, udB, ultroid_bot, ultroid_cmd

bot = "@MissRose_bot"


@ultroid_cmd(
    pattern="fban ?(.*)",
)
async def _(event):
    msg = await event.eor(get_string("sf_1"))
    inputt = event.pattern_match.group(1)
    if event.reply_to_msg_id:
        FBAN = (await event.get_reply_message()).sender_id
        if inputt:
            REASON = inputt
    elif inputt:
        REASON = "#ULTMassBanned"
        arg = event.text.split()
        if len(arg) == 2:
            FBAN = await event.client.parse_id(arg[1])
        elif len(arg) > 2:
            FBAN = await event.client.parse_id(arg[1])
            REASON = event.text.split(maxsplit=2)[-1]
        else:
            return await msg.edit(get_string("sf_22"))
    else:
        return await msg.edit("`Reply to a message or give some input...`")
    if FBAN in DEVLIST:
        return await msg.edit("The user is my Dev and cannot be FBanned!")

    if udB.get_key("FBAN_GROUP_ID"):
        chat = int(udB.get_key("FBAN_GROUP_ID"))
    else:
        chat = await event.get_chat()
    fedList = []
    if not fedList:
        for a in range(3):
            async with event.client.conversation("@MissRose_bot") as bot_conv:
                await bot_conv.send_message("/start")
                await asyncio.sleep(3)
                await bot_conv.send_message("/myfeds")
                await asyncio.sleep(3)
                try:
                    response = await bot_conv.get_response()
                except asyncio.exceptions.TimeoutError:
                    return await msg.edit(
                        get_string("sf_4"),
                    )
                await asyncio.sleep(3)
                if "make a file" in response.text or "Looks like" in response.text:
                    await response.click(0)
                    await asyncio.sleep(3)
                    fedfile = await bot_conv.get_response()
                    await asyncio.sleep(3)
                    if fedfile.media:
                        downloaded_file_name = await ultroid_bot.download_media(
                            fedfile,
                            "fedlist",
                        )
                        await asyncio.sleep(6)
                        file = open(downloaded_file_name, errors="ignore")
                        lines = file.readlines()
                        for line in lines:
                            try:
                                fedList.append(line[:36])
                            except BaseException:
                                pass
                    elif get_string("sf_5") in (await bot_conv.get_edit):
                        return await msg.edit(get_string("sf_6"))
                if not fedList:
                    await msg.edit(
                        f"Unable to collect FedAdminList. Retrying ({a+1}/3)...",
                    )
                else:
                    break
        else:
            await msg.edit(get_string("sf_8"))
        In = False
        tempFedId = ""
        for x in response.text:
            if x == "`":
                if In:
                    In = False
                    fedList.append(tempFedId)
                    tempFedId = ""
                else:
                    In = True
            elif In:
                tempFedId += x
    if not fedList:
        return await msg.edit(get_string("sf_9"))
    await msg.edit(f"FBaning in {len(fedList)} feds.")
    try:
        await ultroid_bot.send_message(chat, "/start")
    except BaseException:
        return await msg.edit(get_string("sf_11"))
    await asyncio.sleep(3)
    if udB.get_key("EXCLUDE_FED"):
        excludeFed = udB.get_key("EXCLUDE_FED").split(" ")
        for num, item in enumerate(excludeFed):
            excludeFed[num] = item.strip()
    exCount = 0
    for fed in fedList:
        if udB.get_key("EXCLUDE_FED") and fed in excludeFed:
            await ultroid_bot.send_message(chat, f"{fed} Excluded.")
            exCount += 1
            continue
        await event.client.send_message(chat, f"/joinfed {fed}")
        await asyncio.sleep(3)
        await event.client.send_message(chat, f"/fban {FBAN} {REASON}")
        await asyncio.sleep(3)
    try:
        os.remove("fedlist")
    except Exception as e:
        print(f"Error in removing FedAdmin file.\n{e}")
    await msg.edit(
        f"SuperFBan Completed.\nTotal Feds - {len(fedList)}.\nExcluded - {exCount}.\nAffected {len(fedList) - exCount} feds.\n#Ultroid",
    )


@ultroid_cmd(
    pattern="unfban ?(.*)",
)
async def _(event):
    msg = await event.eor(get_string("sf_15"))
    fedList = []
    if event.reply_to_msg_id:
        previous_message = await event.get_reply_message()
        if previous_message.media:
            downloaded_file_name = await ultroid_bot.download_media(
                previous_message,
                "fedlist",
            )
            file = open(downloaded_file_name, encoding="utf8")
            lines = file.readlines()
            for line in lines:
                try:
                    fedList.append(line[:36])
                except BaseException:
                    pass
            arg = event.text.split(" ", maxsplit=2)
            FBAN = arg[1]
            REASON = arg[2] if len(arg) > 2 else ""
        else:
            FBAN = previous_message.sender_id
            try:
                REASON = event.text.split(" ", maxsplit=1)[1]
            except BaseException:
                REASON = ""
            if REASON.strip() == "":
                REASON = ""
    else:
        arg = event.text.split(" ", maxsplit=2)
        if len(arg) > 2:
            try:
                FBAN = arg[1]
                REASON = arg[2]
            except BaseException:
                return await msg.edit(get_string("sf_2"))
        else:
            try:
                FBAN = arg[1]
                REASON = " #ULTMassUnBanned "
            except BaseException:
                return await msg.edit(get_string("sf_2"))
    if udB.get_key("FBAN_GROUP_ID"):
        chat = int(udB.get_key("FBAN_GROUP_ID"))
    else:
        chat = await event.get_chat()
    if not fedList:
        for a in range(3):
            async with event.client.conversation("@MissRose_bot") as bot_conv:
                await bot_conv.send_message("/start")
                await asyncio.sleep(3)
                await bot_conv.send_message("/myfeds")
                await asyncio.sleep(3)
                try:
                    response = await bot_conv.get_response()
                except asyncio.exceptions.TimeoutError:
                    return await msg.edit(
                        get_string("sf_4"),
                    )
                await asyncio.sleep(3)
                if "make a file" in response.text or "Looks like" in response.text:
                    await response.click(0)
                    await asyncio.sleep(3)
                    fedfile = await bot_conv.get_response()
                    await asyncio.sleep(3)
                    if fedfile.media:
                        downloaded_file_name = await ultroid_bot.download_media(
                            fedfile,
                            "fedlist",
                        )
                        await asyncio.sleep(6)
                        file = open(downloaded_file_name, errors="ignore")
                        lines = file.readlines()
                        for line in lines:
                            try:
                                fedList.append(line[:36])
                            except BaseException:
                                pass
                    elif get_string("sf_5") in (await bot_conv.get_edit):
                        return await msg.edit(get_string("sf_6"))
                if not fedList:
                    await msg.edit(
                        f"Unable to collect FedAdminList. Retrying ({a+1}/3)...",
                    )
                else:
                    break
        else:
            await msg.edit(get_string("sf_8"))
        In = False
        tempFedId = ""
        for x in response.text:
            if x == "`":
                if In:
                    In = False
                    fedList.append(tempFedId)
                    tempFedId = ""
                else:
                    In = True
            elif In:
                tempFedId += x
    if not fedList:
        return await msg.edit(get_string("sf_9"))
    await msg.edit(f"UnFBaning in {len(fedList)} feds.")
    try:
        await event.client.send_message(chat, "/start")
    except BaseException:
        return await msg.edit(get_string("sf_11"))
    await asyncio.sleep(3)
    if udB.get_key("EXCLUDE_FED"):
        excludeFed = udB.get_key("EXCLUDE_FED").split(" ")
        for n, m in enumerate(excludeFed):
            excludeFed[n] = excludeFed[n].strip()
    exCount = 0
    for fed in fedList:
        if udB.get_key("EXCLUDE_FED") and fed in excludeFed:
            await event.client.send_message(chat, f"{fed} Excluded.")
            exCount += 1
            continue
        await ultroid_bot.send_message(chat, f"/joinfed {fed}")
        await asyncio.sleep(3)
        await ultroid_bot.send_message(chat, f"/unfban {FBAN} {REASON}")
        await asyncio.sleep(3)
    try:
        os.remove("fedlist")
    except Exception as e:
        print(f"Error in removing FedAdmin file.\n{e}")
    await msg.edit(
        f"SuperUnFBan Completed.\nTotal Feds - {len(fedList)}.\nExcluded - {exCount}.\n Affected {len(fedList) - exCount} feds.\n#TB",
    )


@ultroid_cmd(
    pattern="fstat ?(.*)",
)
async def _(event):
    ok = await event.eor("`Checking...`")
    if event.reply_to_msg_id:
        previous_message = await event.get_reply_message()
        sysarg = str(previous_message.sender_id)
        user = f"[user](tg://user?id={sysarg})"
        if event.pattern_match.group(1):
            sysarg += f" {event.pattern_match.group(1)}"
    else:
        sysarg = event.pattern_match.group(1)
        user = sysarg
    if sysarg == "":
        await ok.edit(
            get_string("sf_17"),
        )
    else:
        async with event.client.conversation(bot) as conv:
            try:
                await conv.send_message("/start")
                await conv.get_response()
                await conv.send_message("/fedstat " + sysarg)
                audio = await conv.get_response()
                if audio.message.startswith("This command can only be used once"):
                    await ok.edit(
                        "Oops, you can use this command only once every minute!",
                    )
                elif "Looks like" in audio.text:
                    await audio.click(0)
                    await asyncio.sleep(2)
                    audio = await conv.get_response()
                    await event.client.send_file(
                        event.chat_id,
                        audio,
                        caption=f"List of feds {user} has been banned in.\n\nCollected using Ultroid.",
                        link_preview=False,
                    )
                    await ok.delete()
                else:
                    okk = await conv.get_edit()
                    await ok.edit(okk.message)
                await ultroid_bot.send_read_acknowledge(bot)
            except YouBlockedUserError:
                await ok.edit("**Error**\n `Unblock` @MissRose_Bot `and try again!")


@ultroid_cmd(
    pattern="fedinfo ?(.*)",
)
async def _(event):
    ok = await event.edit(get_string("sf_20"))
    sysarg = event.pattern_match.group(1)
    async with event.client.conversation(bot) as conv:
        try:
            await conv.send_message("/start")
            await conv.get_response()
            await conv.send_message("/fedinfo " + sysarg)
            audio = await conv.get_response()
            await event.client.send_read_acknowledge(bot)
            await ok.edit(audio.text + "\n\nFedInfo Extracted by Ultroid")
        except YouBlockedUserError:
            await ok.edit("**Error**\n `Unblock` @MissRose_Bot `and try again!")