-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
766 lines (654 loc) · 28 KB
/
bot.py
File metadata and controls
766 lines (654 loc) · 28 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import traceback
from typing import Any, Optional
import discord
from discord import app_commands
from discord.ext import commands
from cai_client import CAIClient
from config import DISCORD_TOKEN
from session_manager import ActiveCharacter, sessions
from webhook_manager import create_webhook, delete_webhook, send_streaming_via_webhook, send_via_webhook
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
def require_login(interaction: discord.Interaction) -> str | None:
return sessions.get_token(interaction.user.id)
def build_search_avatar_url(avatar_file_name: str) -> str:
if not avatar_file_name:
return ""
if avatar_file_name.startswith("http://") or avatar_file_name.startswith("https://"):
return avatar_file_name
cleaned = avatar_file_name.lstrip("/")
return f"https://characterai.io/i/200/static/avatars/{cleaned}?webp=true&anim=0"
async def cai_error_embed(description: str) -> discord.Embed:
return discord.Embed(
title="Error",
description=description,
color=discord.Color.red(),
)
async def cai_success_embed(title: str, description: str = "") -> discord.Embed:
return discord.Embed(
title=title,
description=description,
color=discord.Color.green(),
)
async def spawn_character_to_channel(
*,
token: str,
char_id: str,
channel: discord.TextChannel,
fallback_name: str | None = None,
fallback_avatar_url: str | None = None,
follow_mode: str = "auto",
owner_user_id: int = 0,
) -> tuple[ActiveCharacter, str | None, str | None]:
existing = sessions.get_active(channel.id)
if existing:
await delete_webhook(bot, existing.webhook_id)
sessions.despawn(channel.id)
client = CAIClient(token)
chat_data = await client.start_chat(char_id)
await client.close()
char_name = (chat_data.get("char_name") or "").strip()
if not char_name or char_name == char_id:
char_name = (fallback_name or char_name or "Character").strip()
avatar_url = (chat_data.get("avatar_url") or fallback_avatar_url or "").strip()
greeting = chat_data.get("greeting")
greeting_turn_id = chat_data.get("greeting_turn_id")
chat_id = chat_data["chat_id"]
webhook = await create_webhook(channel, char_name, avatar_url)
active = ActiveCharacter(
char_id=char_id,
chat_id=chat_id,
name=char_name,
avatar_url=avatar_url,
webhook_id=webhook.id,
webhook_url=webhook.url,
follow_mode=follow_mode,
owner_user_id=owner_user_id,
)
sessions.spawn(channel.id, active)
return active, greeting, greeting_turn_id
async def dispatch_character_reply(
*,
channel_id: int,
char: ActiveCharacter,
reply_text: str,
turn_id: Optional[str],
partial_updates: Optional[list[str]] = None,
) -> list[int]:
updates = partial_updates or []
if updates and len(reply_text) <= 1990:
message_ids = await send_streaming_via_webhook(
webhook_url=char.webhook_url,
name=char.name,
avatar_url=char.avatar_url,
partial_updates=updates,
final_content=reply_text,
max_edits=3,
)
else:
message_ids = await send_via_webhook(
webhook_url=char.webhook_url,
name=char.name,
avatar_url=char.avatar_url,
content=reply_text,
)
if turn_id:
sessions.track_bot_message_turn(channel_id, message_ids, str(turn_id))
return message_ids
async def despawn_channel(interaction: discord.Interaction) -> None:
channel = interaction.channel
if not isinstance(channel, discord.TextChannel):
await interaction.followup.send(
embed=await cai_error_embed("This command only works in text channels.")
)
return
char = sessions.get_active(channel.id)
if not char:
await interaction.followup.send(
embed=await cai_error_embed("No character is active in this channel.")
)
return
await delete_webhook(bot, char.webhook_id)
sessions.despawn(channel.id)
await interaction.followup.send(
embed=await cai_success_embed(
f"{char.name} has left the chat.",
"Use `/search` or `/spawn` to bring in another character.",
)
)
class FollowModeView(discord.ui.View):
def __init__(
self,
owner_id: int,
token: str,
character: dict[str, Any],
channel: discord.TextChannel,
):
super().__init__(timeout=180)
self.owner_id = owner_id
self.token = token
self.character = character
self.channel = channel
async def interaction_check(self, interaction: discord.Interaction) -> bool:
if interaction.user.id != self.owner_id:
await interaction.response.send_message("This action belongs to another user.", ephemeral=True)
return False
return True
async def _spawn_with_mode(self, interaction: discord.Interaction, mode: str) -> None:
await interaction.response.defer(ephemeral=True, thinking=True)
try:
active, greeting, greeting_turn_id = await spawn_character_to_channel(
token=self.token,
char_id=str(self.character["char_id"]),
channel=self.channel,
fallback_name=str(self.character.get("name") or "Character"),
fallback_avatar_url=str(self.character.get("avatar_url") or ""),
follow_mode=mode,
owner_user_id=self.owner_id,
)
mode_text = "Auto-follow enabled (responds to every message)." if mode == "auto" else "Reply-only mode enabled (responds only when users reply to bot messages)."
embed = discord.Embed(
title=f"{active.name} spawned",
description=f"Channel: {self.channel.mention}\n{mode_text}",
color=discord.Color.green(),
)
if active.avatar_url:
embed.set_thumbnail(url=active.avatar_url)
await interaction.followup.send(embed=embed, ephemeral=True)
if greeting:
message_ids = await send_via_webhook(
webhook_url=active.webhook_url,
name=active.name,
avatar_url=active.avatar_url,
content=greeting,
)
if greeting_turn_id:
sessions.track_bot_message_turn(self.channel.id, message_ids, greeting_turn_id)
except discord.Forbidden:
await interaction.followup.send(
embed=await cai_error_embed(
f"I need Manage Webhooks permission in {self.channel.mention}."
),
ephemeral=True,
)
except Exception as e:
traceback.print_exc()
await interaction.followup.send(
embed=await cai_error_embed(f"Failed to spawn character: {e}"),
ephemeral=True,
)
@discord.ui.button(label="Yes (auto follow-up)", style=discord.ButtonStyle.success)
async def btn_auto(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._spawn_with_mode(interaction, "auto")
@discord.ui.button(label="No (reply-only)", style=discord.ButtonStyle.secondary)
async def btn_reply(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._spawn_with_mode(interaction, "reply")
class ChannelSpawnView(discord.ui.View):
def __init__(self, owner_id: int, token: str, character: dict[str, Any]):
super().__init__(timeout=180)
self.owner_id = owner_id
self.token = token
self.character = character
async def interaction_check(self, interaction: discord.Interaction) -> bool:
if interaction.user.id != self.owner_id:
await interaction.response.send_message(
"This selector belongs to another user.",
ephemeral=True,
)
return False
return True
@discord.ui.select(
cls=discord.ui.ChannelSelect,
placeholder="Pick a channel to spawn this character",
min_values=1,
max_values=1,
channel_types=[discord.ChannelType.text],
)
async def select_channel(
self,
interaction: discord.Interaction,
select: discord.ui.ChannelSelect,
) -> None:
selected = select.values[0]
selected_id = int(getattr(selected, "id", 0) or 0)
guild_channel = interaction.guild.get_channel(selected_id) if interaction.guild else None
channel = guild_channel if isinstance(guild_channel, discord.TextChannel) else None
if channel is None and isinstance(selected, discord.TextChannel):
channel = selected
if channel is None:
await interaction.response.send_message(
embed=await cai_error_embed("Please pick a text channel."),
ephemeral=True,
)
return
mode_view = FollowModeView(
owner_id=self.owner_id,
token=self.token,
character=self.character,
channel=channel,
)
embed = discord.Embed(
title=f"Follow-up Mode for {self.character.get('name', 'Character')}",
description=(
f"Channel: {channel.mention}\n"
"Yes = bot responds to every message.\n"
"No = bot responds only when users reply to a bot message."
),
color=discord.Color.blurple(),
)
await interaction.response.send_message(embed=embed, view=mode_view, ephemeral=True)
class SearchResultsView(discord.ui.View):
PAGE_SIZE = 5
def __init__(
self,
owner_id: int,
token: str,
query: str,
characters: list[dict[str, Any]],
):
super().__init__(timeout=300)
self.owner_id = owner_id
self.token = token
self.query = query
self.characters = characters
self.page = 0
self.total_pages = max(1, (len(characters) + self.PAGE_SIZE - 1) // self.PAGE_SIZE)
self._refresh_buttons()
async def interaction_check(self, interaction: discord.Interaction) -> bool:
if interaction.user.id != self.owner_id:
await interaction.response.send_message(
"This search view belongs to another user.",
ephemeral=True,
)
return False
return True
def _page_items(self) -> list[dict[str, Any]]:
start = self.page * self.PAGE_SIZE
return self.characters[start : start + self.PAGE_SIZE]
def _refresh_buttons(self) -> None:
items = self._page_items()
self.btn_prev.disabled = self.page == 0
self.btn_next.disabled = self.page >= self.total_pages - 1
number_buttons = [
self.btn_pick1,
self.btn_pick2,
self.btn_pick3,
self.btn_pick4,
self.btn_pick5,
]
for idx, btn in enumerate(number_buttons):
btn.disabled = idx >= len(items)
btn.label = str(idx + 1)
def build_embeds(self) -> list[discord.Embed]:
items = self._page_items()
start_index = self.page * self.PAGE_SIZE
embeds: list[discord.Embed] = []
for idx, char in enumerate(items, start=1):
name = char.get("name", "Unknown")
title = char.get("title") or ""
description = (char.get("description") or "No description.").strip()
chats = int(char.get("participant__num_interactions") or 0)
char_id = char.get("char_id", "—")
avatar_url = char.get("avatar_url") or ""
header = f"{idx}. {name}"
if title:
header += f" — {title}"
embed = discord.Embed(
title=header,
description=(
f"{description[:600]}\n\n"
f"ID: `{char_id}`\n"
f"Chats: {chats:,}"
),
color=discord.Color.blurple(),
)
if avatar_url:
embed.set_image(url=avatar_url)
embed.set_footer(
text=(
f'Query: "{self.query}" | '
f"Page {self.page + 1}/{self.total_pages} | "
f"Result {start_index + idx} of {len(self.characters)}"
)
)
embeds.append(embed)
return embeds
async def _open_channel_picker(self, interaction: discord.Interaction, local_index: int) -> None:
items = self._page_items()
if local_index >= len(items):
await interaction.response.send_message(
embed=await cai_error_embed("That result slot is empty on this page."),
ephemeral=True,
)
return
chosen = items[local_index]
channel_view = ChannelSpawnView(
owner_id=self.owner_id,
token=self.token,
character=chosen,
)
embed = discord.Embed(
title=f"Spawn {chosen.get('name', 'Character')}",
description=(
f"Character ID: `{chosen.get('char_id', '—')}`\n"
"Select the channel where the webhook should be created."
),
color=discord.Color.green(),
)
if chosen.get("avatar_url"):
embed.set_thumbnail(url=chosen["avatar_url"])
await interaction.response.send_message(embed=embed, view=channel_view, ephemeral=True)
@discord.ui.button(label="Prev", style=discord.ButtonStyle.secondary, row=0)
async def btn_prev(self, interaction: discord.Interaction, _: discord.ui.Button):
if self.page > 0:
self.page -= 1
self._refresh_buttons()
await interaction.response.edit_message(embeds=self.build_embeds(), view=self)
@discord.ui.button(label="Next", style=discord.ButtonStyle.secondary, row=0)
async def btn_next(self, interaction: discord.Interaction, _: discord.ui.Button):
if self.page < self.total_pages - 1:
self.page += 1
self._refresh_buttons()
await interaction.response.edit_message(embeds=self.build_embeds(), view=self)
@discord.ui.button(label="1", style=discord.ButtonStyle.primary, row=1)
async def btn_pick1(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._open_channel_picker(interaction, 0)
@discord.ui.button(label="2", style=discord.ButtonStyle.primary, row=1)
async def btn_pick2(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._open_channel_picker(interaction, 1)
@discord.ui.button(label="3", style=discord.ButtonStyle.primary, row=1)
async def btn_pick3(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._open_channel_picker(interaction, 2)
@discord.ui.button(label="4", style=discord.ButtonStyle.primary, row=1)
async def btn_pick4(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._open_channel_picker(interaction, 3)
@discord.ui.button(label="5", style=discord.ButtonStyle.primary, row=1)
async def btn_pick5(self, interaction: discord.Interaction, _: discord.ui.Button):
await self._open_channel_picker(interaction, 4)
@bot.event
async def on_ready():
await bot.tree.sync()
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("Slash commands synced.")
@bot.event
async def on_message(message: discord.Message):
if message.author.bot or message.webhook_id:
return
char = sessions.get_active(message.channel.id)
if char is None:
await bot.process_commands(message)
return
token = sessions.get_token(char.owner_user_id) if char.owner_user_id else sessions.get_token(message.author.id)
if token is None:
await bot.process_commands(message)
return
if char.follow_mode == "reply":
ref_id = message.reference.message_id if message.reference else None
if not ref_id or not sessions.is_tracked_bot_message(message.channel.id, ref_id):
await bot.process_commands(message)
return
try:
client = CAIClient(token)
result = await client.send_message_with_meta(char.char_id, char.chat_id, message.content)
await client.close()
reply = str(result.get("text") or "")
turn_id = result.get("turn_id")
updates = result.get("updates") or []
await dispatch_character_reply(
channel_id=message.channel.id,
char=char,
reply_text=reply,
turn_id=turn_id,
partial_updates=updates,
)
except Exception as e:
await message.channel.send(
embed=await cai_error_embed(f"Failed to get response: {e}"),
delete_after=10,
)
await bot.process_commands(message)
@bot.tree.command(name="login", description="Link your Character.AI account using your email address")
@app_commands.describe(email="Your Character.AI email address")
async def cmd_login(interaction: discord.Interaction, email: str):
await interaction.response.defer(ephemeral=True)
try:
import login as cai_login_flow
from recaptcha import solve_recaptcha
async def on_email_sent(_: str, user_email: str):
await interaction.followup.send(
embed=await cai_success_embed(
"Magic Link Sent",
(
f"Check your inbox for **{user_email}** and click the Character.AI login link.\n"
"Waiting for verification..."
),
),
ephemeral=True,
)
rc_token = await solve_recaptcha()
result = await cai_login_flow.login(email, rc_token, on_email_sent_hook=on_email_sent)
token = result["token"]
client = CAIClient(token)
user_data = await client.validate_token()
await client.close()
username = user_data.get("user", {}).get("user", {}).get("username", "Unknown")
sessions.set_token(interaction.user.id, token)
await interaction.followup.send(
embed=await cai_success_embed(
"Logged in",
(
f"Authenticated as **{username}**.\n"
"Session is stored in memory for this bot runtime."
),
),
ephemeral=True,
)
except TimeoutError:
await interaction.followup.send(
embed=await cai_error_embed("Login timed out before magic link verification."),
ephemeral=True,
)
except Exception as e:
traceback.print_exc()
await interaction.followup.send(
embed=await cai_error_embed(f"Login failed: {e}"),
ephemeral=True,
)
@bot.tree.command(name="logout", description="Remove your Character.AI session")
async def cmd_logout(interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
if not sessions.has_session(interaction.user.id):
await interaction.followup.send(
embed=await cai_error_embed("You are not logged in."),
ephemeral=True,
)
return
sessions.remove_token(interaction.user.id)
await interaction.followup.send(
embed=await cai_success_embed("Logged out", "Your session was removed."),
ephemeral=True,
)
@bot.tree.command(name="search", description="Search for characters on Character.AI")
@app_commands.describe(query="Search term, e.g. Naruto or therapist")
async def cmd_search(interaction: discord.Interaction, query: str):
await interaction.response.defer()
token = require_login(interaction)
if not token:
await interaction.followup.send(
embed=await cai_error_embed("You need to `/login` first."),
ephemeral=True,
)
return
try:
client = CAIClient(token)
raw_chars = await client.search_characters(query)
await client.close()
if not raw_chars:
await interaction.followup.send(
embed=await cai_error_embed(f'No characters found for "{query}".'),
)
return
normalized: list[dict[str, Any]] = []
for char in raw_chars:
avatar_file_name = str(char.get("avatar_file_name") or "")
normalized.append(
{
"char_id": str(char.get("external_id") or ""),
"name": str(char.get("name") or "Unknown"),
"title": str(char.get("title") or ""),
"description": str(char.get("description") or "No description."),
"participant__num_interactions": int(char.get("participant__num_interactions") or 0),
"avatar_url": build_search_avatar_url(avatar_file_name),
}
)
view = SearchResultsView(
owner_id=interaction.user.id,
token=token,
query=query,
characters=normalized,
)
await interaction.followup.send(embeds=view.build_embeds(), view=view)
except Exception as e:
traceback.print_exc()
await interaction.followup.send(
embed=await cai_error_embed(f"Search failed: {e}"),
)
@bot.tree.command(name="spawn", description="Spawn a character as a webhook in this channel")
@app_commands.describe(char_id="Character external_id from /search")
async def cmd_spawn(interaction: discord.Interaction, char_id: str):
token = require_login(interaction)
if not token:
await interaction.response.defer(ephemeral=True)
await interaction.followup.send(
embed=await cai_error_embed("You need to `/login` first."),
ephemeral=True,
)
return
channel = interaction.channel
if not isinstance(channel, discord.TextChannel):
await interaction.response.defer(ephemeral=True)
await interaction.followup.send(
embed=await cai_error_embed("This command only works in text channels."),
ephemeral=True,
)
return
follow_view = FollowModeView(
owner_id=interaction.user.id,
token=token,
character={"char_id": char_id, "name": char_id, "avatar_url": ""},
channel=channel,
)
embed = discord.Embed(
title=f"Follow-up Mode for `{char_id}`",
description=(
f"Channel: {channel.mention}\n"
"Yes = bot responds to every message.\n"
"No = bot responds only when users reply to a bot message."
),
color=discord.Color.blurple(),
)
await interaction.response.send_message(embed=embed, view=follow_view, ephemeral=True)
@bot.tree.command(name="despawn", description="Remove the active character from this channel")
async def cmd_despawn(interaction: discord.Interaction):
await interaction.response.defer()
await despawn_channel(interaction)
@bot.tree.command(name="delete", description="Delete the active webhook from this channel")
async def cmd_delete(interaction: discord.Interaction):
await interaction.response.defer()
await despawn_channel(interaction)
@bot.tree.command(name="chat", description="Send a message to the active character in this channel")
@app_commands.describe(message="What you want to say to the character")
async def cmd_chat(interaction: discord.Interaction, message: str):
await interaction.response.defer()
token = require_login(interaction)
if not token:
await interaction.followup.send(
embed=await cai_error_embed("You need to `/login` first."),
ephemeral=True,
)
return
channel = interaction.channel
if not isinstance(channel, discord.TextChannel):
await interaction.followup.send(
embed=await cai_error_embed("This command only works in text channels.")
)
return
char = sessions.get_active(channel.id)
if not char:
await interaction.followup.send(
embed=await cai_error_embed("No active character here. Use `/search` or `/spawn` first.")
)
return
try:
client = CAIClient(token)
result = await client.send_message_with_meta(char.char_id, char.chat_id, message)
await client.close()
reply = str(result.get("text") or "")
turn_id = result.get("turn_id")
updates = result.get("updates") or []
await interaction.followup.send(f"**You:** {message}", ephemeral=True)
await dispatch_character_reply(
channel_id=channel.id,
char=char,
reply_text=reply,
turn_id=turn_id,
partial_updates=updates,
)
except Exception as e:
traceback.print_exc()
await interaction.followup.send(
embed=await cai_error_embed(f"Failed to get response: {e}")
)
@bot.tree.command(name="regenerate", description="Regenerate a previous bot reply")
@app_commands.describe(message_id="Optional: message ID of a bot reply. If omitted, uses latest bot reply.")
async def cmd_regenerate(interaction: discord.Interaction, message_id: Optional[str] = None):
await interaction.response.defer(ephemeral=True)
token = require_login(interaction)
if not token:
await interaction.followup.send(embed=await cai_error_embed("You need to `/login` first."), ephemeral=True)
return
channel = interaction.channel
if not isinstance(channel, discord.TextChannel):
await interaction.followup.send(embed=await cai_error_embed("This command only works in text channels."), ephemeral=True)
return
char = sessions.get_active(channel.id)
if not char:
await interaction.followup.send(embed=await cai_error_embed("No active character in this channel."), ephemeral=True)
return
target_turn_id: Optional[str] = None
if message_id:
try:
msg_id_int = int(message_id)
except ValueError:
await interaction.followup.send(embed=await cai_error_embed("`message_id` must be a valid integer."), ephemeral=True)
return
target_turn_id = sessions.get_turn_for_message(channel.id, msg_id_int)
else:
target_turn_id = sessions.get_latest_tracked_turn(channel.id)
if not target_turn_id:
await interaction.followup.send(
embed=await cai_error_embed("Could not find a tracked bot reply to regenerate in this channel."),
ephemeral=True,
)
return
try:
client = CAIClient(token)
result = await client.regenerate_turn_candidate(char.char_id, char.chat_id, target_turn_id)
await client.close()
reply = str(result.get("text") or "")
new_turn_id = result.get("turn_id")
message_ids = await send_via_webhook(
webhook_url=char.webhook_url,
name=char.name,
avatar_url=char.avatar_url,
content=reply,
)
if new_turn_id:
sessions.track_bot_message_turn(channel.id, message_ids, str(new_turn_id))
await interaction.followup.send("Regenerated.", ephemeral=True)
except Exception as e:
traceback.print_exc()
await interaction.followup.send(embed=await cai_error_embed(f"Regenerate failed: {e}"), ephemeral=True)
if __name__ == "__main__":
bot.run(DISCORD_TOKEN)