character-ai-discord-bot/main.py

33 lines
1.0 KiB
Python

import asyncio
import os
import discord
from characterai import aiocai
intents = discord.Intents(messages=True, message_content=True, guilds=True)
client = aiocai.Client(CHAR_AI_TOKEN)
char = CHAT_ID
class MyClient(discord.Client):
channel = discord.channel
async def on_message(self, message) -> None:
if message.channel.id == CHANNEL and message.author.id != BOT_ID:
text = await self.push_to_char_ai(message)
print(text)
await self.channel.send(text.text)
async def push_to_char_ai(self, message) -> str:
async with await client.connect() as chat:
data = await chat.send_message(char=char,
text=f"{message.author.name} says: {message.content}",
chat_id=CHAT_ID)
return data
async def on_ready(self) -> None:
self.channel = self.get_channel(CHANNEL)
bot = MyClient(intents=intents)
bot.run(BOT_TOKEN)