-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (34 loc) · 1.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import discord
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
intents = discord.Intents.default()
client = discord.Client(intents=intents)
def generate_response(message):
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "assistant", "content": message}
]
)
message = completion.choices[0].message.content
return message
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.guild is None: # Check if the message was sent in a DM
async with message.channel.typing():
response = generate_response(message.content)
await message.channel.send(response)
else: # If the message was sent in a guild, only respond if the bot is tagged
if client.user.mentioned_in(message):
async with message.channel.typing():
response = generate_response(message.content)
await message.channel.send(response)
client.run(os.getenv('DISCORD_API_KEY'))