Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Инструкция по установке и настройке:
- 1. Установите Python:
- - Скачайте Python с официального сайта: https://www.python.org/downloads/
- - При установке отметьте галочку "Add Python to PATH"
- 2. Установите необходимые библиотеки:
- - Откройте командную строку (cmd)
- - Введите следующие команды:
- pip install selenium
- pip install webdriver-manager
- pip install characterai
- pip install yt-dlp
- 3. Настройка:
- - Убедитесь, что у вас установлен Google Chrome
- - В коде найдите строки с CHAR_TOKEN и CHAR_ID
- - Замените их на свои токены от Character.AI
- 4. Запуск:
- - Сохраните этот файл как .py (например, bot.py)
- - Откройте командную строку
- - Перейдите в папку с файлом
- - Введите: python bot.py
- 5. Дополнительно:
- - Бот автоматически откроет браузер и перейдет на сайт
- - Следуйте инструкциям в консоли
- Если возникнут ошибки:
- - Проверьте, что все библиотеки установлены
- - Убедитесь, что Python добавлен в PATH
- - Проверьте версию Chrome (должна быть актуальной)
- - Попробуйте переустановить библиотеки
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.chrome.service import Service
- from webdriver_manager.chrome import ChromeDriverManager
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- import time
- import asyncio
- from characterai import aiocai
- import random
- import re
- from collections import defaultdict
- import argparse
- import yt_dlp
- driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
- driver.get("https://esonline.su/#/eso/")
- time.sleep(3)
- CHAR_TOKEN = "7f6f4b088c0a04d446d746c2e8cdc19b30948424"
- CHAR_ID = "Сюда вставить ID персонажа из CharacterAI!"
- #Зайдите в чат с персонажем и скопируйте в адресной строке его айди, после вставьте сюда.
- TARGET_PLAYER = ""
- client = None
- chat = None
- chat_id = None
- me = None
- is_silent = False
- CHOSEN_PLAYER = None
- AUTO_LOCATION = True
- AUTO_MESSAGE = True
- MIN_STAY_TIME = 300
- MAX_STAY_TIME = 900
- player_activity = defaultdict(int)
- USE_MESSAGE_BUFFER = False
- MESSAGE_TIMEOUT = 5
- async def initialize_client_and_chat():
- global client, chat, chat_id, me
- print("🔄 Начинаем инициализацию Character.AI...")
- for attempt in range(3):
- try:
- print(f"Попытка подключения {attempt + 1}/3...")
- client = aiocai.Client(CHAR_TOKEN)
- print("✅ Клиент Character.AI создан")
- me = await client.get_me()
- print(f"✅ Получен профиль: {me.name} (ID: {me.id})")
- print("🔄 Подключаемся к чату...")
- chat = await client.connect()
- print("✅ Подключение к чату установлено")
- async with chat as chat_obj:
- print(f"🔄 Создаем новый чат с персонажем {CHAR_ID}...")
- new_chat, answer = await chat_obj.new_chat(CHAR_ID, me.id)
- chat_id = new_chat.chat_id
- print(f"✅ Чат создан! ID чата: {chat_id}")
- print(f"📝 Первое сообщение персонажа: {answer.text}")
- return True
- except Exception as e:
- print(f"❌ Ошибка при инициализации (попытка {attempt + 1}): {str(e)}")
- if attempt < 2:
- print("🔄 Повторная попытка через 5 секунд...")
- await asyncio.sleep(5)
- else:
- print("❌ Все попытки подключения завершились неудачей")
- return False
- async def get_dan_response(user_input):
- try:
- print(f"\n📤 Отправляем сообщение в Character.AI: {user_input}")
- async with chat as chat_obj:
- response = await chat_obj.send_message(CHAR_ID, chat_id, user_input)
- print(f"📥 Получен ответ от Character.AI: {response.text}")
- return response.text
- except Exception as e:
- print(f"❌ Ошибка при получении ответа от Character.AI: {str(e)}")
- if "closed" in str(e).lower() or "connection" in str(e).lower():
- print("🔄 Попытка переподключения...")
- if await initialize_client_and_chat():
- async with chat as chat_obj:
- response = await chat_obj.send_message(CHAR_ID, chat_id, user_input)
- return response.text
- return "Извините, произошла ошибка при генерации ответа."
- def send_text_with_delay(element, text):
- element.clear()
- for char in text:
- element.send_keys(char)
- time.sleep(0.01)
- element.send_keys(Keys.ENTER)
- def send_chat_command(command):
- chat_input = None
- for selector in ["textarea", ".input input[type='text']", "input[type='text']"]:
- try:
- chat_input = driver.find_element(By.CSS_SELECTOR, selector)
- break
- except:
- continue
- if chat_input:
- send_text_with_delay(chat_input, command)
- return True
- return False
- def search_youtube_video(query):
- ydl_opts = {
- 'quiet': True,
- 'no_warnings': True,
- 'format': 'bestaudio/best',
- 'max_downloads': 1,
- 'noplaylist': True,
- }
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
- info = ydl.extract_info(f"ytsearch1:{query}", download=False)
- if 'entries' in info and info['entries']:
- return info['entries'][0]['webpage_url']
- return None
- def determine_mood(message):
- message = message.lower()
- happy_keywords = [" :)", "хаха", "lol", "лол", "смешно", "круто", "привет", "здорово"]
- if any(keyword in message for keyword in happy_keywords):
- return "happy"
- sad_keywords = [":(", "грустно", "плохо", "жаль", "печаль"]
- if any(keyword in message for keyword in sad_keywords):
- return "sad"
- surprised_keywords = ["?", "!", "ого", "вау", "серьезно", "шок"]
- if any(keyword in message for keyword in surprised_keywords):
- return "surprised"
- return "neutral"
- def is_approach_request(message):
- message = message.lower()
- approach_keywords = [
- "подойди", "иди сюда", "пойдем", "давай сюда", "приди",
- "подойти", "иди ко мне", "иди", "ко мне", "сюда",
- "подходи", "подойдешь", "подойдёшь", "подойдите"
- ]
- return any(keyword in message for keyword in approach_keywords)
- def change_sprite(mood):
- sprite_commands = {
- "neutral": "/sp 5282 5283 5289 5293",
- "happy": "/sp 5282 5284 5289 5293",
- "sad": "/sp 5282 5285 5289 5293",
- "surprised": "/sp 5282 5286 5289 5293"
- }
- command = sprite_commands.get(mood, sprite_commands["neutral"])
- send_chat_command(command)
- def move_character():
- x = random.randint(-50, 50)
- command = f"/move {x}"
- time.sleep(1)
- send_chat_command(command)
- def choose_player_with_dd():
- try:
- send_chat_command(".дд")
- time.sleep(1)
- players = driver.find_elements(By.CSS_SELECTOR, ".context-menu-wrapper span.msg font")
- if not players:
- return None
- actions = ActionChains(driver)
- for player in players:
- actions.move_to_element(player).perform()
- time.sleep(0.5)
- player_name = player.text.strip()
- sprite = driver.find_element(By.CSS_SELECTOR, f".character[data-player-name='{player_name}']")
- if "glow" in sprite.get_attribute("class"):
- return player_name
- return None
- except Exception as e:
- print(f"Ошибка при выборе игрока: {e}")
- return None
- def approach_player_by_sprite(target_player):
- try:
- send_chat_command(".дд")
- time.sleep(1)
- player_element = None
- players = driver.find_elements(By.CSS_SELECTOR, ".context-menu-wrapper span.msg font")
- for player in players:
- if player.text.strip() == target_player:
- player_element = player
- break
- if not player_element:
- return False
- all_sprites = driver.find_elements(By.CSS_SELECTOR, ".character")
- actions = ActionChains(driver)
- actions.move_to_element(player_element).perform()
- time.sleep(0.5)
- target_sprite = None
- for sprite in all_sprites:
- if "glow" in sprite.get_attribute("class"):
- target_sprite = sprite
- break
- if target_sprite:
- transform_style = target_sprite.get_attribute("style")
- print(f"Стиль спрайта: {transform_style}")
- x_coord_match = re.search(r'translateX\(([-\d.]+)%\)', transform_style)
- if x_coord_match:
- target_x = float(x_coord_match.group(1))
- print(f"Найдена X координата: {target_x}%")
- normalized_x = int(target_x / 2)
- normalized_x = max(-50, min(50, normalized_x))
- offset = random.choice([-1, 1]) * random.randint(5, 15)
- final_x = normalized_x + offset
- print(f"Нормализованная координата: {normalized_x}, с отступом: {final_x}")
- command = f"/move {final_x}"
- send_chat_command(command)
- return True
- return False
- except Exception as e:
- print(f"Ошибка при подходе к игроку: {e}")
- return False
- async def monitor_chat():
- global is_silent, TARGET_PLAYER
- if not TARGET_PLAYER:
- print("🔄 Поиск целевого игрока...")
- TARGET_PLAYER = choose_player_with_dd()
- if not TARGET_PLAYER:
- print("❌ Целевой игрок не найден")
- return
- print(f"✅ Выбран целевой игрок: {TARGET_PLAYER}")
- last_messages = set()
- MAX_MESSAGES = 50
- message_buffer = []
- last_message_time = time.time()
- while True:
- try:
- chat_container = driver.find_element(By.CSS_SELECTOR, ".messages")
- messages = chat_container.find_elements(By.CSS_SELECTOR, "span.msg")
- for msg in messages:
- message_text = msg.text.strip()
- if message_text and message_text not in last_messages:
- last_messages.add(message_text)
- print(f"📨 Новое сообщение в чате: {message_text}")
- if len(last_messages) > MAX_MESSAGES:
- last_messages.clear()
- print("🔄 Буфер сообщений очищен")
- if TARGET_PLAYER in message_text:
- message = message_text
- if f"{TARGET_PLAYER}:" in message:
- message = message_text.split(f"{TARGET_PLAYER}:", 1)[-1].strip()
- elif f"[{TARGET_PLAYER}]" in message:
- message = message_text.split(f"[{TARGET_PLAYER}]", 1)[-1].strip()
- else:
- message = message_text.split(TARGET_PLAYER, 1)[-1].strip()
- message_lower = message.lower().strip()
- print(f"📝 Обработка сообщения от {TARGET_PLAYER}: {message}")
- if message_lower == "заткнись":
- is_silent = True
- print("🔇 Режим молчания активирован")
- send_chat_command("Молчу!")
- message_buffer.clear()
- continue
- elif message_lower == "говори":
- is_silent = False
- print("🔊 Режим молчания деактивирован")
- send_chat_command("Говорю!")
- message_buffer.clear()
- continue
- elif message_lower.startswith("включи"):
- if is_silent:
- print("🔇 Пропуск команды 'включи' из-за режима молчания")
- continue
- query = message[len("включи"):].strip()
- if query:
- print(f"🎵 Поиск видео: {query}")
- video_url = search_youtube_video(query)
- if video_url:
- play_command = f"/play {video_url}"
- print(f"🎵 Воспроизведение видео: {video_url}")
- send_chat_command(play_command)
- else:
- print("❌ Видео не найдено")
- send_chat_command("Не нашёл видео!")
- else:
- print("❌ Не указано название видео")
- send_chat_command("Укажи название видео!")
- message_buffer.clear()
- continue
- if is_silent:
- print("🔇 Пропуск сообщения из-за режима молчания")
- continue
- if USE_MESSAGE_BUFFER:
- message_buffer.append(message)
- last_message_time = time.time()
- print(f"📥 Сообщение добавлено в буфер. Текущий размер буфера: {len(message_buffer)}")
- else:
- print(f"\n📤 Отправка сообщения в Character.AI: {message}")
- dan_response = await get_dan_response(message)
- dan_response = dan_response.replace("\n", " ")[:200]
- print(f"📥 Ответ от Character.AI: {dan_response}")
- mood = determine_mood(dan_response)
- print(f"😊 Определено настроение: {mood}")
- change_sprite(mood)
- if is_approach_request(message):
- print(f"🚶 Подход к игроку {TARGET_PLAYER}")
- approach_player_by_sprite(TARGET_PLAYER)
- else:
- print("🚶 Случайное перемещение")
- move_character()
- print(f"📤 Отправка ответа в чат: {dan_response}")
- send_chat_command(dan_response)
- if USE_MESSAGE_BUFFER and message_buffer and (time.time() - last_message_time >= MESSAGE_TIMEOUT):
- combined_message = " ".join(message_buffer)
- print(f"\n📤 Отправка сообщения в Character.AI: {combined_message}")
- message_buffer.clear()
- dan_response = await get_dan_response(combined_message)
- dan_response = dan_response.replace("\n", " ")[:200]
- print(f"📥 Ответ от Character.AI: {dan_response}")
- mood = determine_mood(dan_response)
- print(f"😊 Определено настроение: {mood}")
- change_sprite(mood)
- if is_approach_request(combined_message):
- print(f"🚶 Подход к игроку {TARGET_PLAYER}")
- approach_player_by_sprite(TARGET_PLAYER)
- else:
- print("🚶 Случайное перемещение")
- move_character()
- print(f"📤 Отправка ответа в чат: {dan_response}")
- send_chat_command(dan_response)
- except Exception as e:
- print(f"❌ Ошибка в monitor_chat: {str(e)}")
- await asyncio.sleep(1)
- def approach_player(target_player):
- x = random.randint(-10, 10)
- command = f"/move {x}"
- send_chat_command(command)
- async def run_modified_mode():
- await asyncio.gather(
- monitor_chat(),
- handle_location_and_messages()
- )
- async def handle_location_and_messages():
- global CHOSEN_PLAYER, TARGET_PLAYER
- while True:
- stay_time = random.randint(MIN_STAY_TIME, MAX_STAY_TIME)
- if AUTO_LOCATION:
- if open_map():
- location = choose_most_populated_location()
- if location and go_to_location(location):
- try:
- send_chat_command(".дд")
- time.sleep(1)
- players = driver.find_elements(By.CSS_SELECTOR, ".context-menu-wrapper span.msg font")
- if not players:
- print("🔄 На локации нет игроков, пробуем войти в помещение...")
- if navigate_to_location():
- print("✅ Успешно вошли в помещение")
- stay_time *= 1.5
- else:
- print("❌ Не удалось войти в помещение")
- except Exception as e:
- print(f"❌ Ошибка при проверке игроков: {str(e)}")
- if AUTO_MESSAGE:
- if not CHOSEN_PLAYER:
- CHOSEN_PLAYER = choose_player_with_dd()
- if CHOSEN_PLAYER and not TARGET_PLAYER:
- TARGET_PLAYER = CHOSEN_PLAYER
- if CHOSEN_PLAYER:
- await talk_to_player(CHOSEN_PLAYER)
- await asyncio.sleep(stay_time)
- def open_map():
- try:
- actions = ActionChains(driver)
- actions.move_by_offset(500, 300).context_click().perform()
- time.sleep(1)
- menu = driver.find_element(By.CSS_SELECTOR, ".context-menu-wrapper")
- for item in menu.find_elements(By.CSS_SELECTOR, "div.button"):
- if item.text.strip() == "Карта":
- item.click()
- time.sleep(2)
- return True
- return False
- except Exception:
- return False
- def choose_most_populated_location():
- try:
- locations = driver.find_elements(By.CSS_SELECTOR, "div.mappoint")
- max_people = -1
- best_location = None
- for location in locations:
- people = int(location.find_element(By.CSS_SELECTOR, "div.point").get_attribute("online") or 0)
- if people > max_people:
- max_people = people
- best_location = location
- return best_location
- except Exception:
- return None
- def go_to_location(location):
- try:
- location.click()
- time.sleep(3)
- return True
- except Exception:
- return False
- async def talk_to_player(player):
- greeting = f"Привет, {player}! Как дела?"
- dan_response = await get_dan_response(greeting)
- dan_response = dan_response.replace("\n", " ")[:200]
- mood = determine_mood(dan_response)
- change_sprite(mood)
- send_chat_command(dan_response)
- def navigate_to_location():
- try:
- print("🔄 Открываем контекстное меню...")
- actions = ActionChains(driver)
- actions.move_by_offset(500, 300).context_click().perform()
- time.sleep(1)
- menu = driver.find_element(By.CSS_SELECTOR, ".context-menu-wrapper")
- if not menu:
- print("❌ Меню не найдено")
- return False
- menu_items = menu.find_elements(By.CSS_SELECTOR, "div.button")
- if not menu_items:
- print("❌ Пункты меню не найдены")
- return False
- for item in menu_items:
- item_text = item.text.strip().lower()
- if "перейти" in item_text or "войти" in item_text:
- print(f"✅ Найден пункт меню: {item.text}")
- item.click()
- time.sleep(2)
- return True
- print("❌ Пункт 'Перейти' не найден в меню")
- return False
- except Exception as e:
- print(f"❌ Ошибка при навигации: {str(e)}")
- return False
- async def main():
- global CHOSEN_PLAYER, AUTO_LOCATION, AUTO_MESSAGE, TARGET_PLAYER, USE_MESSAGE_BUFFER
- parser = argparse.ArgumentParser(description="Запуск скрипта")
- parser.add_argument('--mode', choices=['old', 'modified'])
- parser.add_argument('--player')
- parser.add_argument('--auto-location', choices=['yes', 'no'])
- parser.add_argument('--auto-message', choices=['yes', 'no'])
- parser.add_argument('--buffer', choices=['yes', 'no'], help='Включить/выключить буфер сообщений')
- args = parser.parse_args()
- mode = args.mode or input("1 - старый, 2 - модифицированный: ") == "2" and "modified" or "old"
- if mode == "modified":
- CHOSEN_PLAYER = args.player or input("Имя игрока (Enter для авто): ").strip() or None
- AUTO_LOCATION = args.auto_location == "yes" if args.auto_location else input("Автолокация? (y/n): ").lower() == "y"
- AUTO_MESSAGE = args.auto_message == "yes" if args.auto_message else input("Автосообщения? (y/n): ").lower() == "y"
- USE_MESSAGE_BUFFER = args.buffer == "yes" if args.buffer else input("Использовать буфер сообщений? (y/n): ").lower() == "y"
- if CHOSEN_PLAYER:
- TARGET_PLAYER = CHOSEN_PLAYER
- if not await initialize_client_and_chat():
- return
- await (run_modified_mode() if mode == "modified" else run_old_mode())
- async def run_old_mode():
- await monitor_chat()
- if __name__ == "__main__":
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement