add a history command
This commit is contained in:
@@ -410,6 +410,7 @@ async def _speak_with_bot(
|
||||
user_id=str(ctx.author.id),
|
||||
username=ctx.author.name,
|
||||
content=f"User: {text_to_speak}",
|
||||
bot_name=bot_name,
|
||||
channel_id=str(ctx.channel.id),
|
||||
guild_id=str(ctx.guild.id) if ctx.guild else None,
|
||||
)
|
||||
@@ -420,6 +421,7 @@ async def _speak_with_bot(
|
||||
user_id=str(ctx.bot.user.id),
|
||||
username=ctx.bot.user.name,
|
||||
content=bot_response,
|
||||
bot_name=bot_name,
|
||||
channel_id=str(ctx.channel.id),
|
||||
guild_id=str(ctx.guild.id) if ctx.guild else None,
|
||||
)
|
||||
@@ -551,6 +553,59 @@ async def retcon(ctx: CommandsContext[Bot], *, message: str) -> None:
|
||||
await ctx.send(file=send_img)
|
||||
|
||||
|
||||
@bot.command(name="history")
|
||||
async def history(ctx: CommandsContext[Bot], bot_name: str) -> None:
|
||||
"""View the chat history of a custom bot.
|
||||
|
||||
Usage: !history <bot_name>
|
||||
"""
|
||||
logger.info(
|
||||
"History command triggered by %s for bot %r",
|
||||
ctx.author.name,
|
||||
bot_name,
|
||||
)
|
||||
|
||||
custom_bot_manager = CustomBotManager()
|
||||
bot_info = custom_bot_manager.get_custom_bot(bot_name)
|
||||
|
||||
if not bot_info:
|
||||
await ctx.send(f"Custom bot '{bot_name}' not found.")
|
||||
return
|
||||
|
||||
db = get_database()
|
||||
history = db.get_bot_history(bot_name=bot_name, limit=20)
|
||||
|
||||
if not history:
|
||||
await ctx.send(f"No chat history found for **{bot_name}**. ")
|
||||
return
|
||||
|
||||
history.reverse()
|
||||
|
||||
formatted_history: list[str] = []
|
||||
for user_msg, bot_resp in history:
|
||||
formatted_history.append(user_msg)
|
||||
formatted_history.append(f"{bot_name}: {bot_resp}")
|
||||
|
||||
header = f"Chat History for **{bot_name}**:\n\n"
|
||||
full_text = header + "\n---\n".join(formatted_history)
|
||||
|
||||
chunk_size = 1900
|
||||
chunks: list[str] = []
|
||||
current_chunk = full_text
|
||||
while current_chunk:
|
||||
if len(current_chunk) <= chunk_size:
|
||||
chunks.append(current_chunk)
|
||||
break
|
||||
split_pos = current_chunk.rfind("\n", 0, chunk_size)
|
||||
if split_pos == -1:
|
||||
split_pos = chunk_size
|
||||
chunks.append(current_chunk[:split_pos])
|
||||
current_chunk = current_chunk[split_pos:].lstrip("\n")
|
||||
|
||||
for chunk in chunks:
|
||||
await ctx.send(chunk)
|
||||
|
||||
|
||||
@bot.command(name="talkforme")
|
||||
async def talkforme(ctx: CommandsContext[Bot], *, message: str) -> None:
|
||||
"""Have two bots talk to each other about a topic.
|
||||
@@ -714,6 +769,7 @@ async def handle_chat(
|
||||
user_id=str(ctx.author.id),
|
||||
username=ctx.author.name,
|
||||
content=f"User: {message}",
|
||||
bot_name=bot_name,
|
||||
channel_id=str(ctx.channel.id),
|
||||
guild_id=str(ctx.guild.id) if ctx.guild else None,
|
||||
)
|
||||
@@ -724,6 +780,7 @@ async def handle_chat(
|
||||
user_id=str(ctx.bot.user.id),
|
||||
username=ctx.bot.user.name,
|
||||
content=bot_response,
|
||||
bot_name=bot_name,
|
||||
channel_id=str(ctx.channel.id),
|
||||
guild_id=str(ctx.guild.id) if ctx.guild else None,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user