refactor: async I/O, input sanitisation, and shared utilities cleanup
This commit is contained in:
+19
-28
@@ -1,41 +1,32 @@
|
||||
"""
|
||||
This plugin provides a command to get a random fortune message.
|
||||
"""
|
||||
# plugins/fortune.py
|
||||
|
||||
import subprocess
|
||||
import asyncio
|
||||
import logging
|
||||
import simplematrixbotlib as botlib
|
||||
|
||||
async def handle_command(room, message, bot, prefix, config):
|
||||
"""
|
||||
Function to handle the !fortune command.
|
||||
|
||||
Args:
|
||||
room (Room): The Matrix room where the command was invoked.
|
||||
message (RoomMessage): The message object containing the command.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
match = botlib.MessageMatch(room, message, bot, prefix)
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("fortune"):
|
||||
logging.info("Received !fortune command")
|
||||
fortune_output = "🃏 " + subprocess.run(['/usr/games/fortune'], capture_output=True).stdout.decode('UTF-8')
|
||||
await bot.api.send_markdown_message(room.room_id, fortune_output)
|
||||
logging.info("Sent fortune to the room")
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
'/usr/games/fortune',
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE
|
||||
)
|
||||
stdout, stderr = await proc.communicate()
|
||||
if proc.returncode == 0 and stdout:
|
||||
fortune_text = "🃏 " + stdout.decode('UTF-8')
|
||||
else:
|
||||
fortune_text = "🃏 Fortune command failed."
|
||||
await bot.api.send_markdown_message(room.room_id, fortune_text)
|
||||
except Exception as e:
|
||||
logging.error(f"Fortune error: {e}")
|
||||
await bot.api.send_text_message(room.room_id, "Fortune unavailable.")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Plugin Metadata
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__version__ = "1.0.1"
|
||||
__author__ = "Funguy Bot"
|
||||
__description__ = "Random fortune message"
|
||||
__help__ = """
|
||||
<details>
|
||||
<summary><strong>!fortune</strong> – Random fortune</summary>
|
||||
<p>Runs the <code>/usr/games/fortune</code> utility and posts a random quote.</p>
|
||||
</details>
|
||||
"""
|
||||
__help__ = """<details><summary><strong>!fortune</strong> – Random fortune</summary>
|
||||
<p>Runs the <code>/usr/games/fortune</code> utility.</p></details>"""
|
||||
|
||||
Reference in New Issue
Block a user