Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 52bc040

Browse files
Merge pull request #755 from Alby084/feature/chat-gpt-discord-bot
Feature: ChatGPT and DALL·E Discord Bot for issue #683
2 parents 6100185 + 14e7346 commit 52bc040

File tree

6 files changed

+873
-0
lines changed

6 files changed

+873
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from openai import OpenAI
2+
from dotenv import load_dotenv
3+
import os
4+
5+
load_dotenv(override=True)
6+
7+
gpt_api_key = os.getenv("GPT_API_KEY")
8+
9+
def gpt(model: str, prompt: str, sys_prompt: str, temp: float):
10+
client = OpenAI(api_key= gpt_api_key)
11+
response = client.chat.completions.create(
12+
model = model,
13+
messages=[
14+
{
15+
"role": "system",
16+
"content": sys_prompt
17+
},
18+
{
19+
"role": "user",
20+
"content": prompt
21+
}
22+
],
23+
temperature = temp,
24+
# max_tokens=64,
25+
top_p=1
26+
)
27+
output = response.choices[0].message.content.strip()
28+
return output
29+
30+
def dalle3(prompt: str, quality: str, size: str, style: str):
31+
client = OpenAI(api_key= gpt_api_key)
32+
response = client.images.generate(
33+
model = "dall-e-3",
34+
prompt = prompt,
35+
size = size,
36+
quality = quality,
37+
style = style,
38+
n=1,
39+
)
40+
image_url = response.data[0].url
41+
return image_url
42+
43+
def dalle2(prompt: str, size: str):
44+
client = OpenAI(api_key= gpt_api_key)
45+
response = client.images.generate(
46+
model = "dall-e-2",
47+
prompt = prompt,
48+
size = size,
49+
n=1,
50+
)
51+
image_url = response.data[0].url
52+
return image_url
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"system_content": [
3+
{
4+
"character_limit_prompt": " Your response must not exceed 4096 characters",
5+
"correct_grammar": "You will be provided with user text, and your task is to only grammar check the user text and to also provide information on what you changed.",
6+
"single_page_website": "Only respond to questions about making a single paged website, Ignoring other unrelated questions. Outputted code should be encased by markdown syntax. Your response should include HTML code with embedded javascript and CSS.",
7+
"text_to_emoji": "You will be provided with a message, and your task is to convert each word in that user text into the appropriate emojis. Do not respond in words, only respond in emojis. Ignore punctuation",
8+
"text_to_block_letters":"Your task is to convert a given message into a sequence of Discord regional indicator emojis and selected punctuation emojis. For each letter in the message, add ':regional_indicator_' before the letter and ':' after it. For exclamation points '!' and question marks '?', simply add ':' before and after the punctuation. Remove all other punctuation and characters that are not letters, exclamation points, or question marks. Preserve the original spacing between words. Here are some examples of the desired output format: ':regional_indicator_h::regional_indicator_e::regional_indicator_l::regional_indicator_l::regional_indicator_o: :regional_indicator_h::regional_indicator_o::regional_indicator_w: :regional_indicator_a::regional_indicator_r::regional_indicator_e: :regional_indicator_y::regional_indicator_o::regional_indicator_u::question:', ':regional_indicator_l::regional_indicator_e::regional_indicator_t::regional_indicator_s: :regional_indicator_g::regional_indicator_o::exclamation:'. Please strictly adhere to this format in your responses and do not deviate from or elaborate on the given instructions.",
9+
"code_debug":"You will be provided with a piece of code in any programming language. Your task is to analyze the code, identify any potential bugs or issues, and fix them. If the code appears to be working correctly, explain how it works and offer any possible improvements or optimizations. Outputted code should be encased by markdown syntax. Ignore questions that don't contain code.",
10+
"short_story":"You are a creative writing assistant capable of generating engaging short stories. When the user provides a topic, create a well-structured, coherent story of approximately 200-300 words. Use vivid descriptions, interesting characters, and a compelling plot to bring the story to life. Adapt your writing style to suit the given topic and ensure a satisfying conclusion.",
11+
"general_questions":"You are ChatGPT, a friendly, knowledgeable, and capable AI assistant with extensive knowledge across many domains and excellent language skills. Engage in conversations on a wide range of topics, provide helpful information, and assist with various tasks such as writing, analysis, and brainstorming. Adapt your communication style to suit each user, be attentive and responsive to their needs, provide explanations and context when appropriate, share original thoughts while acknowledging differing views, encourage critical thinking, mirror the user's language style, check if responses meet expectations, and express friendly enthusiasm for their interests and ideas."
12+
}
13+
]
14+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Requirements
2+
3+
This project requires python version 3.12.3 discord.py version 2.3.2, OpenAI version 1.30.1 and python-dotenv version 1.0.1 which can be installed via `requirements.txt`:
4+
5+
```bash
6+
pip install -r requirements.txt
7+
```
8+
9+
### Manually install requirements:
10+
11+
```bash
12+
pip install discord.py~=2.3.2
13+
```
14+
15+
```bash
16+
pip install openai~=1.30.1
17+
```
18+
19+
```bash
20+
pip install python-dotenv~=1.0.1
21+
```
22+
23+
# ``.env`` File settup
24+
25+
1. Create a ``.env`` file in the bot's root folder. (Same folder as main.py)
26+
2. Format the ``.env`` file as follows, replacing ``TOKEN_HERE``, ``OWNER_UID_HERE``, ``CHAT_GPT_API_KEY_HERE``, ``DISCORD_SERVER_1`` and ``DISCORD_SERVER_2`` with your bot's token, your Discord UID, ChatGPT API key and discord server ID's respectively. Keeping the double quotation marks.
27+
```text
28+
BOT_TOKEN = "TOKEN_HERE"
29+
OWNER_ID = "OWNER_UID_HERE"
30+
GPT_API_KEY = "CHAT_GPT_API_KEY_HERE"
31+
DISCORD_SERVER_1 = "FIRST_DISCORD_SERVER_ID_HERE"
32+
DISCORD_SERVER_2 = "SECOND_DISCORD_SERVER_ID_HERE"
33+
```
34+
For example:
35+
```text
36+
BOT_TOKEN = "12ab56c89"
37+
OWNER_ID = "123456789"
38+
GPT_API_KEY = "12ab56c89"
39+
DISCORD_SERVER_1 = "123456789"
40+
DISCORD_SERVER_2 = "123456789"
41+
```
42+
``OWNER_ID``, ``DISCORD_SERVER_1`` and ``DISCORD_SERVER_2`` must be a numeric whole value
43+
44+
3. The ``.gitignore`` file will ignore the ``.env``.<br>
45+
46+
### Note:
47+
48+
If you want to change the location of the ``.env`` file, you will need to make a reference for it by adding:
49+
```python
50+
dotenv_path = os.path.join("path/to/env", ".env")
51+
```
52+
53+
above ``load_dotenv(override=True)`` and update ``load_dotenv(override=True)`` to:
54+
```python
55+
load_dotenv(dotenv_path, override=True)
56+
```
57+
58+
If you want to change your ``.env`` file name as well add this reference to the ``.env``:
59+
```python
60+
dotenv_path = os.path.join("path/to/env", "Env_Name_Here.env")
61+
```
62+
63+
# How to run
64+
65+
Open a new command line in the same folder as the main.py script (Make sure python is installed and/or your python venv is active) and type:
66+
```bash
67+
python main.py
68+
```
69+
70+
# Creating a discord bot application and getting bot token
71+
1. Visit to the discord developer portal applications page [Here](https://discord.com/developers/applications).
72+
2. Click the ``New Application`` button at the top right of the page next to your discord profile picture.
73+
3. Name your application and click create. This will be used as the default username for your bot.
74+
4. Navigate to the ``Bot`` page on the left navigation pannel as shown below:
75+
<img src="https://github.com/Alby084/Discord-GPT-Api-Bot/assets/99786431/1d95f080-b1a7-4be2-acce-c789237f5622" alt="drawing" width="220"/>
76+
77+
5. Change your bot's username under ``USERNAME`` if desired.
78+
6. Click the ``Reset Token`` button and copy your bots token for use in the ``.env`` file. You will have to reset the bot token to view and copy it again.
79+
7. Navigate to the ``OAuth2`` page on the left navigation pannel as shown below:
80+
<img src="https://github.com/Alby084/Discord-GPT-Api-Bot/assets/99786431/18dc9f85-380e-4dc3-b6e8-ba29f31ed5a8" alt="drawing" width="220"/>
81+
82+
8. Select ``bot`` under ``OAuth2 URL Generator`` ``SCOPES`` and select ``Administrator`` under ``BOT PERMISSIONS`` ``GENERAL PERMISSIONS``.
83+
9. Copy the generated discord link under ``GENERATED URL`` at the bottom of the page and paste this link into your web browsers address bar.
84+
10. Follow the prompts to add your bot to any discord server where you have the ``Manage Server`` or ``Administrator`` permission.
85+
86+
# Getting ID's and GPT API key
87+
88+
### Getting your discord UID
89+
1. On Discord, go to Settings > Advanced
90+
2. Scroll down and make sure that Developer Mode is **on**
91+
3. Exit settings and left click on your profile picture at the bottom left of discord (same place as the settings button) and click ``Copy User ID`` as shown below:
92+
93+
<img src="https://github.com/Alby084/Discord-GPT-Api-Bot/assets/99786431/a6de8ccb-206b-4656-abe2-35bb36751f7f" alt="drawing" width="380"/> <br>
94+
95+
### Getting discord server ID
96+
1. On Discord, go to Settings > Advanced
97+
2. Scroll down and make sure that Developer Mode is **on**
98+
3. Exit settings and right click on the server(s) your bot is in
99+
and click ``Copy Server ID`` as shown below:
100+
101+
<img src="https://github.com/Alby084/python-beginner-projects/assets/99786431/cd4e8349-b916-4f51-adc4-fd774465483f" alt="drawing" width="220"/> <br>
102+
103+
### Getting chat GPT API key
104+
1. Visit the openai playground website settings page [Here](https://platform.openai.com/settings/organization/general).
105+
2. Click the ``Create Project`` button at the bottom of the settings list as shown below:
106+
<img src="https://github.com/Alby084/Discord-GPT-Api-Bot/assets/99786431/867e01a4-e628-4a36-9436-a56f5c30c12d" alt="drawing" width="220"/>
107+
108+
3. Name your project and click the ``Create`` button.
109+
4. Navigate to the ``API keys`` page above the settings page on the left side navigation pannel. Alternatively you can click [Here](https://platform.openai.com/api-keys).
110+
5. Click the ``Create new secret key`` button.
111+
6. Choose ``You`` under ``Owned by``. Name your API key something descriptive and select the new project you just created in the ``Project`` dropdown. Set ``Permissions`` as ``All`` and click the ``Create secret key`` button as shown below:
112+
113+
<img src="https://github.com/Alby084/Discord-GPT-Api-Bot/assets/99786431/29ee99c3-5187-4082-b833-3c80580d0b4b" alt="drawing" width="220"/> <br>
114+
115+
116+
### Note on GPT ``Credit balance``:
117+
Ensure you have an available ``Credit balance``. You can check on the ``Billing`` page in ``Settings`` or by clicking [Here](https://platform.openai.com/settings/organization/billing/overview). If you do not have a ``Credit balance`` you will need to add money (credit) to your account otherwise this discord bot's chat GPT functionality will not work.

0 commit comments

Comments
 (0)