Infermatic output cleanup. Added dictionary and joke plugins. Updated Readme.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
Plugin for looking up word definitions using the system's dict command.
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import logging
|
||||
import simplematrixbotlib as botlib
|
||||
|
||||
async def handle_command(room, message, bot, prefix, config):
|
||||
"""
|
||||
Function to handle the !define command.
|
||||
|
||||
Args:
|
||||
room (Room): The Matrix room where the command was invoked.
|
||||
message (RoomMessage): The message object containing the command.
|
||||
bot (Bot): The bot object.
|
||||
prefix (str): The command prefix.
|
||||
config (dict): Configuration parameters.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
match = botlib.MessageMatch(room, message, bot, prefix)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("define"):
|
||||
args = match.args()
|
||||
if not args:
|
||||
await bot.api.send_text_message(room.room_id, "Usage: !define <word>")
|
||||
return
|
||||
|
||||
word = ' '.join(args)
|
||||
logging.info(f"Looking up definition for: {word}")
|
||||
|
||||
try:
|
||||
# Use the WordNet dictionary by default for concise definitions
|
||||
result = subprocess.run(
|
||||
['/usr/bin/dict', '-d', 'wn', word],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
# Clean up the output for better presentation
|
||||
definition = result.stdout.strip()
|
||||
# Remove extra newlines and clean up formatting
|
||||
lines = [line for line in definition.split('\n') if line.strip()]
|
||||
formatted_definition = '\n'.join(lines)
|
||||
|
||||
# Send as markdown for better formatting
|
||||
await bot.api.send_markdown_message(room.room_id, f"**Definition of '{word}':**\n```\n{formatted_definition}\n```")
|
||||
elif result.stderr and "No definitions found" in result.stderr:
|
||||
# Try with all dictionaries if WordNet doesn't have it
|
||||
result_all = subprocess.run(
|
||||
['/usr/bin/dict', '-d', 'all', word],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if result_all.returncode == 0 and result_all.stdout.strip():
|
||||
definition = result_all.stdout.strip()
|
||||
lines = [line for line in definition.split('\n') if line.strip()]
|
||||
# Limit output to prevent overly long messages
|
||||
if len(lines) > 20:
|
||||
formatted_definition = '\n'.join(lines[:20]) + f"\n... (truncated, {len(lines)} total lines)"
|
||||
else:
|
||||
formatted_definition = '\n'.join(lines)
|
||||
|
||||
await bot.api.send_markdown_message(room.room_id, f"**Definition of '{word}':**\n```\n{formatted_definition}\n```")
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, f"No definition found for '{word}'")
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, f"No definition found for '{word}'")
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
await bot.api.send_text_message(room.room_id, "Sorry, the dictionary lookup timed out. Please try again.")
|
||||
except Exception as e:
|
||||
logging.error(f"Error looking up definition: {e}")
|
||||
await bot.api.send_text_message(room.room_id, f"Error looking up definition for '{word}': {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Plugin Metadata
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "Funguy Bot"
|
||||
__description__ = "Look up word definitions using system dictionary"
|
||||
__help__ = """
|
||||
<details>
|
||||
<summary><strong>!define</strong> – Word definitions</summary>
|
||||
<p>Look up definitions of words using multiple dictionaries including WordNet, Webster's, and others.<br>
|
||||
Usage: <code>!define <word></code></p>
|
||||
</details>
|
||||
"""
|
||||
Reference in New Issue
Block a user