import discord import traceback DISCORD_TOKEN = 'указать токен' class MyClient(discord.Client): def __init__(self, *args, **kwargs): intents = discord.Intents.default() intents.message_content = True super().__init__(intents=intents, *args, **kwargs) async def on_message(self, message): print("Received message:", message.content) try: if message.author.id == self.user.id: return context = update_context(message.author.id, message, "user") print("Updated context:", context) messages = format_prompt(context) print("Formatted prompt:", messages) response = await call_huggingface_api(messages) print("Received response:", response) if response is not None and 'generated_text' in response[0]: response_text = response[0]['generated_text'] print("Filtered response:", response_text) response_text = filter_response(response_text) print("Post-processed response:", response_text) # Check if the response contains the user's question if context[-1]["content"].lower() not in response_text.lower(): # If the response does not contain the user's question, clear the context and generate a new response contexts[message.author.id] = [] context = update_context(message.author.id, message, "user") messages = format_prompt(context) response = await call_huggingface_api(messages) response_text = response[0]['generated_text'] response_text = filter_response(response_text) response_text = post_process(response_text) last_bot_response = response_text.split("user:")[-1].strip() print("Sending response:", last_bot_response) if len(last_bot_response) > 2000: chunks = [last_bot_response[i:i+2000] for i in range(0, len(last_bot_response), 2000)] for chunk in chunks: await message.channel.send(chunk) else: await message.channel.send(last_bot_response) else: print("Error generating text.") await message.channel.send("Error generating text.") except Exception as e: print(f"Error in on_message: {e}") print(traceback.format_exc()) await message.channel.send("An error occurred while processing your message. Please try again later.") async def main(): client = MyClient() await client.start(DISCORD_TOKEN)