From a6fa9e55ca49fa316c7cde1f5079d0c0663bf8d0 Mon Sep 17 00:00:00 2001 From: L_DelOff Date: Sun, 27 Oct 2024 14:04:25 +0300 Subject: [PATCH] pandas -> json --- channels.csv | 39 --------------------------- channels.json | 47 +++++++++++++++++++++++++++++++++ harontest.pem => haron.pem | 0 settings.ini | 6 ++--- src/config/BotConfig.py | 39 ++++++++++++++------------- src/model/MumbleChannel.py | 4 +-- src/service/ChannelService.py | 2 ++ src/service/MumbleBotService.py | 13 ++++----- welcome.csv | 1 - 9 files changed, 81 insertions(+), 70 deletions(-) delete mode 100644 channels.csv create mode 100644 channels.json rename harontest.pem => haron.pem (100%) delete mode 100644 welcome.csv diff --git a/channels.csv b/channels.csv deleted file mode 100644 index fd90fa2..0000000 --- a/channels.csv +++ /dev/null @@ -1,39 +0,0 @@ -path;message -Комнаты постояльцев;"

" -Комнаты постояльцев/CSGO;"

" -Комнаты постояльцев/Minecraft;"

" -Барная стойка;"

" -Гостинная;"

" -Стойка консьержа;"

 

- - - - - - -
-

 Заказать встречу с aдминистратором  

-
-

" -Концертный зал;"

 

- - - - - - -
-

 Заказать песню

-
-

" -Гостинная/Стол Эволюция;"

 

- - - - - - -
-

 Создать игру или присоединиться

-
-

" diff --git a/channels.json b/channels.json new file mode 100644 index 0000000..2ddcb3c --- /dev/null +++ b/channels.json @@ -0,0 +1,47 @@ +[ + { + "name": "root", + "path": "root", + "message": "

Добро пожаловать в отель!

" + }, + { + "name": "Комнаты постояльцев", + "path": "Комнаты постояльцев", + "message": "

" + }, + { + "name": "CSGO", + "path": "Комнаты постояльцев/CSGO", + "message": "

" + }, + { + "name": "Minecraft", + "path": "Комнаты постояльцев/Minecraft", + "message": "

" + }, + { + "name": "Барная стойка", + "path": "Барная стойка", + "message": "

" + }, + { + "name": "Гостинная", + "path": "Гостинная", + "message": "

" + }, + { + "name": "Стойка консьержа", + "path": "Стойка консьержа", + "message": "

 

\n\n\n\n\n\n\n
\n

 Заказать встречу с aдминистратором  

\n
\n

" + }, + { + "name": "Концертный зал", + "path": "Концертный зал", + "message": "

 

\n\n\n\n\n\n\n
\n

 Заказать песню

\n
\n

" + }, + { + "name": "Стол Эволюция", + "path": "Гостинная/Стол Эволюция", + "message": "

 

\n\n\n\n\n\n\n
\n

 Создать игру или присоединиться

\n
\n

" + } +] diff --git a/harontest.pem b/haron.pem similarity index 100% rename from harontest.pem rename to haron.pem diff --git a/settings.ini b/settings.ini index 356413f..f10b99f 100644 --- a/settings.ini +++ b/settings.ini @@ -1,11 +1,11 @@ [Server] -address = ldeloff.ru +address = mumble.ldeloff.ru port = 64738 password = tokens = [Bot] -bot_name = Харон_test -certfile = harontest.pem +bot_name = Харон +certfile = haron.pem data_folder = bot_data userlist = userlist.ini admins = L_DelOff;L_DelOff_m; diff --git a/src/config/BotConfig.py b/src/config/BotConfig.py index 982727d..8d155fc 100644 --- a/src/config/BotConfig.py +++ b/src/config/BotConfig.py @@ -1,8 +1,8 @@ import configparser +import json import logging import os from typing import List -import pandas from src.model.MumbleChannel import MumbleChannel @@ -35,23 +35,24 @@ class BotConfig: def load_channel_list(self): _logger.info("Загрузка файла с настройками каналов ...") - data = pandas.read_csv('channels.csv', delimiter=";") - data = data.reset_index() - for index, row in data.iterrows(): - self.channelList.append(MumbleChannel(row['path'], row['message'])) - _logger.info("Файл с настройками каналов загружен") + try: + with open("channels.json", encoding="utf-8") as f: + data = json.load(f) + for item in data: + if all(key in item for key in ('name', 'path', 'message')): + self.channelList.append(MumbleChannel(item['name'], item['path'], item['message'])) + else: + _logger.warning(f"Пропущен элемент с недостаточными данными: {item}") + _logger.info("Файл с настройками каналов загружен") + except FileNotFoundError: + _logger.error("Файл channels.json не найден") + except json.JSONDecodeError: + _logger.error("Ошибка в формате JSON файла channels.json") def load_welcome_message(self) -> str: - _logger.info("Загрузка файла с приветствием ...") - f = open('welcome.csv', 'r', encoding='utf-8') - message = f.read() - f.close() - return message - - -# bot = BotConfig() -# config = bot.config -# adminList = bot.adminList -# userList = bot.userList -#for ch in bot.channelList: -# print(ch.__str__()) + for channel in self.channelList: + if channel.name == "root": + _logger.info("Приветственное сообщение загружено") + return channel.message + _logger.warning("Приветственное сообщение не найдено загружено") + return "" diff --git a/src/model/MumbleChannel.py b/src/model/MumbleChannel.py index 9afecc4..a861e98 100644 --- a/src/model/MumbleChannel.py +++ b/src/model/MumbleChannel.py @@ -3,10 +3,10 @@ class MumbleChannel: path: str message: str - def __init__(self, path: str, message: str): + def __init__(self, name: str, path: str, message: str): self.path = path self.message = message - self.name = path.split('/')[-1] + self.name = name def __str__(self) -> str: string = f"MumbleChannel:\n name: {self.name} \n path: {self.path} \n message: {self.message}" diff --git a/src/service/ChannelService.py b/src/service/ChannelService.py index 0f87415..b144a08 100644 --- a/src/service/ChannelService.py +++ b/src/service/ChannelService.py @@ -16,6 +16,8 @@ class ChannelService: self.bot_data.config.load_channel_list() channels = self.bot_data.config.channelList for channel in channels: + if channel.name == 'root': + continue parent_channel_id = 0 path_split = channel.path.split('/') for path in path_split: diff --git a/src/service/MumbleBotService.py b/src/service/MumbleBotService.py index 7938294..8879d7c 100644 --- a/src/service/MumbleBotService.py +++ b/src/service/MumbleBotService.py @@ -1,6 +1,4 @@ import logging -from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError -from concurrent.futures import ThreadPoolExecutor from typing import Optional from pymumble_py3.users import User, Users @@ -58,10 +56,13 @@ class MumbleBotService: # TODO: одинаковые каналы (коллизия сообщений) def user_change_channel(self, user: User, action): - _logger.info(f"{user['name']} перешёл в канал: {self.bot_data.bot.channels[action['channel_id']]['name']}") - message = self.channel_service.get_message_by_channel_id(action['channel_id']) - if len(message) > 0: - self.get_user_by_session(user['session']).send_text_message(message) + try: + _logger.info(f"{user['name']} перешёл в канал: {self.bot_data.bot.channels[action['channel_id']]['name']}") + message = self.channel_service.get_message_by_channel_id(action['channel_id']) + if len(message) > 0: + self.get_user_by_session(user['session']).send_text_message(message) + except KeyError: + return def user_connect_server(self, action): user = self.get_user_by_session(action['session']) diff --git a/welcome.csv b/welcome.csv deleted file mode 100644 index cc3d127..0000000 --- a/welcome.csv +++ /dev/null @@ -1 +0,0 @@ -

Добро пожаловать в отель!

\ No newline at end of file