From 693b45ea60dbbd7772f5427dd943edb152326129 Mon Sep 17 00:00:00 2001 From: Ricky8955555 Date: Wed, 29 Mar 2023 16:55:29 +0800 Subject: [PATCH] =?UTF-8?q?idna=20morse=20=E6=94=AF=E6=8C=81=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- idna/DES.md | 4 ++-- idna/main.py | 38 ++++++++++++++++++-------------------- morse/DES.md | 4 ++-- morse/main.py | 34 ++++++++++++++++------------------ 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/idna/DES.md b/idna/DES.md index 9c49a52..98b73de 100644 --- a/idna/DES.md +++ b/idna/DES.md @@ -1,5 +1,5 @@ 国际化域名 (IDNA) 编码转换工具 指令: -- `,punyencode <待编码内容>` (编码至 Punycode) -- `,punydecode <待解码内容>` (从 Punycode 解码) +- `,punyencode <待编码内容> (支持回复消息)` (编码至 Punycode) +- `,punydecode <待解码内容> (支持回复消息)` (从 Punycode 解码) diff --git a/idna/main.py b/idna/main.py index 6746af7..24183d8 100644 --- a/idna/main.py +++ b/idna/main.py @@ -5,25 +5,23 @@ from pagermaid.enums import Message from pagermaid.listener import listener -@listener(command="punyencode", description="编码至 Punycode", parameters="[待编码内容]") -async def punyencode(message: Message) -> None: - if message.arguments: - try: - result = message.arguments.encode("idna").decode() - except Exception: - result = "呜呜呜 ~ 转换失败了,可能含有非法字符。" - else: - result = "请输入参数" - await message.edit(f"`{result}`") +@listener(command="punyencode", description="编码至 Punycode", parameters="[待编码内容] (支持回复消息)") +async def punyencode(message: Message): + if not (text := message.obtain_message()): + return await message.edit("请输入参数") + try: + encoded = text.encode("idna").decode() + except Exception: + return await message.edit("呜呜呜 ~ 转换失败了,可能含有非法字符。") + await message.edit(f"`{encoded}`") -@listener(command="punydecode", description="从 Punycode 解码", parameters="[待解码内容]") -async def punydecode(message: Message) -> None: - if message.arguments: - try: - result = message.arguments.encode().decode("idna") - except Exception: - result = "呜呜呜 ~ 转换失败了,可能含有非法字符。" - else: - result = "请输入参数" - await message.edit(f"`{result}`") +@listener(command="punydecode", description="从 Punycode 解码", parameters="[待解码内容] (支持回复消息)") +async def punydecode(message: Message): + if not (text := message.obtain_message()): + return await message.edit("请输入参数") + try: + decoded = text.encode().decode("idna") + except Exception: + return await message.edit("呜呜呜 ~ 转换失败了,可能含有非法字符。") + await message.edit(f"`{decoded}`") diff --git a/morse/DES.md b/morse/DES.md index a20187f..77fe67c 100644 --- a/morse/DES.md +++ b/morse/DES.md @@ -1,5 +1,5 @@ 摩斯密码转换 (支持非英文) 指令: -- `,enmorse <待转换文本>` (转换指定文本到摩斯密码) -- `,demorse <摩斯密码>` (转换摩斯密码到明文) +- `,enmorse <待转换文本> (支持回复消息)` (转换指定文本到摩斯密码) +- `,demorse <摩斯密码> (支持回复消息)` (转换摩斯密码到明文) diff --git a/morse/main.py b/morse/main.py index 0352529..b66cbcd 100644 --- a/morse/main.py +++ b/morse/main.py @@ -91,25 +91,23 @@ def decode(morse: str) -> str: ) -@listener(command="enmorse", description="转换指定文本到摩斯密码", parameters="[待转换文本]") +@listener(command="enmorse", description="转换指定文本到摩斯密码", parameters="[待转换文本] (支持回复消息)") async def enmorse(message: Message): - if message.arguments: - try: - result = encode(message.arguments) - except Exception: - result = "呜呜呜 ~ 转换失败了,可能含有非法字符。" - else: - result = "请输入参数" - await message.edit(f"`{result}`") + if not (text := message.obtain_message()): + return await message.edit("请输入参数") + try: + morse = encode(text) + except Exception: + return await message.edit("呜呜呜 ~ 转换失败了,可能含有非法字符。") + await message.edit(f"`{morse}`") -@listener(command="demorse", description="转换摩斯密码到明文", parameters="[摩斯密码]") +@listener(command="demorse", description="转换摩斯密码到明文", parameters="[摩斯密码] (支持回复消息)") async def demorse(message: Message): - if message.arguments: - try: - result = decode(message.arguments) - except Exception: - result = "呜呜呜 ~ 转换失败了,可能含有非法字符。" - else: - result = "请输入参数" - await message.edit(f"`{result}`") + if not (morse := message.obtain_message()): + return await message.edit("请输入参数") + try: + text = decode(morse) + except Exception: + return await message.edit("呜呜呜 ~ 转换失败了,可能含有非法字符。") + await message.edit(f"`{text}`")