Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions interaction/chat.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from discord import Bot, Message
from discord import Bot, Message, TextChannel, Thread

from llm.llm import get_llm_service
from utils.send_like_human import send_like_human


async def handle_chat(bot: Bot, message: Message) -> None:
user = message.author
bot_user = bot.user

if bot_user is None or bot_user == user or user.bot:
return
return None

if bot_user not in message.mentions:
return
return None

content = message.content
for mention_user in message.mentions:
Expand All @@ -25,17 +27,22 @@ async def handle_chat(bot: Bot, message: Message) -> None:
llm_service = get_llm_service()
except ValueError as e:
await message.reply(f"聊天功能無法使用: {str(e)}")
return
return None
except Exception as e:
await message.reply(f"初始化聊天服務時發生錯誤: {str(e)}")
return
return None

try:
async with message.channel.typing():
response = await llm_service.process_message(
message=message,
)
await message.reply(response, mention_author=False)

if not isinstance(message.channel, (TextChannel, Thread)):
await message.reply("無法回覆訊息: 頻道類型不受支持")
return None

await send_like_human(message.channel, response)
except Exception as e:
error_msg = f"抱歉,處理訊息時發生錯誤: {str(e)}"
await message.reply(error_msg)
3 changes: 2 additions & 1 deletion llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from db import get_db
from repository.chat_repository import ChatRepository
from tools import AVAILABLE_TOOLS
from utils.send_like_human import send_like_human

from .config import OpenAIConfig
from .filter import post_filter
Expand Down Expand Up @@ -296,7 +297,7 @@ async def system_event_callback(
content=response,
)

await text_channel.send(response)
await send_like_human(text_channel, response)


# pylint: disable=invalid-name
Expand Down
20 changes: 20 additions & 0 deletions utils/send_like_human.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from discord import TextChannel, Thread

from asyncio import sleep as asleep
from random import random
from typing import Union


async def send_like_human(
channel: Union[TextChannel, Thread],
content: str
) -> None:
message_list = content.split("\n\n")
for i, msg in enumerate(message_list):
await channel.send(msg)

if i >= len(message_list) - 1:
break

async with channel.typing():
await asleep(random() + 0.3)
Loading