From c16a4c52c3297216418001243cc059fa4dcd1b9a Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 10 Feb 2022 22:40:11 +0800 Subject: [PATCH] =?UTF-8?q?dictionary=20=E6=9F=A5=E8=AF=A2=E8=8B=B1?= =?UTF-8?q?=E8=AF=AD=E5=8D=95=E8=AF=8D/=E4=BF=9A=E8=AF=AD=E7=9A=84?= =?UTF-8?q?=E6=84=8F=E6=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dictionary.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ list.json | 10 ++++++++ 2 files changed, 79 insertions(+) create mode 100644 dictionary.py diff --git a/dictionary.py b/dictionary.py new file mode 100644 index 0000000..43a004f --- /dev/null +++ b/dictionary.py @@ -0,0 +1,69 @@ +""" PagerMaid Plugin to provide a dictionary lookup. """ + +from pagermaid import version +from pagermaid.listener import listener +from pagermaid.utils import alias_command, pip_install, obtain_message, client +from telethon.events.newmessage import NewMessage + +pip_install("PyDictionary") + +from PyDictionary import PyDictionary + +dictionary = PyDictionary() + + +@listener(is_plugin=True, outgoing=True, command=alias_command("dictionary"), + description="查询英语单词的意思") +async def get_word_mean(context: NewMessage.Event) -> None: + """ Look up a word in the dictionary. """ + try: + word = await obtain_message(context) + except ValueError: + await context.edit(f"[dictionary] 使用方法:`-{alias_command('dictionary')} <单词>`") + return + + result = dictionary.meaning(word) + output = f"**Word :** __{word}__\n\n" + if result: + try: + for a, b in result.items(): + output += f"**{a}**\n" + for i in b: + output += f"☞__{i}__\n" + await context.edit(output) + except Exception: + await context.edit("[dictionary] 无法查询到单词的意思") + else: + await context.edit("[dictionary] 无法查询到单词的意思") + + +@listener(is_plugin=True, outgoing=True, command=alias_command("urbandictionary"), + description="解释英语俚语词汇") +async def get_urban_mean(context: NewMessage.Event) -> None: + """ To fetch meaning of the given word from urban dictionary. """ + try: + word = await obtain_message(context) + except ValueError: + await context.edit(f"[urbandictionary] 使用方法:`-{alias_command('urbandictionary')} <单词>`") + return + + response = await client.get(f"https://api.urbandictionary.com/v0/define?term={word}") + try: + response = response.json() + except: + await context.edit("[urbandictionary] API 接口无法访问") + return + + if len(response["list"]) == 0: + await context.edit("[urbandictionary] 无法查询到单词的意思") + return + + word = response["list"][0]["word"] + definition = response["list"][0]["definition"] + example = response["list"][0]["example"] + result = f"**Word :** __{word}__\n\n" \ + f"**Meaning:**\n" \ + f"`{definition}`\n\n" \ + f"**Example:**\n" \ + f"`{example}`" + await context.edit(result) diff --git a/list.json b/list.json index 9bc04b5..63834c0 100644 --- a/list.json +++ b/list.json @@ -879,6 +879,16 @@ "supported": true, "des-short": "凌晨自动备份。", "des": "凌晨自动备份数据文件。" + }, + { + "name": "dictionary", + "version": "1.00", + "section": "chat", + "maintainer": "xtaodada", + "size": "2.4 kb", + "supported": true, + "des-short": "查询英语单词/俚语的意思。", + "des": "查询英语单词/俚语的意思。\n指令:-dictionary -urbandictionary" } ] }