-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
89 lines (66 loc) · 2.57 KB
/
bot.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import sys
import discord
import gptobject as go
import httpx
import asyncio
#get openai api key and Discord bot token key from the environmental variables
try:
openaikey = os.environ["OPENAIKEY"]
discordbotkey = os.environ["DISCORDBOTKEY"]
except KeyError as e:
print(f"Error: The environment variable '{e}' is not set correctly, please set it right and try again.")
sys.exit()
# openai endpoint
endpoint = 'https://api.openai.com/v1/chat/completions'
# openai model
model = "gpt-4o"
#response tokens
max_tokens = 2000
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
# On bot ready
@client.event
async def on_ready():
print(f"Bot has logged in as {client.user}")
# Handling messages with typing indicator
@client.event
async def on_message(message):
if message.author == client.user:
return
# Check if the message starts with ">"
if message.content.startswith('>'):
# Extract the sentence by removing the leading ">" character
sentence = message.content[1:]
try:
async with message.channel.typing():
# Call the async chatgpt function
answer = await go.chatgptAsync(sentence, endpoint, model, max_tokens, openaikey)
# Send the response in chunks every 300 milliseconds
for msg in answer:
await message.channel.send(msg)
#wait for 300 milliseconds
await asyncio.sleep(0.3)
except httpx.HTTPStatusError as e:
if e.response.status_code == 400:
await message.channel.send("Error: The user prompt exceeds the maximum tokens allowed.")
else:
await message.channel.send(f"An HTTP error occurred: {e.response.status_code}")
except discord.errors.HTTPException as e:
await message.channel.send(f"Discord HTTP Exception the response exceed 2000 characters try again with a different prompt: {e}")
except Exception as e:
await message.channel.send(f"An unexpected error occurred: {e}")
else:
await message.channel.send("Every prompt need to start with a greater than sign '>'. Try again")
async def main():
try:
await client.start(discordbotkey)
except discord.errors.PrivilegedIntentsRequired as e:
print(f"Error: Privileged intents are required. {e}")
sys.exit(1)
except discord.errors.LoginFailure as e:
print(f"Login failed: {e}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())