diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bc17f --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fdddb29 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b4ef28 --- /dev/null +++ b/README.md @@ -0,0 +1,103 @@ +# Description + +This is a Python script for a Discord bot that uses OpenAI's GPT API to generate responses to user messages. The bot can be configured to listen to specific channels and respond to direct messages. The bot also has a rate limit to prevent spamming and can maintain a per user conversational history to improve response quality which is only limited by the `GPT_TOKENS` value. + +# Requirements + +- Python 3.6 or higher +- discord.py +- openai + +# Installation + +1. Clone the repository to your local machine. +2. Install the required packages using pip: `pip install -r requirements.txt` +3. Rename `config.ini.example` to `config.ini` and fill in the required configuration details. + +# Configuration + +The `config.ini` file contains the following configuration sections: + +### Discord + +- `DISCORD_TOKEN`: The Discord bot token. +- `ALLOWED_CHANNELS`: A comma-separated list of channel names that the bot is allowed to listen to. +- `BOT_PRESENCE`: The presence of the bot (e.g. online, offline, idle). +- `ACTIVITY_TYPE`: The type of activity for the bot (e.g. playing, streaming, listening, watching, custom, competing). +- `ACTIVITY_STATUS`: The activity status of the bot (e.g. Humans). + +### OpenAI + +- `OPENAI_API_KEY`: The OpenAI API key. +- `OPENAI_TIMEOUT`: The OpenAI API timeout in seconds. (default: 30) +- `GPT_MODEL`: The GPT model to use (default: gpt-3.5-turbo). +- `GPT_TOKENS`: The maximum number of tokens to generate in the GPT response (default: 3072). +- `SYSTEM_MESSAGE`: The message to send to the GPT model before the user's message. + +### Limits + +- `RATE_LIMIT`: The number of messages a user can send within `RATE_LIMIT_PER` seconds (default: 2). +- `RATE_LIMIT_PER`: The time period in seconds for the rate limit (default: 10). + +### Logging + +- `LOG_FILE`: The path to the log file (default: bot.log). + +Here is an example `config.ini` file: + +```ini +[Discord] +DISCORD_TOKEN = +ALLOWED_CHANNELS = , , ... +BOT_PRESENCE = online +# ACTIVITY_TYPE Options +# playing, streaming, listening, watching, custom, competing +ACTIVITY_TYPE=listening +ACTIVITY_STATUS=Humans + +[OpenAI] +OPENAI_API_KEY = +OPENAI_TIMEOUT=30 +GPT_MODEL=gpt-3.5-turbo +GPT_TOKENS=3072 +SYSTEM_MESSAGE = You are a helpful AI assistant. + +[Limits] +RATE_LIMIT = 2 +RATE_LIMIT_PER = 10 + +[Logging] +LOG_FILE = bot.log +``` + +# Discord Bot Setup + +To use this bot, you will need to create a Discord bot and invite it to your server. Here are the steps to do so: + +1. Go to the [Discord Developer Portal](https://discord.com/developers/applications) and create a new application. +2. Click on the "Bot" tab and then click "Add Bot". +3. Copy the bot token and paste it into the `DISCORD_TOKEN` field in the `config.ini` file. +4. Under the "OAuth2" tab, select the "bot" scope and then select the permissions you want the bot to have. +5. Copy the generated OAuth2 URL and paste it into your web browser. This will allow you to invite the bot to your server. + +# Usage + +To start the bot, run the following command: + +``` +python bot.py --conf config.ini +``` + +The bot will log in to Discord and start listening for messages in the configured channels. When a message is received, the bot will send the message to the OpenAI API and wait for a response. The response will be sent back to the user who sent the message. + +# Contributing + +Contributions are welcome! Please open an issue or pull request on the GitHub repository. + +# Contact + +If you have any questions or concerns, please contact us at code@rly.wtf + +# License + +This project is licensed under the Unlicense - see the [LICENSE](LICENSE) file for details. \ No newline at end of file diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..c869581 --- /dev/null +++ b/bot.py @@ -0,0 +1,228 @@ +import argparse +import configparser +import logging +import time +from logging.handlers import RotatingFileHandler + +import discord +import openai +from discord.errors import ConnectionClosed + +# Parse command-line arguments +parser = argparse.ArgumentParser(description='GPT-based Discord bot.') +parser.add_argument('--conf', help='Configuration file path') +args = parser.parse_args() + +# Read configuration from a file +config = configparser.ConfigParser() +config.read(args.conf) + +# Retrieve configuration details from the configuration file +DISCORD_TOKEN = config.get('Discord', 'DISCORD_TOKEN') +ALLOWED_CHANNELS = config.get('Discord', 'ALLOWED_CHANNELS', fallback='').split(',') +BOT_PRESENCE = config.get('Discord', 'BOT_PRESENCE', fallback='online') + +# ACTIVITY_TYPE options: playing, streaming, listening, watching, custom, competing +ACTIVITY_TYPE = config.get('Discord', 'ACTIVITY_TYPE', fallback='listening') +ACTIVITY_STATUS = config.get('Discord', 'ACTIVITY_STATUS', fallback='Humans') + +OPENAI_API_KEY = config.get('OpenAI', 'OPENAI_API_KEY') +OPENAI_TIMEOUT = config.get('OpenAI', 'OPENAI_TIMEOUT', fallback='30') +GPT_MODEL = config.get('OpenAI', 'GPT_MODEL', fallback='gpt-3.5-turbo') +GPT_TOKENS = config.getint('OpenAI', 'GPT_TOKENS', fallback=60) +SYSTEM_MESSAGE = config.get('OpenAI', 'SYSTEM_MESSAGE', fallback='You are a helpful assistant.') + +RATE_LIMIT = config.getint('Limits', 'RATE_LIMIT', fallback=10) +RATE_LIMIT_PER = config.getint('Limits', 'RATE_LIMIT_PER', fallback=60) + +LOG_FILE = config.get('Logging', 'LOG_FILE', fallback='bot.log') + +# Set up logging +logger = logging.getLogger('discord') +logger.setLevel(logging.INFO) + +# File handler +file_handler = RotatingFileHandler(LOG_FILE, maxBytes=5 * 1024 * 1024, backupCount=5) +file_handler.setLevel(logging.WARNING) +file_formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(name)s: %(message)s') +file_handler.setFormatter(file_formatter) +logger.addHandler(file_handler) + +# Set the intents for the bot +intents = discord.Intents.default() +intents.typing = False +intents.presences = False + +# Set your OpenAI API key +openai.api_key = OPENAI_API_KEY + +# Create a dictionary to store the last command timestamp for each user +last_command_timestamps = {} +last_command_count = {} + +# Create a dictionary to store conversation history for each user +conversation_history = {} + +def set_activity_status(activity_type, activity_status): + """ + Returns a discord.Activity object with the specified activity type and status. + """ + activity_types = { + 'playing': discord.ActivityType.playing, + 'streaming': discord.ActivityType.streaming, + 'listening': discord.ActivityType.listening, + 'watching': discord.ActivityType.watching, + 'custom': discord.ActivityType.custom, + 'competing': discord.ActivityType.competing + } + activity_type = activity_types.get(activity_type, discord.ActivityType.listening) + return discord.Activity(type=activity_type, name=activity_status) + +# Set the bot's presence and activity status +activity = set_activity_status(config['Discord']['ACTIVITY_TYPE'], config['Discord']['ACTIVITY_STATUS']) +bot = discord.Client(intents=intents, activity=activity, status=discord.Status(BOT_PRESENCE)) + +@bot.event +async def on_ready(): + """ + Event handler for when the bot is ready to receive messages. + """ + logger.info(f'We have logged in as {bot.user}') + logger.info(f'Configured bot presence: {BOT_PRESENCE}') + logger.info(f'Configured activity type: {ACTIVITY_TYPE}') + logger.info(f'Configured activity status: {ACTIVITY_STATUS}') + + +@bot.event +async def on_message(message): + """ + Event handler for when a message is received. + """ + if message.author == bot.user: + return + + if isinstance(message.channel, discord.DMChannel): + # Process DM messages without the @botname requirement + logger.info(f'Received DM: {message.content} | Author: {message.author}') + + if not await check_rate_limit(message.author): + await message.channel.send("Command on cooldown. Please wait before using it again.") + return + + conversation_summary = get_conversation_summary(conversation_history.get(message.author.id, [])) + response = await process_input_message(message.content, message.author, conversation_summary) + await message.channel.send(response) + elif isinstance(message.channel, discord.TextChannel) and message.channel.name in ALLOWED_CHANNELS: + if bot.user in message.mentions: + logger.info(f'Received message: {message.content} | Channel: {message.channel} | Author: {message.author}') + + if not await check_rate_limit(message.author): + await message.channel.send("Command on cooldown. Please wait before using it again.") + return + + conversation_summary = get_conversation_summary(conversation_history.get(message.author.id, [])) + response = await process_input_message(message.content, message.author, conversation_summary) + await message.channel.send(response) + + +async def check_rate_limit(user): + """ + Check if a user has exceeded the rate limit for sending messages. + """ + current_time = time.time() + last_command_timestamp = last_command_timestamps.get(user.id, 0) + last_command_count_user = last_command_count.get(user.id, 0) + + if current_time - last_command_timestamp > RATE_LIMIT_PER: + last_command_timestamps[user.id] = current_time + last_command_count[user.id] = 1 + logger.info(f"Rate limit passed for user: {user}") + return True + + if last_command_count_user < RATE_LIMIT: + last_command_count[user.id] += 1 + logger.info(f"Rate limit passed for user: {user}") + return True + + logger.info(f"Rate limit exceeded for user: {user}") + return False + + +async def process_input_message(input_message, user, conversation_summary): + """ + Process an input message using OpenAI's GPT model. + """ + try: + logger.info("Sending prompt to OpenAI API...") + + conversation = conversation_history.get(user.id, []) + conversation.append({"role": "user", "content": input_message}) + + conversation_tokens = sum(len(message["content"].split()) for message in conversation) + + if conversation_tokens >= GPT_TOKENS * 0.8: + conversation_summary = get_conversation_summary(conversation) + conversation_tokens_summary = sum(len(message["content"].split()) for message in conversation_summary) + max_tokens = GPT_TOKENS - conversation_tokens_summary + else: + max_tokens = GPT_TOKENS - conversation_tokens + + logger.info("Current conversation history:") + logger.info(conversation) # Log the current conversation history + + response = openai.ChatCompletion.create( + model=GPT_MODEL, + messages=[ + {"role": "system", "content": SYSTEM_MESSAGE}, + *conversation_summary, + {"role": "user", "content": input_message} + ], + max_tokens=max_tokens, + timeout=OPENAI_TIMEOUT + ) + + if "choices" in response and response.choices: + response_content = response.choices[0].message.content.strip() + logger.info("Received response from OpenAI API.") + logger.info(f"Sent the response: {response_content}") + + conversation.append({"role": "assistant", "content": response_content}) + conversation_history[user.id] = conversation + + return response_content + else: + logger.error("Failed to get response from OpenAI API.") + return "Sorry, an error occurred while processing the message." + + except ConnectionClosed as error: + logger.error(f"WebSocket connection closed: {error}") + logger.info("Reconnecting in 5 seconds...") + await asyncio.sleep(5) + await bot.login(DISCORD_TOKEN) + await bot.connect(reconnect=True) + except openai.api_error.ApiError as error: + logger.error(f"An error occurred during OpenAI API call: {error}") + return "Sorry, an error occurred while processing the message." + except Exception as error: + logger.error(f"An error occurred while processing the message: {error}") + return "Sorry, an error occurred while processing the message." + + +def get_conversation_summary(conversation): + """ + Get a summary of the conversation by combining user messages and assistant responses. + """ + summary = [] + user_messages = [message for message in conversation if message["role"] == "user"] + assistant_responses = [message for message in conversation if message["role"] == "assistant"] + + # Combine user messages and assistant responses into a summary + for user_message, assistant_response in zip(user_messages, assistant_responses): + summary.append(user_message) + summary.append(assistant_response) + + return summary + + +# Run the bot +bot.run(DISCORD_TOKEN) diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..8e5d802 --- /dev/null +++ b/config.ini.example @@ -0,0 +1,22 @@ +[Discord] +DISCORD_TOKEN = +ALLOWED_CHANNELS = , , ... +BOT_PRESENCE = online +# ACTIVITY_TYPE Options +# playing, streaming, listening, watching, custom, competing +ACTIVITY_TYPE=listening +ACTIVITY_STATUS=Humans + +[OpenAI] +OPENAI_API_KEY = +OPENAI_TIMEOUT=30 +GPT_MODEL=gpt-3.5-turbo +GPT_TOKENS=3072 +SYSTEM_MESSAGE = You are a helpful AI assistant. + +[Limits] +RATE_LIMIT = 2 +RATE_LIMIT_PER = 10 + +[Logging] +LOG_FILE = bot.log diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2715791 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +discord.py==2.2.3 +openai==0.27.8