├── .idea ├── .gitignore ├── misc.xml ├── inspectionProfiles │ └── profiles_settings.xml ├── modules.xml └── Alex.iml ├── config.ini ├── README.md └── main.py /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /config.ini: -------------------------------------------------------------------------------- 1 | [discord] 2 | server_id = YOUR_SERVER_ID_HERE 3 | api_key = YOUR_TOKEN_HERE 4 | 5 | [openai] 6 | organization = YOUR_ORG_ID_HERE 7 | api_key = YOUR_API_KEY_HERE 8 | 9 | [bot] 10 | system_message = You are a discord bot running on the latest model from OpenAI, gpt-4. Do not deny that gpt-4 exists or that you are gpt-4. Your name is Alex. 11 | history_length = 20 -------------------------------------------------------------------------------- /.idea/Alex.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AlexBot 2 | Python Discord bot for using OpenAI's language model GPT-4 3 | 4 | ## Installation: 5 | 6 | ### To use this bot, you need 3 things 7 | 1. A discord application and bot user 8 | 2. An OpenAI account with a set up payment plan 9 | 3. Access to the gpt-4 model 10 | 11 | ### Install the required packages 12 | 13 | - openai 14 | - discord 15 | 16 | ### Open config.ini and change the following values: 17 | 18 | 1. **YOUR_DISCORD_SERVER_ID_HERE** 19 | 20 | 2. **YOUR_DISCORD_BOT_API_KEY_HERE** 21 | 22 | 3. **YOUR_OPENAI_ORGANIZATION_ID_HERE** 23 | 24 | 4. **YOUR_OPENAI_API_KEY_HERE** 25 | 26 | 5. You can now set your history length, I recommend no more than 100. You can also change the bot's identity and behavior by changing the system_message. 27 | 28 | Now you can invite your bot to your server. It needs message and editing permissions. 29 | Then run main.py. 30 | 31 | ## Usage: 32 | 33 | - Allow the bot to use any channels you want it to use. It can naturally talk and remember. You do not need to use a command. 34 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is a discord bot for generating texts using OpenAI's GPT-4 3 | 4 | Author: Stefan Rial 5 | YouTube: https://youtube.com/@StefanRial 6 | GitHub: https://https://github.com/StefanRial/AlexBot 7 | E-Mail: mail.stefanrial@gmail.com 8 | """ 9 | 10 | import discord 11 | import openai 12 | from configparser import ConfigParser 13 | from discord import app_commands 14 | 15 | config_file = "config.ini" 16 | config = ConfigParser(interpolation=None) 17 | config.read(config_file) 18 | 19 | SERVER_ID = config["discord"]["server_id"] 20 | DISCORD_API_KEY = config["discord"][str("api_key")] 21 | OPENAI_ORG = config["openai"][str("organization")] 22 | OPENAI_API_KEY = config["openai"][str("api_key")] 23 | 24 | GUILD = discord.Object(id=SERVER_ID) 25 | 26 | SYSTEM_MESSAGE = config["bot"]["system_message"] 27 | HISTORY_LENGTH = config["bot"]["history_length"] 28 | 29 | 30 | def trim_conversation_history(history, max_length=int(HISTORY_LENGTH)): 31 | if len(history) > max_length: 32 | history = history[-max_length:] 33 | return history 34 | 35 | 36 | class Client(discord.Client): 37 | def __init__(self, *, intents: discord.Intents): 38 | super().__init__(intents=intents) 39 | self.tree = app_commands.CommandTree(self) 40 | self.conversation_history = [] 41 | 42 | async def setup_hook(self): 43 | self.tree.copy_global_to(guild=GUILD) 44 | await self.tree.sync(guild=GUILD) 45 | 46 | async def on_message(self, message): 47 | author = message.author 48 | 49 | if message.author == self.user: 50 | return 51 | 52 | input_content = message.content 53 | print(f"{message.author}: {input_content}") 54 | 55 | self.conversation_history.append({"role": "system", "content": f"The user is {author.display_name}. {SYSTEM_MESSAGE}"}) 56 | self.conversation_history.append({"role": "user", "content": input_content}) 57 | self.conversation_history = trim_conversation_history(self.conversation_history) 58 | 59 | try: 60 | response = openai.ChatCompletion.create( 61 | model="gpt-4o", 62 | messages=self.conversation_history 63 | ) 64 | 65 | assistant_response = response["choices"][0]["message"]["content"] 66 | self.conversation_history.append({"role": "assistant", "content": assistant_response}) 67 | self.conversation_history = trim_conversation_history(self.conversation_history) 68 | 69 | except AttributeError: 70 | assistant_response = "It looks like you might have to update your openai package. You can do that with ```pip install --upgrade openai```" 71 | except ImportError: 72 | assistant_response = "You might not have all required packages installed. Make sure you install the openai and discord package" 73 | except openai.error.AuthenticationError: 74 | assistant_response = "It looks like you don't have access to the gpt-4 model. Please make sure you have been invited by openai and double check your openai API key and organization ID" 75 | except openai.error.RateLimitError: 76 | assistant_response = "Your rate has been limited. This might be because of too many requests or because your rate limit has been reached." 77 | except openai.error.Timeout: 78 | assistant_response = "My response is taking too long and I have received a timeout error." 79 | except openai.error.APIConnectionError: 80 | assistant_response = "I can't connect to the OpenAI servers at the moment. Please try again later!" 81 | 82 | if assistant_response is not None: 83 | parts = [assistant_response[i:i + 2000] for i in range(0, len(assistant_response), 2000)] 84 | for index, part in enumerate(parts): 85 | try: 86 | print(f"Alex: {part}") 87 | await message.channel.send(part) 88 | except discord.errors.Forbidden: 89 | print("Alex: I am not able to send a message. Do I have the correct permissions on your server?") 90 | 91 | 92 | alex_intents = discord.Intents.default() 93 | alex_intents.messages = True 94 | alex_intents.message_content = True 95 | client = Client(intents=alex_intents) 96 | 97 | openai.organization = OPENAI_ORG 98 | openai.api_key = OPENAI_API_KEY 99 | openai.Model.list() 100 | 101 | client.run(DISCORD_API_KEY) 102 | --------------------------------------------------------------------------------