added backend which maintains ai instances;
added frontend as control panel
This commit is contained in:
49
backend/ai/services/DcClient.py
Normal file
49
backend/ai/services/DcClient.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from threading import Thread
|
||||
|
||||
from discord import Intents, Client
|
||||
|
||||
from . import AiInstance
|
||||
|
||||
|
||||
class DcClient:
|
||||
_intents: Intents
|
||||
_discord_client: Client
|
||||
|
||||
_ai_instance: AiInstance
|
||||
|
||||
def __init__(self, _ai_instance: AiInstance):
|
||||
self._ai_instance = _ai_instance
|
||||
|
||||
self._intents = Intents.default()
|
||||
self._intents.message_content = True
|
||||
self._discord_client = Client(intents=self._intents)
|
||||
|
||||
@self._discord_client.event
|
||||
async def on_ready():
|
||||
print(f'We have logged in as {self._discord_client.user}')
|
||||
|
||||
@self._discord_client.event
|
||||
async def on_message(message):
|
||||
if not self._ai_instance.discord.react_to_chat:
|
||||
return None
|
||||
|
||||
if message.author == self._discord_client.user:
|
||||
return
|
||||
|
||||
if message.content.isspace():
|
||||
await message.channel.send('### Empty message')
|
||||
return
|
||||
|
||||
response = self._ai_instance.chat_text(message.author.name, message.content)
|
||||
if response is not None:
|
||||
await message.channel.send(response['content'])
|
||||
|
||||
def connect(self):
|
||||
def start():
|
||||
self._discord_client.run(self._ai_instance.configuration.discord_token)
|
||||
|
||||
thread = Thread(target=start)
|
||||
thread.start()
|
||||
|
||||
def disconnect(self):
|
||||
self._discord_client.close()
|
||||
Reference in New Issue
Block a user