forked from TheExplainthis/ChatGPT-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (47 loc) · 2.03 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from dotenv import load_dotenv
import discord
from src.discordBot import DiscordClient, Sender
from src.logger import logger
from src.chatgpt import ChatGPT, DALLE
from src.models import OpenAIModel
from src.memory import Memory
from src.server import keep_alive
load_dotenv()
models = OpenAIModel(api_key=os.getenv('OPENAI_API'), model_engine=os.getenv('OPENAI_MODEL_ENGINE'))
memory = Memory(system_message=os.getenv('SYSTEM_MESSAGE'))
chatgpt = ChatGPT(models, memory)
dalle = DALLE(models)
def run():
client = DiscordClient()
sender = Sender()
@client.tree.command(name="chat", description="Have a chat with ChatGPT")
async def chat(interaction: discord.Interaction, *, message: str):
user_id = interaction.user.id
if interaction.user == client.user:
return
await interaction.response.defer()
receive = chatgpt.get_response(user_id, message)
await sender.send_message(interaction, message, receive)
@client.tree.command(name="imagine", description="Generate image from text")
async def imagine(interaction: discord.Interaction, *, prompt: str):
if interaction.user == client.user:
return
await interaction.response.defer()
image_url = dalle.generate(prompt)
await sender.send_image(interaction, prompt, image_url)
@client.tree.command(name="reset", description="Reset ChatGPT conversation history")
async def reset(interaction: discord.Interaction):
user_id = interaction.user.id
logger.info(f"resetting memory from {user_id}")
try:
chatgpt.clean_history(user_id)
await interaction.response.defer(ephemeral=True)
await interaction.followup.send(f'> Reset ChatGPT conversation history < - <@{user_id}>')
except Exception as e:
logger.error(f"Error resetting memory: {e}")
await interaction.followup.send('> Oops! Something went wrong. <')
client.run(os.getenv('DISCORD_TOKEN'))
if __name__ == '__main__':
keep_alive()
run()