Skip to content

Commit

Permalink
Discord Python Bot
Browse files Browse the repository at this point in the history
  • Loading branch information
GiridharRNair committed Apr 11, 2023
0 parents commit 68c5594
Show file tree
Hide file tree
Showing 149 changed files with 31,527 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
token=MTA5MDc4NTA4MjgyODE0MDU4NA.G8sE-m.nqCM23WjchWXcsvAk7420V5kad9UYImZ_Rn4W4
api_key=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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
token={Your Discord bot token}
api_key={Your ChatGPT API Key}
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/DiscordBot.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Discord-ChatGPT-Bot

This Python Discord bot uses the ChatGPT 3.5 Turbo API to generate text and images based on user input.

**Prerequisites** </br>
You need to have your Discord bot set up on the Discord Developer Portal with all intents and necessary permissions enabled, and your OpenAI API key ready.

You can create and configure a Discord bot at https://discord.com/developers/applications, and you can find your personal OpenAI API key at https://platform.openai.com/account/api-keys.

**Installation** </br>
1. Clone this repository: </br>
`git clone https://github.com/yourusername/your-discord-bot.git`
2. Change into the cloned directory: </br>
`cd your-discord-bot`
3. Install the required dependencies: </br>
`pip install -r requirements.txt`
4. Create a .env file with your Discord bot token and ChatGPT API key: </br>
`DISCORD_TOKEN=<your-discord-bot-token>` </br>
`OPENAI_API_KEY=<your-openai-api-key>` </br>
Make sure to reference the .env.example file for guidance.
5. Run the bot: </br>
`python bot.py`

**Usage** </br>
This bot's prefix for text is "!" and its prefix for images is "/". </br>
Use the following commands to interact with the bot:

!chat [prompt] - Generates a response to the given prompt using ChatGPT 3.5 Turbo API.</br>

/image [prompt] - Generates and sends an image based on the given prompt using ChatGPT 3.5 Turbo API.</br>

<img width="640" alt="Screenshot 2023-03-29 at 8 12 26 PM" src="https://user-images.githubusercontent.com/49298134/228702804-1f347893-3baa-4745-94e6-581c62726dc1.png">

<img width="612" alt="Screenshot 2023-03-30 at 11 26 17 AM" src="https://user-images.githubusercontent.com/49298134/228902473-62a8bc12-5ab4-484a-b468-9c96bd88e4a3.png">
50 changes: 50 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import discord
import responses
from dotenv import load_dotenv
import os


def configure():
load_dotenv()


async def send_message(message, user_message, is_private):
try:
if not is_private:
response = responses.get_response(user_message)
else:
response = responses.get_response_private(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)

except Exception as e:
print(e)


def run_discord_bot():
configure()
token = os.getenv('token')
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
print(f'{client.user} is now running!')

@client.event
async def on_message(message):
if message.author == client.user:
return

username = str(message.author)
user_message = str(message.content)
channel = str(message.channel.type)

print(f'{username} said: "{user_message}" ({channel})')

if channel == "private":
await send_message(message, user_message, is_private=True)
else:
await send_message(message, user_message, is_private=False)

client.run(token)
11 changes: 11 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import bot
from dotenv import load_dotenv


def configure():
load_dotenv()


if __name__ == '__main__':
configure()
bot.run_discord_bot()
99 changes: 99 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/asynckit/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 68c5594

Please sign in to comment.