-
Notifications
You must be signed in to change notification settings - Fork 33
/
dictionary.py
69 lines (57 loc) · 2.39 KB
/
dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)