-
Notifications
You must be signed in to change notification settings - Fork 0
/
caibot.py
60 lines (42 loc) · 1.62 KB
/
caibot.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
# bot.py
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
DISCORD_TOKEN = "" #given when making a Discord bot
import asyncio
import random
from characterai import PyAsyncCAI
CAI_TOKEN = "" #In the URL of the character's chat in character.ai
CAI_ID = "" #See kramcat's documentation on how to get this
load_dotenv() #load environment variables
intents = discord.Intents.default()
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if (message.author.id != client.user.id):
if client.user.mentioned_in(message):
print("generating response...")
async with message.channel.typing():
mssg = (str(message.content))
CAIclient = PyAsyncCAI(CAI_TOKEN)
await CAIclient.start()
char = CAI_ID
chat = await CAIclient.chat.get_chat(char)
history_id = chat['external_id']
participants = chat['participants']
if not participants[0]['is_human']:
tgt = participants[0]['user']['username']
else:
tgt = participants[1]['user']['username']
data = await CAIclient.chat.send_message(
char, mssg, history_external_id=history_id, tgt=tgt
)
botmssg = data['replies'][0]['text']
await message.channel.send(f"{botmssg}")
client.run(DISCORD_TOKEN)