Skip to content

Commit

Permalink
Add: Add extract nickname utils function
Browse files Browse the repository at this point in the history
  • Loading branch information
whats2000 committed Oct 26, 2024
1 parent feab2ec commit 7e14963
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion bot/commands/tts_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from bot import user_settings
from bot.api.tts_handler import text_to_speech
from bot.utils.extract_user_nickname import extract_user_nickname
from config import GUILD_ID
from utils.logger import logger
from utils.file_utils import load_sample_data, list_characters
Expand Down Expand Up @@ -132,7 +133,8 @@ async def play_tts(self, inter: disnake.ApplicationCommandInteraction, text: str
for attempt in range(1, self.max_retries + 1):
try:
logger.info(f"Fetching TTS audio (attempt {attempt})...")
text = f"{inter.author.display_name} 說: {text}"
player_name = extract_user_nickname(inter.author.display_name)
text = f"{player_name} 說: {text}"
audio_data = text_to_speech(text, character_name)
logger.info("Audio data fetched successfully")
logger.info(f"Audio data length: {len(audio_data)} bytes")
Expand Down
10 changes: 5 additions & 5 deletions bot/events/message_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from disnake.ext import commands
from bot import user_settings
from bot.api.tts_handler import text_to_speech
from bot.utils.extract_user_nickname import extract_user_nickname
from utils.logger import logger
from config import TTS_TARGET_CHANNEL_ID, MESSAGE_BOT_TARGET_USER_ID

Expand Down Expand Up @@ -93,11 +94,10 @@ async def on_message(self, message: disnake.Message):
for attempt in range(1, self.max_retries + 1):
try:
logger.info(f"Fetching TTS audio (attempt {attempt})...")
# Remove the any (any text) the display name
player_name = member.display_name.replace(
re.search(r'\s\(.+?\)', member.display_name).group(),
''
)

# Get the first part of the name (split by space)
player_name = extract_user_nickname(member.display_name)

audio_data = text_to_speech(
f'{player_name} 說: {user_message}',
character_name,
Expand Down
Empty file added bot/utils/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions bot/utils/extract_user_nickname.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import re


def extract_user_nickname(display_name: str) -> str:
"""
Extract user nickname from display name
Args:
display_name (str): Display name of the user
Returns:
str: User nickname
"""
# Remove any text within parentheses () or ()
cleaned_name = re.sub(r'\s*[((].*?[))]\s*', '', display_name)

# Get the first part of the name (split by space)
return cleaned_name.split(' ')[0]
4 changes: 4 additions & 0 deletions data/sample_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"file": "飛霄.wav",
"text": "我曾追尋「流星」的方向,而後得到了自由。但「流星」落下的故地,卻已不見。"
},
"藿藿": {
"file": "藿藿.wav",
"text": "用這令旗,不但調兵遣將能勝人一籌,投降也能先人一步……"
},
"老簡": {
"file": "老簡.m4a",
"text": "云镇工艺为目前亚洲顶尖技术生存服务器之一,活跃于Minecraft原版环境下的技术研究。"
Expand Down
6 changes: 3 additions & 3 deletions data/user_settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"user_settings": {
"495903741354967041": {
"selected_sample": "\u8349\u795e",
"selected_sample": "\u85ff\u85ff",
"tts_enabled": true,
"game_id": "whats2000_"
},
Expand Down Expand Up @@ -78,7 +78,7 @@
"tts_enabled": false
},
"866858877928210445": {
"selected_sample": "\u8349\u795e",
"selected_sample": "\u7518\u96e8",
"tts_enabled": true,
"game_id": "Zero_One_015645"
},
Expand All @@ -87,7 +87,7 @@
},
"854147624604139521": {
"game_id": "BasedSinger5437",
"selected_sample": "\u8349\u795e",
"selected_sample": "\u85ff\u85ff",
"tts_enabled": true
}
}
Expand Down

0 comments on commit 7e14963

Please sign in to comment.