2
2
import asyncio
3
3
import aiohttp
4
4
import websockets
5
- from characterai import aiocai
6
- from apicode import aiclient , token
7
- from imagetotext import ImageCaptioning
8
- from animeani import AnilistHandler
9
-
10
- # WebSocket URL and token
11
- ws_url = 'wss://gateway.discord.gg/?v=6&encoding=json'
12
- token = token # apicode.py
13
-
14
- # Initialize ImageCaptioning class and Discord sender
15
- image_captioning = ImageCaptioning ()
16
-
17
- # ANSI escape codes for color
18
- RESET = '\033 [0m'
19
- GREEN = '\033 [92m'
20
- RED = '\033 [91m'
21
- YELLOW = '\033 [93m'
22
- CYAN = '\033 [96m'
23
- BLACK = '\033 [30m'
24
-
25
-
26
- class CommandHandler :
27
- def __init__ (self ):
28
- # Initialize command handlers
29
- self .handlers = {
30
- "-anime" : self .handle_anime , # anime info
31
- "-manga" : self .handle_manga , # manga info
32
- "-ch" : self .handle_character , # character info
33
- "-rina" : self .handle_rina , # (AI) command
34
- }
35
-
36
- # Initialize instances of handler classes
37
- self .anilist_handler = AnilistHandler ()
38
-
39
- async def handle_command (self , command , args ):
40
- """
41
- Handle a command by calling the appropriate method.
42
- """
43
- if command in self .handlers :
44
- await self .handlers [command ](args )
45
- else :
46
- await print_response ("unknown_command" , f"Unknown command: { command } " )
47
-
48
- async def handle_anime (self , args ):
49
- """
50
- Handle the -anime command.
51
- """
52
- self .anilist_handler .print_anime_info (args )
53
- await print_response ("anime_info" , f"Fetched anime info for: { args } " )
54
-
55
- async def handle_manga (self , args ):
56
- """
57
- Handle the -manga command.
58
- """
59
- self .anilist_handler .print_manga_info (args )
60
- await print_response ("manga_info" , f"Fetched manga info for: { args } " )
61
-
62
- async def handle_character (self , args ):
63
- """
64
- Handle the -character command.
65
- """
66
- self .anilist_handler .print_character_info (args )
67
- await print_response ("character_info" , f"Fetched character info for: { args } " )
68
-
69
- async def handle_rina (self , args ):
70
- """
71
- Handle the -rina command by initializing Rina's AI and responding.
72
- """
73
- rina_ai = AIPart ('gc6qOU5zms07_eFoWdGWKCUlGxmHEVIBj33ZhNfUxY0' , aiclient ) # Assuming correct character ID and API key
74
- await rina_ai .initialize_chat () # Initialize the AI chat session
75
- response = await rina_ai .process_incoming_message (args )
76
- await print_response ("rina_response" , f"Rina: { response } " ) # Output Rina's response in the console
77
-
78
-
79
- class AIPart :
80
- def __init__ (self , char , api_key ):
81
- """
82
- Initialize the AIPart class with the character ID and API key.
83
- """
84
- self .char = char
85
- self .client = aiocai .Client (api_key )
86
- self .chat = None
87
- self .chat_id = None
88
-
89
- async def initialize_chat (self ):
90
- """
91
- Set up a new chat session with the Character AI and store the chat ID.
92
- """
93
- me = await self .client .get_me ()
94
- chat = await self .client .connect ()
95
- new , answer = await chat .new_chat (self .char , me .id )
96
- self .chat = chat
97
- self .chat_id = new .chat_id
98
- await print_response ("chat_init" , f'{ answer .name } : { answer .text } ' )
99
-
100
- async def handle_message (self , text ):
101
- """
102
- Send a message to the Character AI and receive a response.
103
- """
104
- try :
105
- message = await self .chat .send_message (self .char , self .chat_id , text )
106
- return f'{ message .name } : { message .text } '
107
- except Exception as e :
108
- return f"An error occurred: { e } "
109
-
110
- async def process_incoming_message (self , text ):
111
- """
112
- Process an incoming message by sending it to the Character AI and returning the response.
113
- """
114
- response = await self .handle_message (text )
115
- return response
116
-
117
-
118
- async def get_json_request (ws , request ):
119
- """
120
- Send a JSON request to the WebSocket server.
121
- """
122
- await ws .send (json .dumps (request ))
123
-
124
-
125
- async def got_json_response (ws ):
126
- """
127
- Receive and parse a JSON response from the WebSocket server.
128
- """
129
- response = await ws .recv ()
130
- if response :
131
- return json .loads (response )
132
- return None
133
-
134
-
135
- async def heartbeat (ws , interval ):
136
- """
137
- Send a heartbeat to the WebSocket server to keep the connection alive.
138
- """
139
- await print_response ("heartbeat" , "Heartbeat begin" )
140
- while True :
141
- await asyncio .sleep (interval )
142
- heartbeatJSON = {
143
- "op" : 1 ,
144
- "d" : None
145
- }
146
- await get_json_request (ws , heartbeatJSON )
147
- await print_response ("heartbeat" , "Heartbeat sent" )
148
-
149
-
150
- async def connect ():
151
- """
152
- Connect to the WebSocket server and start the event loop.
153
- """
154
- async with websockets .connect (ws_url ) as ws :
155
- await print_response ("websocket" , "WebSocket connected" )
156
-
157
- event = await got_json_response (ws )
158
- if event and event .get ('op' ) == 10 :
159
- heartbeat_interval = event ['d' ]['heartbeat_interval' ] / 1000
160
-
161
- payload = {
162
- 'op' : 2 ,
163
- 'd' : {
164
- import json
165
- import asyncio
166
- import aiohttp
167
- import websockets
168
5
import requests
169
6
from characterai import aiocai
170
7
from apicode import aiclient , token
@@ -214,7 +51,7 @@ async def handle_anime(self, args):
214
51
Handle the -anime command.
215
52
"""
216
53
if args :
217
- anime_info = self .anilist_handler .print_anime_info (args ) # Assuming print_anime_info returns info
54
+ anime_info = self .anilist_handler .print_anime_info (args ) # print_anime_info returns info
218
55
self .discord_sender .send_tagged_message ("anime_info" , anime_info )
219
56
else :
220
57
self .discord_sender .send_tagged_message ("anime_info" , "No anime name provided." )
@@ -224,7 +61,7 @@ async def handle_manga(self, args):
224
61
Handle the -manga command.
225
62
"""
226
63
if args :
227
- manga_info = self .anilist_handler .print_manga_info (args ) # Assuming print_manga_info returns info
64
+ manga_info = self .anilist_handler .print_manga_info (args ) # print_manga_info returns info
228
65
self .discord_sender .send_tagged_message ("manga_info" , manga_info )
229
66
else :
230
67
self .discord_sender .send_tagged_message ("manga_info" , "No manga name provided." )
@@ -234,7 +71,7 @@ async def handle_character(self, args):
234
71
Handle the -character command.
235
72
"""
236
73
if args :
237
- character_info = self .anilist_handler .print_character_info (args ) # Assuming print_character_info returns info
74
+ character_info = self .anilist_handler .print_character_info (args ) # print_character_info returns info
238
75
self .discord_sender .send_tagged_message ("character_info" , character_info )
239
76
else :
240
77
self .discord_sender .send_tagged_message ("character_info" , "No character name provided." )
@@ -243,7 +80,7 @@ async def handle_rina(self, args):
243
80
"""
244
81
Handle the -rina command by initializing Rina's AI and responding.
245
82
"""
246
- rina_ai = AIPart ('gc6qOU5zms07_eFoWdGWKCUlGxmHEVIBj33ZhNfUxY0' , aiclient ) # Assuming correct character ID and API key
83
+ rina_ai = AIPart ('gc6qOU5zms07_eFoWdGWKCUlGxmHEVIBj33ZhNfUxY0' , aiclient ) # correct character ID and API key
247
84
await rina_ai .initialize_chat () # Initialize the AI chat session
248
85
response = await rina_ai .process_incoming_message (args )
249
86
self .discord_sender .send_tagged_message ("rina_response" , response ) # Send Rina's response to Discord
@@ -410,7 +247,7 @@ async def event_loop(ws):
410
247
411
248
class DiscordSender :
412
249
def __init__ (self , bot_token ):
413
- self .url = f'https://discord.com/api/v10/channels/741223003261763646/messages' # Updated API version
250
+ self .url = f'https://discord.com/api/v10/channels/741223003261763646/messages'
414
251
self .headers = {
415
252
'Authorization' : token ,
416
253
'Content-Type' : 'application/json'
0 commit comments