2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-22 18:19:16 +00:00
microbot/ubot/modules/urbandict.py

52 lines
1.4 KiB
Python
Raw Normal View History

2020-03-16 21:02:05 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
2023-10-16 09:46:31 -04:00
import io
from ubot.micro_bot import micro_bot
ldr = micro_bot.loader
UD_QUERY_URL = 'http://api.urbandictionary.com/v0/define'
UD_RANDOM_URL = 'http://api.urbandictionary.com/v0/random'
@ldr.add("ud")
2023-10-16 09:46:31 -04:00
async def urban_dict(event):
2020-05-02 16:40:26 -04:00
udquery = event.args
2023-10-16 09:46:31 -04:00
if udquery:
params = {'term': udquery}
url = UD_QUERY_URL
else:
params = None
url = UD_RANDOM_URL
2020-05-09 13:15:08 -04:00
async with ldr.aioclient.get(url, params=params) as response:
2023-10-16 09:46:31 -04:00
if response.status == 200:
response = await response.json()
else:
await event.reply(f"`An error occurred, response code:` **{response.status}**")
return
if response['list']:
response_word = response['list'][0]
else:
await event.reply(f"`No results for query:` **{udquery}**")
return
definition = f"**{response_word['word']}**: `{response_word['definition']}`"
if response_word['example']:
definition += f"\n\n**Example**: `{response_word['example']}`"
definition = definition.replace("[", "").replace("]", "")
if len(definition) >= 4096:
file_io = io.BytesIO()
file_io.write(bytes(definition.encode('utf-8')))
file_io.name = f"definition of {response_word['word']}.txt"
await event.reply(file=file_io, caption="`Output was too large, sent it as a file.`")
return
await event.reply(definition)