File size: 18,077 Bytes
5e876b6
c31d06c
98ab4e9
1b5719b
 
 
98ab4e9
eef97a7
9ab31c1
1b5719b
 
 
 
 
 
 
c1dc926
98ab4e9
1b5719b
bb430b4
e64dc9b
bb430b4
7956eb0
 
 
 
 
b002286
 
 
7956eb0
 
 
 
 
 
 
0206afd
7956eb0
 
0206afd
7956eb0
 
 
 
 
 
 
0206afd
7956eb0
 
 
 
 
 
 
 
0206afd
7956eb0
 
 
 
 
 
c1dc926
3580517
7956eb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056428
7956eb0
 
 
 
ba83449
 
 
7956eb0
89c3556
7956eb0
 
 
5e49d57
7956eb0
 
89c3556
7956eb0
b002286
 
ba83449
5e49d57
4056428
5e49d57
 
ba83449
 
 
 
3c9449b
7956eb0
 
ba83449
4056428
3c9449b
7956eb0
89c3556
7956eb0
89c3556
7956eb0
ba83449
7956eb0
d720c41
7956eb0
 
 
 
5e49d57
7956eb0
 
 
ba83449
7956eb0
 
 
 
 
9be7ff3
d7e1221
 
 
 
 
 
 
4056428
 
d7e1221
7956eb0
d7e1221
 
 
 
 
7956eb0
 
 
 
 
d720c41
 
 
 
7956eb0
89c3556
d7e1221
0206afd
7956eb0
 
 
 
 
b002286
 
 
 
7956eb0
 
 
 
0206afd
7956eb0
711b749
4056428
78d4936
 
4056428
78d4936
4056428
d720c41
7956eb0
4056428
d720c41
7956eb0
 
963774e
7956eb0
 
 
 
 
 
 
0206afd
7956eb0
 
89c3556
7956eb0
 
 
 
52af1ce
7956eb0
52af1ce
 
 
 
3c9449b
52af1ce
d720c41
52af1ce
239f614
0319c8e
d720c41
2707517
239f614
d720c41
 
4056428
 
d720c41
0319c8e
a1848f9
3c05ff4
4056428
3c05ff4
 
 
52af1ce
 
 
 
 
 
 
4056428
a1848f9
 
4056428
eb73660
 
9830b39
 
a1848f9
9729818
a1848f9
9729818
 
 
 
a1848f9
9729818
a1848f9
 
9729818
eb73660
03941af
da6c977
9729818
eb73660
9729818
a1848f9
9729818
d720c41
 
 
 
aa9c445
d720c41
0a08e1c
3c05ff4
 
 
 
 
 
f5602f8
 
 
52af1ce
239f614
b002286
239f614
5e876b6
 
 
 
 
63f642d
4056428
8367e56
 
 
0c06d08
0bcf3b4
49bf1cd
5185539
7956eb0
 
239f614
 
cbc9e11
647d44c
bb430b4
43ff66e
 
bb430b4
43ff66e
 
647d44c
43ff66e
 
5ca983b
5e876b6
43ff66e
9ab31c1
 
7956eb0
43ff66e
 
7956eb0
43ff66e
4056428
43ff66e
d720c41
5ca983b
239f614
43ff66e
8367e56
d0b1f31
 
 
8367e56
d720c41
d0b1f31
d720c41
3c9449b
624601b
7956eb0
affb278
17d2cd0
624601b
3c9449b
17d2cd0
bb430b4
7956eb0
 
239f614
7956eb0
b002286
4056428
b002286
239f614
7956eb0
 
239f614
 
 
7956eb0
239f614
7956eb0
239f614
 
 
7956eb0
 
239f614
 
7956eb0
 
 
 
239f614
7956eb0
239f614
7956eb0
 
 
4056428
d7e1221
 
 
 
 
 
 
 
 
 
 
 
4056428
 
d7e1221
4056428
d7e1221
 
4056428
d7e1221
c96448f
4056428
d7e1221
4056428
d7e1221
7956eb0
9ab31c1
 
 
 
 
 
 
 
c1dc926
9ab31c1
 
 
 
 
 
 
 
 
 
 
c1dc926
9ab31c1
3c9449b
9ab31c1
 
 
 
 
 
 
 
 
 
c1dc926
9ab31c1
 
c1dc926
9ab31c1
 
 
 
 
 
 
 
 
 
7956eb0
d7e1221
7956eb0
818d601
d76971a
809532c
d76971a
 
818d601
2707517
 
5e49d57
2707517
 
 
 
d7e1221
9ab31c1
818d601
 
2707517
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import json
import re
from io import BytesIO
from os import remove

import aiofiles
from gpytranslate import Translator
from pyrogram import enums, filters
from pyrogram.errors import MessageTooLong, PeerIdInvalid, RPCError
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
from wikipedia import summary
from wikipedia.exceptions import DisambiguationError, PageError

from Powers import *
from Powers.bot_class import Gojo
from Powers.database.users_db import Users
from Powers.supports import get_support_staff
from Powers.utils.clean_file import remove_markdown_and_html
from Powers.utils.custom_filters import command
from Powers.utils.extract_user import extract_user
from Powers.utils.http_helper import *
from Powers.utils.parser import mention_html


@Gojo.on_message(command("wiki"))
async def wiki(_, m: Message):
    if len(m.text.split()) <= 1:
        return await m.reply_text(
            text="Please check help on how to use this this command."
        )

    search = m.text.split(None, 1)[1]
    try:
        res = summary(search)
    except DisambiguationError as de:
        return await m.reply_text(
            f"Disambiguated pages found! Adjust your query accordingly.\n<i>{de}</i>",
            parse_mode=enums.ParseMode.HTML,
        )
    except PageError as pe:
        return await m.reply_text(f"<code>{pe}</code>", parse_mode=enums.ParseMode.HTML)
    if res:
        result = f"<b>{search}</b>\n\n"
        result += f"<i>{res}</i>\n"
        result += f"""<a href="https://en.wikipedia.org/wiki/{search.replace(" ", "%20")}">Read more...</a>"""
        try:
            return await m.reply_text(
                result,
                parse_mode=enums.ParseMode.HTML,
                disable_web_page_preview=True,
            )
        except MessageTooLong:
            with BytesIO(str.encode(await remove_markdown_and_html(result))) as f:
                f.name = "result.txt"
                return await m.reply_document(
                    document=f,
                    quote=True,
                    parse_mode=enums.ParseMode.HTML,
                )
    await m.stop_propagation()


@Gojo.on_message(command("gdpr"))
async def gdpr_remove(_, m: Message):
    supports = get_support_staff()
    if m.from_user.id in supports:
        await m.reply_text(
            "You're in my support staff, I cannot do that unless you are no longer a part of it!",
        )
        return

    Users(m.from_user.id).delete_user()
    await m.reply_text(
        "Your personal data has been deleted.\n"
        "Note that this will not unban you from any chats, as that is telegram data, not Gojo data."
        " Flooding, warns, and gbans are also preserved, as of "
        "[this](https://ico.org.uk/for-organisations/guide-to-the-general-data-protection-regulation-gdpr/individual-rights/right-to-erasure/),"
        " which clearly states that the right to erasure does not apply 'for the performance of a task carried out in the public interest', "
        "as is the case for the aforementioned pieces of data.",
        disable_web_page_preview=True,
    )
    await m.stop_propagation()


@Gojo.on_message(
    command("lyrics") & (filters.group | filters.private),
)
async def get_lyrics(_, m: Message):
    if not is_genius_lyrics:
        await m.reply_text("Genius lyrics api is missing")
        return
    if len(m.text.split()) <= 1:
        await m.reply_text(text="Please check help on how to use this this command.")
        return

    query = m.text.split(None, 1)[1]
    artist = m.text.split("-")[-1].strip()
    song = ""
    if not query:
        await m.edit_text(text="You haven't specified which song to look for!")
        return
    song_name = query
    em = await m.reply_text(text=f"Finding lyrics for <code>{song_name}<code>...")
    try:
        if artist:
            song = genius_lyrics.search_song(query, artist)
        else:
            song = genius_lyrics.search_song(query)
    except Exception as e:
        await em.delete()
        await m.reply_text("Connection error try again after sometime")
        return

    if song:
        if song.lyrics:
            reply = song.lyrics
            reply = reply.split("\n", 1)[1]
            artist = artist or song.artist
        else:
            reply = "Couldn't find any lyrics for that song!"
    else:
        reply = "Song not found!"
    try:
        await em.edit_text(f"**{query.capitalize()} by {artist}**\n`{reply}`")
    except MessageTooLong:
        header = f"{query.capitalize()} by {artist}"
        with BytesIO(str.encode(await remove_markdown_and_html(reply))) as f:
            f.name = "lyrics.txt"
            await m.reply_document(
                document=f,
                caption=header
            )
        await em.delete()
    return


@Gojo.on_message(
    command("id") & (filters.group | filters.private),
)
async def id_info(c: Gojo, m: Message):
    ChatType = enums.ChatType
    user_id, _, _ = await extract_user(c, m)
    try:
        if user_id and len(m.text.split()) == 2:
            txt = f"Given user's id: <code>{user_id}</code>"
            await m.reply_text(txt, parse_mode=enums.ParseMode.HTML)
            return
        elif m.chat.type in [ChatType.SUPERGROUP, ChatType.GROUP] and not m.reply_to_message:
            await m.reply_text(
                text=f"This Group's ID is <code>{m.chat.id}</code>\nYour ID <code>{m.from_user.id}</code>")
            return

        elif m.chat.type == ChatType.PRIVATE and not m.reply_to_message:
            await m.reply_text(text=f"Your ID is <code>{m.chat.id}</code>.")
            return
    except Exception as e:
        await m.reply_text(e)
        return
    if user_id:
        if m.reply_to_message and m.reply_to_message.forward_from:
            user1 = m.reply_to_message.from_user
            user2 = m.reply_to_message.forward_from
            orig_sender = await mention_html(user2.first_name, user2.id)
            orig_id = f"<code>{user2.id}</code>"
            fwd_sender = await mention_html(user1.first_name, user1.id)
            fwd_id = f"<code>{user1.id}</code>"
            await m.reply_text(
                text=f"""Original Sender - {orig_sender} (<code>{orig_id}</code>)
Forwarder - {fwd_sender} (<code>{fwd_id}</code>)""",
                parse_mode=enums.ParseMode.HTML,
            )
        else:
            try:
                user = await c.get_users(user_id)
            except PeerIdInvalid:
                await m.reply_text(
                    text="""Failed to get user
      Peer ID invalid, I haven't seen this user anywhere earlier, maybe username would help to know them!"""
                )
                return

            await m.reply_text(
                f"{(await mention_html(user.first_name, user.id))}'s ID is <code>{user.id}</code>.",
                parse_mode=enums.ParseMode.HTML,
            )
    elif m.chat.type == ChatType.PRIVATE:
        text = f"Your ID is <code>{m.chat.id}</code>."
        if m.reply_to_message:
            if m.forward_from:
                text += f"Forwarded from user ID <code>{m.forward_from.id}</code>."
            elif m.forward_from_chat:
                text += f"Forwarded from user ID <code>{m.forward_from_chat.id}</code>."
        await m.reply_text(text)
    else:
        text = f"Chat ID <code>{m.chat.id}</code>\nYour ID <code>{m.from_user.id}</code>"
        await m.reply_text(text)
    return


@Gojo.on_message(
    command("gifid") & (filters.group | filters.private),
)
async def get_gifid(_, m: Message):
    if m.reply_to_message and m.reply_to_message.animation:
        await m.reply_text(
            f"Gif ID:\n<code>{m.reply_to_message.animation.file_id}</code>",
            parse_mode=enums.ParseMode.HTML,
        )
    else:
        await m.reply_text(text="Please reply to a gif to get it's ID.")
    return


@Gojo.on_message(
    command(["github", "git"]) & (filters.group | filters.private),
)
async def github(_, m: Message):
    if len(m.text.split()) == 2:
        username = m.text.split(maxsplit=1)[1]
    else:
        await m.reply_text("Usage: <code>/github username</code>")
        return
    username = username.split("/")[-1].strip("@")
    URL = f"https://api.github.com/users/{username}"
    try:
        r = resp_get(URL, timeout=5)
    except requests.exceptions.ConnectTimeout:
        return await m.reply_text("request timeout")
    except Exception as e:
        return await m.reply_text(f"ERROR:\n`{e}`")
    if r.status_code != 200:
        await m.reply_text(
            f"{username} this user is not available on github\nMake sure you have given correct username")
        return
    r = r.json()
    avtar = r.get("avatar_url", None)
    if avtar:
        avtar = avtar.rsplit("=", 1)
        avtar.pop(-1)
        avtar.append("5")
        avtar = "=".join(avtar)
    url = r.get("html_url", None)
    name = r.get("name", None)
    company = r.get("company", None)
    followers = r.get("followers", 0)
    following = r.get("following", 0)
    public_repos = r.get("public_repos", 0)
    bio = r.get("bio", None)
    created_at = r.get("created_at", "NA").replace("T", " ").replace("Z", "")
    location = r.get("location", None)
    email = r.get("email", None)
    updated_at = r.get("updated_at", "NA").replace("T", " ").replace("Z", "")
    blog = r.get("blog", None)
    twitter = r.get("twitter_username", None)

    REPLY = ""
    if name:
        REPLY += f"<b>πŸ§‘β€πŸ’» GitHub Info of {name}:</b>"
    if url:
        REPLY += f"\n<b>πŸ“Ž URL:</b> <a href='{url}'>{username}</a>"
    REPLY += f"\n<b>πŸ”‘ Public Repos:</b> {public_repos}"
    REPLY += f"\n<b>🧲 Followers:</b> {followers}"
    REPLY += f"\n<b>✨ Following:</b> {following}"
    if email:
        REPLY += f"\n<b>βœ‰οΈ Email:</b> <code>{email}</code>"
    if company:
        org_url = company.strip("@")
        REPLY += f"\n<b>ℒ️ Organization:</b> <a href='https://github.com/{org_url}'>{company}</a>"
    if blog:
        bname = blog.split(".")[-2]
        bname = bname.split("/")[-1]
        REPLY += f"\n<b>πŸ“ Blog:</b> <a href={blog}>{bname}</a>"
    if twitter:
        REPLY += f"\n<b>⚜️ Twitter:</b> <a href='https://twitter.com/{twitter}'>{twitter}</a>"
    if location:
        REPLY += f"\n<b>πŸš€ Location:</b> <code>{location}</code>"
    if created_at != "NA":
        REPLY += f"\n<b>πŸ’« Created at:</b> <code>{created_at}</code>"
    if updated_at != "NA":
        REPLY += f"\n<b>⌚️ Updated at:</b> <code>{updated_at}</code>"
    if bio:
        REPLY += f"\n\n<b>🎯 Bio:</b> {bio}"

    kb = InlineKeyboardMarkup(
        [
            InlineKeyboardButton("")
        ]
    )

    if avtar:
        return await m.reply_photo(photo=f"{avtar}", caption=REPLY)
    await m.reply_text(REPLY)
    return


pattern = re.compile(r"^text/|json$|yaml$|xml$|toml$|x-sh$|x-shellscript$")
BASE = "https://pasty.lus.pm/"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
    "content-type": "application/json",
}


def paste(content: str):
    data = {"content": content}
    resp = resp_post(f"{BASE}api/v1/pastes", data=json.dumps(data), headers=headers)
    if not resp.ok:
        return
    resp = resp.json()
    return BASE + resp['id']


@Gojo.on_message(command("paste"))
async def paste_func(_, message: Message):
    r = message.reply_to_message
    m = await message.reply_text("Pasting...")

    if not r:
        content = message.text.split(None, 1)[1]

    if r:
        if not r.text and not r.document:
            return await m.edit("Only text and documents are supported")

        if r.text:
            content = r.text
            exe = "txt"
        if r.document:
            # if r.document.file_size > 40000:
            #     return await m.edit("You can only paste files smaller than 40KB.")

            if not pattern.search(r.document.mime_type):
                return await m.edit("Only text files can be pasted.")

            doc = await message.reply_to_message.download()
            exe = doc.rsplit(".", 1)[-1]
            async with aiofiles.open(doc, mode="r") as f:
                fdata = await f.read()
                content = fdata

            remove(doc)
    try:
        link = paste(content)
    except Exception as e:
        await m.edit_text(e)
        return
    if not link:
        await m.edit_text("Failed to post!")
        return
    kb = [[InlineKeyboardButton(text="πŸ“ Paste πŸ“", url=f"{link}.{exe}")]]
    await m.delete()
    try:
        await message.reply_text("Here's your paste", reply_markup=InlineKeyboardMarkup(kb))
    except Exception as e:
        if link:
            return await message.reply_text(f"Here's your paste:\n [link]({link}.{exe})")
        return await message.reply_text(f"Failed to post. Due to following error:\n{e}")


@Gojo.on_message(command("tr"))
async def tr(_, message):
    trl = Translator()
    if message.reply_to_message and (
            message.reply_to_message.text or message.reply_to_message.caption
    ):
        if len(message.text.split()) == 1:
            target_lang = "en"
        else:
            target_lang = message.text.split()[1]
        if message.reply_to_message.text:
            text = message.reply_to_message.text
        else:
            text = message.reply_to_message.caption
    else:
        if len(message.text.split()) <= 2:
            await message.reply_text(
                "Provide lang code.\n[Available options](https://telegra.ph/Lang-Codes-02-22).\n<b>Usage:</b> <code>/tr en</code>",
            )
            return
        target_lang = message.text.split(None, 2)[1]
        text = message.text.split(None, 2)[2]
    detectlang = await trl.detect(text)
    try:
        tekstr = await trl(text, targetlang=target_lang)
    except ValueError as err:
        await message.reply_text(f"Error: <code>{str(err)}</code>")
        return
    return await message.reply_text(
        f"<b>Translated:</b> from {detectlang} to {target_lang} \n<code>``{tekstr.text}``</code>",
    )


@Gojo.on_message(command("bug"))
async def reporting_query(c: Gojo, m: Message):
    repl = m.reply_to_message
    if not repl:
        await m.reply_text("Please reply to a message to report it as bug")
        return
    if not repl.text:
        await m.reply_text("Please reply to a text message.")
        return
    txt = "#BUG\n"
    txt += repl.text.html
    txt += f"\nReported by: {m.from_user.id} ({m.from_user.mention})"
    kb = InlineKeyboardMarkup([[InlineKeyboardButton("Update channel", url=f"https://t.me/{SUPPORT_GROUP}")], [
        InlineKeyboardButton("Report on github", url="https://github.com/Gojo-Bots/Gojo_Satoru/issues/new/choose")]])
    try:
        z = await c.send_message(MESSAGE_DUMP, txt, parse_mode=enums.ParseMode.HTML)
    except Exception:
        txt = repl.text.html
        z = await c.send_message(MESSAGE_DUMP, txt, parse_mode=enums.ParseMode.HTML)
        await z.reply_text(f"#BUG\nReported by: {m.from_user.id} ({m.from_user.mention})")
    await repl.delete()
    await m.reply_photo(photo="./extras/Fire.jpg", caption="Successfully reported your bug", reply_markup=kb)
    ppost = z.link
    await c.send_message(OWNER_ID, f"New bug report\n{ppost}", disable_web_page_preview=True)
    return


@Gojo.on_message(command("botstaffs"))
async def botstaff(c: Gojo, m: Message):
    try:
        owner = await c.get_users(OWNER_ID)
        reply = f"<b>🌟 Owner:</b> {(await mention_html(owner.first_name, OWNER_ID))} (<code>{OWNER_ID}</code>)\n"
    except RPCError:
        pass
    true_dev = get_support_staff("dev")
    reply += "\n<b>Developers ⚑️:</b>\n"
    if not true_dev:
        reply += "No Dev Users\n"
    else:
        for each_user in true_dev:
            user_id = int(each_user)
            try:
                user = await c.get_users(user_id)
                reply += f"β€’ {(await mention_html(user.first_name, user_id))} (<code>{user_id}</code>)\n"
            except RPCError:
                pass
    true_sudo = get_support_staff("sudo")
    reply += "\n<b>Sudo Users πŸ‰:</b>\n"
    if not true_sudo:
        reply += "No Sudo Users\n"
    else:
        for each_user in true_sudo:
            user_id = int(each_user)
            try:
                user = await c.get_users(user_id)
                reply += f"β€’ {(await mention_html(user.first_name, user_id))} (<code>{user_id}</code>)\n"
            except RPCError:
                pass
    reply += "\n<b>Whitelisted Users 🐺:</b>\n"
    if not get_support_staff("whitelist"):
        reply += "No additional whitelisted users\n"
    else:
        for each_user in get_support_staff("whitelist"):
            user_id = int(each_user)
            try:
                user = await c.get_users(user_id)
                reply += f"β€’ {(await mention_html(user.first_name, user_id))} (<code>{user_id}</code>)\n"
            except RPCError:
                pass
    await m.reply_text(reply)
    return


__PLUGIN__ = "utils"
_DISABLE_CMDS_ = ["paste", "wiki", "id", "gifid", "tr", "github", "git", "bug"]
__alt_name__ = ["util", "misc", "tools"]

__HELP__ = """
**Utils**

Some utils provided by bot to make your tasks easy!

β€’ /id: Get the current group id. If used by replying to a message, get that user's id.
β€’ /gifid: Reply to a gif to me to tell you its file ID.
β€’ /lyrics `<song name>`-`<artist name>` : Find your song and give the lyrics of the song
β€’ /wiki: `<query>`: wiki your query.
β€’ /tr `<language>`: Translates the text and then replies to you with the language you have specifed, works as a reply to message.
β€’ /git `<username>`: Search for the user using github api!
β€’ /weebify `<text>` or `<reply to message>`: To weebify the text.
β€’ /bug <reply to text message> : To report a bug
β€’ /botstaffs : Give the list of the bot staffs.

**Example:**
`/git iamgojoof6eyes`: this fetches the information about a user from the database."""