PagerMaid_Plugins_Pyro/urbandictionary/main.py

36 lines
1.2 KiB
Python
Raw Normal View History

from pagermaid.listener import listener
from pagermaid.enums import AsyncClient, Message
from pagermaid.utils import alias_command
2023-07-01 12:18:58 +00:00
@listener(command="urbandictionary", parameters="[单词]", description="解释英语俚语词汇")
async def get_urban_mean(message: Message, httpx: AsyncClient):
2023-07-01 12:18:58 +00:00
"""To fetch meaning of the given word from urban dictionary."""
word = message.arguments
if not word:
2023-07-01 12:18:58 +00:00
return await message.edit(
f"[urbandictionary] 使用方法:`,{alias_command('urbandictionary')} <单词>`"
)
try:
2023-07-01 12:18:58 +00:00
response = (
await httpx.get(f"https://api.urbandictionary.com/v0/define?term={word}")
).json()
except Exception as e:
return await message.edit(f"[urbandictionary] API 接口无法访问:{e}")
if len(response["list"]) == 0:
return await message.edit("[urbandictionary] 无法查询到单词的意思")
word = response["list"][0]["word"]
definition = response["list"][0]["definition"]
example = response["list"][0]["example"]
2023-07-01 12:18:58 +00:00
result = (
f"**Word :** __{word}__\n\n"
f"**Meaning:**\n"
f"`{definition}`\n\n"
f"**Example:**\n"
f"`{example}`"
)
await message.edit(result)