File size: 1,547 Bytes
ec83114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
import "dotenv/config";
import { ActivityType, Client, GatewayIntentBits } from "discord.js";
import CharacterAI = require("node_characterai");
const {
  Guilds,
  MessageContent,
  GuildMessages,
  GuildMembers,
  GuildVoiceStates,
} = GatewayIntentBits;
const client = new Client({
  intents: [
    Guilds,
    MessageContent,
    GuildMessages,
    GuildMembers,
    GuildVoiceStates,
  ],
});
const characterAI = new CharacterAI();

const fs = require("fs");
const eventFiles = fs
  .readdirSync("./events")
  .filter((file: any) => file.endsWith(".ts"));

let chatClient: any;
let characterName: any;
const connectCai = async () => {
  try {
    await characterAI.authenticateWithToken(process.env.ACCESS_TOKEN);
  } catch (error) {
    console.log(error);
  }
  try {
    chatClient = await characterAI.createOrContinueChat(
      process.env.CHARACTER_ID
    );
    let characterinfo = await characterAI.fetchCharacterInfo(
      process.env.CHARACTER_ID
    );
    characterName = characterinfo.name;
    await client.user.setPresence({
      activities: [
        { name: `${characterinfo.name}`, type: ActivityType.Playing },
      ],
    });
    for (const file of eventFiles) {
      const event = require(`./events/${file}`);
      const eventName = file.split(".")[0]; // Assuming your files are named "eventname.js"
      client.on(eventName, (message) => {
        event(client, message, chatClient, characterName);
      });
    }
  } catch (error) {
    console.log(error);
  }
};

connectCai();
client.login(process.env.TOKEN);