-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (52 loc) · 1.93 KB
/
main.py
File metadata and controls
67 lines (52 loc) · 1.93 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import discord
import requests
import json
from flask import Flask
# Get the OpenAI API key from the environment variables
api_key = os.environ['CHATGPT_API_KEY']
#client = discord.Client()
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
channel = client.get_channel(1056001153747390587)
# Send the message to the channel
await channel.send('Im back to being live! Feel free to ask any questions or just have a chat!')
@client.event
async def on_message(message):
print (message.content)
if message.author == client.user:
return
if message.content.startswith(''):
# Set the prompt and maximum number of tokens
model = 'text-babbage-001'
# Extract the user's question from the message content
#question = message.content[len('prompt'):].strip()
question = message.content
#prompt = 'What is your favorite color?'
max_tokens = 500
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
data = {
'model': model,
'prompt': question,
'max_tokens': max_tokens,
}
print("here")
try:
# Send the request to the OpenAI API
response = requests.post('https://api.openai.com/v1/completions', headers=headers, json=data)
response_object = response.text
response_text = json.loads(response_object)['choices'][0]['text']
await message.channel.send(response_text)
except Exception as e:
# Handle any exceptions that might occur
print(f'An error occurred: {e}')
# Get the Discord bot token from the environment variables
bot_token = os.environ['DISCORD_BOT_TOKEN']
client.run(bot_token)