File size: 5,495 Bytes
c7dfe8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SOURCE https://github.com/Team-ProjectCodeX
# CREATED BY https://t.me/O_okarma
# API BY https://www.github.com/SOME-1HING
# PROVIDED BY https://t.me/ProjectCodeX

# <============================================== IMPORTS =========================================================>
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import CallbackContext, CallbackQueryHandler, CommandHandler

from Mikobot import function
from Mikobot.state import state

# <=======================================================================================================>


# <================================================ FUNCTIONS =====================================================>
async def get_pokemon_info(name_or_id):
    try:
        response = await state.get(
            f"https://sugoi-api.vercel.app/pokemon?name={name_or_id}"
        )
        if response.status_code == 200:
            return response.json()

        response = await state.get(
            f"https://sugoi-api.vercel.app/pokemon?id={name_or_id}"
        )
        if response.status_code == 200:
            return response.json()

    except Exception as e:
        print(f"An error occurred: {str(e)}")

    return None


async def pokedex(update: Update, context: CallbackContext):
    try:
        if context.args:
            name_or_id = context.args[0]
            pokemon_info = await get_pokemon_info(name_or_id)

            if pokemon_info:
                reply_message = (
                    f"🐾 π—‘π—”π— π—˜: {pokemon_info['name']}\n"
                    f"β€’βž₯ π—œπ——: {pokemon_info['id']}\n"
                    f"β€’βž₯ π—›π—˜π—œπ—šπ—›π—§: {pokemon_info['height']}\n"
                    f"β€’βž₯ π—ͺπ—˜π—œπ—šπ—›π—§: {pokemon_info['weight']}\n"
                )

                abilities = ", ".join(
                    ability["ability"]["name"] for ability in pokemon_info["abilities"]
                )
                reply_message += f"β€’βž₯ π—”π—•π—œπ—Ÿπ—œπ—§π—œπ—˜π—¦: {abilities}\n"

                types = ", ".join(
                    type_info["type"]["name"] for type_info in pokemon_info["types"]
                )
                reply_message += f"β€’βž₯ π—§π—¬π—£π—˜π—¦: {types}\n"

                image_url = f"https://img.pokemondb.net/artwork/large/{pokemon_info['name']}.jpg"

                # Create inline buttons
                keyboard = [
                    [
                        InlineKeyboardButton(text="πŸ”– STATS", callback_data="stats"),
                        InlineKeyboardButton(text="⚜️ MOVES", callback_data="moves"),
                    ]
                ]

                reply_markup = InlineKeyboardMarkup(keyboard)

                await update.message.reply_photo(
                    photo=image_url,
                    caption=reply_message,
                    reply_markup=reply_markup,
                )
            else:
                await update.message.reply_text("Pokemon not found.")
        else:
            await update.message.reply_text("Please provide a Pokemon name or ID.")
    except Exception as e:
        await update.message.reply_text(f"An error occurred: {str(e)}")


async def callback_query_handler(update: Update, context: CallbackContext):
    query = update.callback_query
    await query.answer()

    try:
        name = query.message.caption.split("\n")[0].split(": ")[1]
        pokemon_info = await get_pokemon_info(name)

        if pokemon_info:
            stats = "\n".join(
                f"{stat['stat']['name'].upper()}: {stat['base_stat']}"
                for stat in pokemon_info["stats"]
            )
            stats_message = f"β€’βž₯ STATS:\n{stats}\n"

            moves = ", ".join(
                move_info["move"]["name"] for move_info in pokemon_info["moves"]
            )
            moves_message = f"β€’βž₯ MOVES: {moves}"

            if query.data == "stats":
                await query.message.reply_text(stats_message)
            elif query.data == "moves":
                if len(moves_message) > 1000:
                    with open("moves.txt", "w") as file:
                        file.write(moves_message)
                    await query.message.reply_text(
                        "The moves exceed 1000 characters. Sending as a file.",
                        disable_web_page_preview=True,
                    )
                    await query.message.reply_document(document=open("moves.txt", "rb"))
                else:
                    await query.message.reply_text(moves_message)
        else:
            await query.message.reply_text("Pokemon not found.")
    except Exception as e:
        await query.message.reply_text(f"An error occurred: {str(e)}")


# <================================================ HANDLER =======================================================>
# Add the command and callback query handlers to the dispatcher
function(CommandHandler("pokedex", pokedex, block=False))
function(
    CallbackQueryHandler(callback_query_handler, pattern="^(stats|moves)$", block=False)
)

# <================================================ HANDLER =======================================================>
__help__ = """

πŸ₯ *POKEMON SEARCH*

➠ *Commands*:

Β»  /pokedex < Search > : Gives that pokemon info.
"""

__mod_name__ = "POKEDEX"
# <================================================ END =======================================================>