From c5f00a1d1b779504487039aed0fab08cadd91f00 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 23 Apr 2026 23:27:38 +0100 Subject: [PATCH] fix: remove old extension in external repo's --- AGENTS.md | 2 +- extensions/discord/__init__.py | 0 extensions/discord/bot.py | 158 --------------------------------- 3 files changed, 1 insertion(+), 159 deletions(-) delete mode 100644 extensions/discord/__init__.py delete mode 100644 extensions/discord/bot.py diff --git a/AGENTS.md b/AGENTS.md index 89213e7d..f9a603b8 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -115,7 +115,7 @@ vale . - `frontend/`: Vite + React + TypeScript application. - `frontend/src/`: main UI code, including `components`, `conversation`, `hooks`, `locale`, `settings`, `upload`, and Redux store wiring in `store.ts`. - `docs/`: separate documentation site built with Next.js/Nextra. -- `extensions/`: integrations and widgets such as Chatwoot, Chrome, Discord, React widget, Slack bot, and web widget. +- `extensions/`: integrations and widgets — currently the Chatwoot webhook bridge and the React widget (published to npm as `docsgpt`). The Discord bot, Slack bot, and Chrome extension have been moved to their own repos under `arc53/`. - `deployment/`: Docker Compose variants and Kubernetes manifests. ## Coding rules diff --git a/extensions/discord/__init__.py b/extensions/discord/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/extensions/discord/bot.py b/extensions/discord/bot.py deleted file mode 100644 index 94daf7e2..00000000 --- a/extensions/discord/bot.py +++ /dev/null @@ -1,158 +0,0 @@ -import os -import re -import logging -import aiohttp -import discord -from discord.ext import commands -import dotenv - -dotenv.load_dotenv() - -# Enable logging -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -# Bot configuration -TOKEN = os.getenv("DISCORD_TOKEN") -PREFIX = '!' # Command prefix -BASE_API_URL = os.getenv("API_BASE", "https://gptcloud.arc53.com") -API_URL = BASE_API_URL + "/api/answer" -API_KEY = os.getenv("API_KEY") - -intents = discord.Intents.default() -intents.message_content = True - -bot = commands.Bot(command_prefix=PREFIX, intents=intents) - -# Store conversation history per user -conversation_histories = {} - -def chunk_string(text, max_length=2000): - """Splits a string into chunks of a specified maximum length.""" - # Create list to store the split strings - chunks = [] - # Loop through the text, create substrings with max_length - while len(text) > max_length: - # Find last space within the limit - idx = text.rfind(' ', 0, max_length) - # Ensure we don't have an empty part - if idx == -1: - # If no spaces, just take chunk - chunks.append(text[:max_length]) - text = text[max_length:] - else: - # Push whatever we've got up to the last space - chunks.append(text[:idx]) - text = text[idx+1:] - # Catches the remaining part - chunks.append(text) - return chunks - -def escape_markdown(text): - """Escapes Discord markdown characters.""" - escape_chars = r'\*_$$$$()~>#+-=|{}.!' - return re.sub(f'([{re.escape(escape_chars)}])', r'\\\1', text) - -def split_string(input_str): - """Splits the input string to detect bot mentions.""" - pattern = r'^<@!?{0}>\s*'.format(bot.user.id) - match = re.match(pattern, input_str) - if match: - content = input_str[match.end():].strip() - return str(bot.user.id), content - return None, input_str - -@bot.event -async def on_ready(): - print(f'{bot.user.name} has connected to Discord!') - -async def generate_answer(question, messages, conversation_id): - """Generates an answer using the external API.""" - payload = { - "question": question, - "api_key": API_KEY, - "history": messages, - "conversation_id": conversation_id - } - headers = { - "Content-Type": "application/json; charset=utf-8" - } - timeout = aiohttp.ClientTimeout(total=60) - async with aiohttp.ClientSession(timeout=timeout) as session: - async with session.post(API_URL, json=payload, headers=headers) as resp: - if resp.status == 200: - data = await resp.json() - conversation_id = data.get("conversation_id") - answer = data.get("answer", "Sorry, I couldn't find an answer.") - return {"answer": answer, "conversation_id": conversation_id} - else: - return {"answer": "Sorry, I couldn't find an answer.", "conversation_id": None} - -@bot.command(name="start") -async def start(ctx): - """Handles the /start command.""" - await ctx.send(f"Hi {ctx.author.mention}! How can I assist you today?") - -@bot.command(name="custom_help") -async def custom_help_command(ctx): - """Handles the /custom_help command.""" - help_text = ( - "Here are the available commands:\n" - "`!start` - Begin a new conversation with the bot\n" - "`!help` - Display this help message\n\n" - "You can also mention me or send a direct message to ask a question!" - ) - await ctx.send(help_text) - -@bot.event -async def on_message(message): - if message.author == bot.user: - return - - # Process commands first - await bot.process_commands(message) - - # Check if the message is in a DM channel - if isinstance(message.channel, discord.DMChannel): - content = message.content.strip() - else: - # In guild channels, check if the message mentions the bot at the start - content = message.content.strip() - prefix, content = split_string(content) - if prefix is None: - return - part_prefix = str(bot.user.id) - if part_prefix != prefix: - return # Bot not mentioned at the start, so do not process - - # Now process the message - user_id = message.author.id - if user_id not in conversation_histories: - conversation_histories[user_id] = { - "history": [], - "conversation_id": None - } - - conversation = conversation_histories[user_id] - conversation["history"].append({"prompt": content}) - - # Generate the answer - response_doc = await generate_answer( - content, - conversation["history"], - conversation["conversation_id"] - ) - answer = response_doc["answer"] - conversation_id = response_doc["conversation_id"] - - answer_chunks = chunk_string(answer) - for chunk in answer_chunks: - await message.channel.send(chunk) - - conversation["history"][-1]["response"] = answer - conversation["conversation_id"] = conversation_id - - # Keep conversation history to last 10 exchanges - conversation["history"] = conversation["history"][-10:] - -bot.run(TOKEN) \ No newline at end of file