added discord bot

This commit is contained in:
wea_ondara
2024-04-17 21:06:42 +02:00
parent 41d5b4f591
commit 71b25ef337
4 changed files with 47 additions and 12 deletions

18
chat.py
View File

@@ -1,16 +1,16 @@
import requests
from utils.prompt import prompt
class ChatClient:
messages = []
while True:
user_prompt = prompt('>> User: ')
messages.append({'role': 'user', 'content': user_prompt})
response = requests.post('http://localhost:8900/', json=messages)
def input(self, message):
self.messages.append({'role': 'user', 'content': message})
response = requests.post('http://localhost:8900/', json=self.messages)
if response.status_code == 200:
messages = response.json()
print('>> Bot : ' + messages[-1]['content'])
self.messages = response.json()
return self.messages[-1]['content']
else:
messages = messages[0:-1]
print('### Error from backend')
self.messages = self.messages[0:-1]
return '### Error from backend'

8
chat_cli.py Normal file
View File

@@ -0,0 +1,8 @@
from chat import ChatClient
from utils.prompt import prompt
client = ChatClient()
while True:
user_prompt = prompt('>> User: ')
response = client.input(user_prompt)
print(response)

26
discord_bot.py Normal file
View File

@@ -0,0 +1,26 @@
import discord
from chat import ChatClient
from discord_bot_token import discord_bot_token
intents = discord.Intents.default()
intents.message_content = True
discord_client = discord.Client(intents=intents)
chat_client = ChatClient()
@discord_client.event
async def on_ready():
print(f'We have logged in as {discord_client.user}')
@discord_client.event
async def on_message(message):
if message.author == discord_client.user:
return
response = chat_client.input(message.content)
await message.channel.send(response)
discord_client.run(discord_bot_token)

1
discord_bot_token.py Normal file
View File

@@ -0,0 +1 @@
discord_bot_token = '<token here>'