Add channel name to the messages, fix things.

This commit is contained in:
Fjerkroa Auto 2023-03-22 23:28:32 +01:00
parent d23e780248
commit 95e810db7a
3 changed files with 12 additions and 6 deletions

View File

@ -17,9 +17,10 @@ class AIMessageBase(object):
class AIMessage(AIMessageBase): class AIMessage(AIMessageBase):
def __init__(self, user: str, message: str) -> None: def __init__(self, user: str, message: str, channel: str = "chat") -> None:
self.user = user self.user = user
self.message = message self.message = message
self.channel = channel
class AIResponse(AIMessageBase): class AIResponse(AIMessageBase):

View File

@ -40,10 +40,12 @@ class FjerkroaBot(commands.Bot):
@classmethod @classmethod
def load_config(self, config_file: str = "config.toml"): def load_config(self, config_file: str = "config.toml"):
logging.info(f"config file {config_file} changed, reloading.")
with open(config_file, encoding='utf-8') as file: with open(config_file, encoding='utf-8') as file:
return toml.load(file) return toml.load(file)
def on_config_file_modified(self, event): def on_config_file_modified(self, event):
logging.info(f"file {event.src_path} modified")
if event.src_path == self.config_file: if event.src_path == self.config_file:
self.config = self.load_config(self.config_file) self.config = self.load_config(self.config_file)
self.airesponder.config = self.config self.airesponder.config = self.config
@ -60,19 +62,22 @@ class FjerkroaBot(commands.Bot):
async def on_member_join(self, member): async def on_member_join(self, member):
logging.info(f"User {member.name} joined") logging.info(f"User {member.name} joined")
msg = AIMessage(member.name, self.config['join-message'].replace('{name}', member.name))
if self.welcome_channel is not None: if self.welcome_channel is not None:
msg = AIMessage(member.name, self.config['join-message'].replace('{name}', member.name), str(self.welcome_channel.name))
await self.respond(msg, self.welcome_channel) await self.respond(msg, self.welcome_channel)
async def on_message(self, message: Message) -> None: async def on_message(self, message: Message) -> None:
if self.user is not None and message.author.id == self.user.id: if self.user is not None and message.author.id == self.user.id:
return return
msg = AIMessage(message.author.name, str(message.content).strip()) message_content = str(message.content).strip()
if len(message_content) < 1:
return
msg = AIMessage(message.author.name, message_content, str(message.channel.name))
await self.respond(msg, message.channel) await self.respond(msg, message.channel)
async def respond(self, message: AIMessage, channel: TextChannel) -> None: async def respond(self, message: AIMessage, channel: TextChannel) -> None:
try: try:
channel_name = channel.name channel_name = str(channel.name)
except Exception: except Exception:
channel_name = str(channel.id) channel_name = str(channel.id)
logging.info(f"handle message {str(message)} for channel {channel_name}") logging.info(f"handle message {str(message)} for channel {channel_name}")

View File

@ -35,7 +35,7 @@ class TestBotBase(unittest.IsolatedAsyncioTestCase):
patch.object(FjerkroaBot, 'user', new_callable=PropertyMock) as mock_user: patch.object(FjerkroaBot, 'user', new_callable=PropertyMock) as mock_user:
mock_user.return_value = MagicMock(spec=User) mock_user.return_value = MagicMock(spec=User)
mock_user.return_value.id = 12 mock_user.return_value.id = 12
self.bot = FjerkroaBot('config.json') self.bot = FjerkroaBot('config.toml')
self.bot.staff_channel = AsyncMock(spec=TextChannel) self.bot.staff_channel = AsyncMock(spec=TextChannel)
self.bot.staff_channel.send = AsyncMock() self.bot.staff_channel.send = AsyncMock()
self.bot.welcome_channel = AsyncMock(spec=TextChannel) self.bot.welcome_channel = AsyncMock(spec=TextChannel)
@ -58,7 +58,7 @@ class TestFunctionality(TestBotBase):
def test_load_config(self) -> None: def test_load_config(self) -> None:
with patch('builtins.open', mock_open(read_data=toml.dumps(self.config_data))): with patch('builtins.open', mock_open(read_data=toml.dumps(self.config_data))):
result = FjerkroaBot.load_config('config.json') result = FjerkroaBot.load_config('config.toml')
self.assertEqual(result, self.config_data) self.assertEqual(result, self.config_data)
async def test_on_message_event(self) -> None: async def test_on_message_event(self) -> None: