Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
3.2 kB
3
Indexable
Never
import os
import keep_alive
import tensorflow as tf
import requests
import re
from PIL import Image
import numpy as np
import discord
import io
import asyncio
import random
from tensorflow.python.ops.numpy_ops import np_config

np_config.enable_numpy_behavior()

user_token = os.environ['user_token']

pokemon_list = eval(open("pokemon.txt", "r", encoding="utf-8").read())
with open('data/pokemon', 'r', encoding='utf8') as file:
    pokemon_names = file.read()


def solve(message):
    hint = []
    for i in range(15, len(message) - 1):
        if message[i] != '\\':
            hint.append(message[i])
    hint_string = ''
    for i in hint:
        hint_string += i
    hint_replaced = hint_string.replace('_', '.')
    solution = re.findall('^' + hint_replaced + '$', pokemon_names,
                          re.MULTILINE)
    return solution


async def catch(message):
    interpreter = tf.lite.Interpreter(model_path="pokemon.tflite")
    interpreter.allocate_tensors()
    res = requests.get(message.embeds[0].image.url, stream=True).content
    image = Image.open(io.BytesIO(res))
    resized_image = image.resize((200, 125))
    image_array = np.array(resized_image) / 255.0
    image_array = np.expand_dims(image_array, axis=0)
    image_array = image_array.astype(np.float32)
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]["index"], image_array)
    interpreter.invoke()
    pokemon_index = tf.argmax(interpreter.get_tensor(output_details[0]["index"]), axis=1).tolist()[0]
    pokemon_name = pokemon_list[pokemon_index]
    if re.search("[a-zA-Z.]", pokemon_name) is None:
        pokemon_name = pokemon_name.replace(" ", "")
    await asyncio.sleep(random.randint(2, 6))
    await message.channel.send(f"<@716390085896962058> c {pokemon_name}")


async def hint(message):
    await asyncio.sleep(1)
    await message.channel.send("<@716390085896962058> h", delete_after = 1)


class Discord(discord.Client):
    async def on_ready(self):
        print(f"Logged in as {self.user}")
        await self.change_presence(status=discord.Status.invisible, activity=discord.Game(name="Free Fire", type=3))

    async def on_message(self, message):
        if message.author.id != 744475570557026316 and message.author.id != 716390085896962058:
            return

        if len(message.embeds) > 0 and "wild pokémon has appeared!" in message.embeds[0].title:
            await catch(message)

        content = message.content
        solutions = []
        if "the wrong pokémon!" in content:
            await hint(message)
        elif "The pokémon is " in content:
            solutions = solve(content)
            if not solutions:
                print('Pokemon not found.')
            else:
                for i in solutions:
                    await message.delete()
                    await asyncio.sleep(random.randint(2, 3))
                    await message.channel.send(f'<@716390085896962058> c {i}')
        elif "human" in content:
            await message.channel.send('!kick <@716390085896962058>')             

client = Discord()
keep_alive.keep_alive()
client.run(user_token)