diff --git a/README.md b/README.md index 4ed9a5c..6fb2ae7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +
+As CPPS.APP will be shut down on 25 January 2024, the bot will soon be disabled +
+

Puffle Bot

diff --git a/bot/core/server.py b/bot/core/server.py index 8cb7f40..e7f1040 100644 --- a/bot/core/server.py +++ b/bot/core/server.py @@ -9,6 +9,7 @@ from bot.events import event from bot.events.module import hot_reload_module import bot.handlers +from bot.misc.constants import owner_ids class Server: @@ -58,7 +59,7 @@ async def start(self): command_sync_flags.sync_commands = True self.bot = PuffleBot(defer=self.config.defer, intents=intents, command_sync_flags=command_sync_flags, - owner_id=527140180696629248) # test_guilds=[755445822920982548], + owner_ids=owner_ids) # test_guilds=[guild_ids] await hot_reload_module(bot.handlers) await event.emit("boot", self) @@ -81,5 +82,5 @@ async def connect_to_houdini(self): except ConnectionRefusedError: logger.error("The remote computer refused the network connection") return - logger.info(f"Server ('{self.config.houdini_address}', {self.config.houdini_port}) connected") + logger.info(f"Connected to server ('{self.config.houdini_address}', {self.config.houdini_port})") self.client_object = self.client_class(self, reader, writer) diff --git a/bot/handlers/button.py b/bot/handlers/button.py index f971dd5..b81514f 100644 --- a/bot/handlers/button.py +++ b/bot/handlers/button.py @@ -95,8 +95,8 @@ async def coins100_button(self, button: disnake.ui.Button, inter: MessageInterac async def coins500_button(self, button: disnake.ui.Button, inter: MessageInteraction): await self.donate(inter, int(button.custom_id)) - @disnake.ui.button(label="1000", style=disnake.ButtonStyle.blurple, emoji="<:coin:788877461588279336>", - custom_id="1 000") + @disnake.ui.button(label="1 000", style=disnake.ButtonStyle.blurple, emoji="<:coin:788877461588279336>", + custom_id="1000") async def coins1000_button(self, button: disnake.ui.Button, inter: MessageInteraction): await self.donate(inter, int(button.custom_id)) diff --git a/bot/handlers/cogs/commands.py b/bot/handlers/cogs/commands.py index 82ae8cb..4232a54 100644 --- a/bot/handlers/cogs/commands.py +++ b/bot/handlers/cogs/commands.py @@ -55,8 +55,7 @@ async def card(self, inter: AppCommandInter, if user: p = await user.penguin if p is None: - await inter.send(self.bot.i18n.get("USER_PENGUIN_NOT_FOUND")[lang], ephemeral=True) - return + raise CommandError("USER_PENGUIN_NOT_FOUND") else: p = await inter.user.penguin @@ -100,9 +99,11 @@ async def pay(self, inter: AppCommandInter, """ await inter.response.defer() p: Penguin = await inter.user.penguin - receiver_id, = await Penguin.select('id').where(Penguin.username == nickname.lower()).gino.first() - if receiver_id is None: - return await inter.send(self.bot.i18n.get("PENGUIN_NOT_FOUND")[str(inter.avail_lang)], ephemeral=True) + try: + receiver_id, = await Penguin.select('id').where(Penguin.username == nickname.lower()).gino.first() + except TypeError: + raise CommandError("PENGUIN_NOT_FOUND") + r: Penguin = await Penguin.get(int(receiver_id)) await transfer_coins(p, r, coins) @@ -246,7 +247,7 @@ async def gift(self, inter: AppCommandInter, channel: disnake.TextChannel, coins if message is None: message = self.bot.i18n.get("GIFT_DEFAULT_RESPONSE")[lang] elif not is_message_valid(message): - return await inter.send(self.bot.i18n.get("KEEP_RULES")[lang], ephemeral=True) + raise CommandError("KEEP_RULES") if coins <= 0: raise CommandError("INCORRECT_COINS_AMOUNT") diff --git a/bot/misc/constants.py b/bot/misc/constants.py index 8cba092..1873673 100644 --- a/bot/misc/constants.py +++ b/bot/misc/constants.py @@ -23,6 +23,7 @@ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"} # Arrays +owner_ids = [527140180696629248] guild_ids = [cppsapp_server_id, test_server_id] non_deferred_commands = ["settings"] commands_without_penguin_requirement = ["ilyash", "online", "login", "top", "settings"] diff --git a/bot/misc/utils.py b/bot/misc/utils.py index 287245f..6972efb 100644 --- a/bot/misc/utils.py +++ b/bot/misc/utils.py @@ -7,9 +7,9 @@ @event.on("boot") -async def setup(server): - global client - client = server.client_object +async def setup(_server): + global server + server = _server async def get_my_penguin_from_user_id(user_id: int) -> Penguin: @@ -76,6 +76,11 @@ async def get_penguin_from_penguin_id(penguin_id: int) -> Penguin: return p +async def check_connection_to_houdini(): + if server.client_object.__writer.is_closing(): + await server.connect_to_houdini() + + async def transfer_coins(sender: Penguin, receiver: Penguin, coins: int): """ Transfer coins from one penguin to another and return a status dictionary. @@ -118,6 +123,7 @@ async def transfer_coins(sender: Penguin, receiver: Penguin, coins: int): text=f"Получил от {sender.username} {int(coins)} монет. Через Discord бота", room_id=0, server_id=8000) - await client.send_xml({'body': {'action': 'pb-cdu', 'r': '0'}, 'penguin': {'p': str(sender.id)}}) - await client.send_xml({'body': {'action': 'pb-cdu', 'r': '0'}, 'penguin': {'p': str(receiver.id)}}) + await check_connection_to_houdini() + await server.client_object.send_xml({'body': {'action': 'pb-cdu', 'r': '0'}, 'penguin': {'p': str(sender.id)}}) + await server.client_object.send_xml({'body': {'action': 'pb-cdu', 'r': '0'}, 'penguin': {'p': str(receiver.id)}}) return