Skip to content

Commit

Permalink
fix(bot): 🐛 fix bot naming issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Feb 27, 2024
1 parent a3ebfeb commit 5e9d240
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/bot/bot/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ def parse_name(self, name: str) -> str:
:param name: Name of the bot.
:return: Parsed bot name string.
"""
# Lowercase
name = name.lower()

# Prefix
if not name.startswith(self.__plugin.config.name_prefix):
name = self.__plugin.config.name_prefix + name
Expand Down
37 changes: 24 additions & 13 deletions src/bot/bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from mcdreforged.api.event import MCDRPluginEvents
from mcdreforged.api.types import PluginServerInterface, Info
from mcdreforged.api.decorator import event_listener, new_thread
from mcdreforged.minecraft.rtext.style import RColor
from mcdreforged.minecraft.rtext.text import RText, RTextList

if TYPE_CHECKING:
from bot.plugin import Plugin
Expand Down Expand Up @@ -43,32 +45,41 @@ def on_player_joined(
r'\w+\[local] logged in with entity id \d+ at \(.*\)',
info.content
):
# parse name
name = plugin.command_handler.parse_name(player)
if name != player.lower():
message = RText(
f'Warning: Bot "{player}" is not named correctly, '
f'it is suggested to use "{name}" as the name',
color=RColor.yellow
)
server.logger.warning(message)
server.say(message)
return

# debug log
server.logger.debug(f'Bot {player} joined')

# Lowercase
player = player.lower()

# To Bot instance
if plugin.bot_manager.is_in_list(player):
bot = plugin.bot_manager.get_bot(player)
if plugin.bot_manager.is_in_list(name):
bot = plugin.bot_manager.get_bot(name)
else:
bot = plugin.bot_manager.new_bot(
player, plugin.get_location(player)
)
location = plugin.get_location(player)
bot = plugin.bot_manager.new_bot(name, location)

# Spawned handler
bot.spawned()

@staticmethod
@event_listener(MCDRPluginEvents.PLAYER_LEFT)
def on_player_left(server: PluginServerInterface, player: str):
# Lowercase
player = player.lower()
# parse name
name = plugin.command_handler.parse_name(player)

if plugin.bot_manager.is_in_list(player):
server.logger.debug(f'Bot {player} left')
plugin.bot_manager.get_bot(player).set_online(False)
# remove from list
if plugin.bot_manager.is_in_list(name):
server.logger.debug(f'Bot {name} left')
plugin.bot_manager.get_bot(name).set_online(False)
plugin.bot_manager.update_list()

@staticmethod
Expand Down

0 comments on commit 5e9d240

Please sign in to comment.