102 lines
4.9 KiB
Python
102 lines
4.9 KiB
Python
# plugins/karma.py
|
|
|
|
import sqlite3
|
|
import logging
|
|
import simplematrixbotlib as botlib
|
|
|
|
async def handle_command(room, message, bot, PREFIX):
|
|
"""
|
|
Function to handle the !karma 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("karma"):
|
|
logging.info("Received !karma command")
|
|
args = match.args()
|
|
sender = str(message.sender)
|
|
|
|
if len(args) == 0:
|
|
# Query sender's own karma
|
|
conn = sqlite3.connect('karma.db')
|
|
c = conn.cursor()
|
|
c.execute('''CREATE TABLE IF NOT EXISTS karma
|
|
(username TEXT PRIMARY KEY, points INTEGER)''')
|
|
c.execute('''INSERT OR IGNORE INTO karma (username, points) VALUES (?, ?)''', (sender, 0))
|
|
c.execute('''SELECT points FROM karma WHERE username = ?''', (sender,))
|
|
row = c.fetchone()
|
|
if row is not None:
|
|
points = row[0]
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {sender}'s karma points: {points}")
|
|
logging.info(f"Sent {sender}'s karma points ({points}) to the room")
|
|
else:
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {sender} does not have any karma points yet.")
|
|
logging.info(f"Sent message that {sender} does not have any karma points yet.")
|
|
conn.close()
|
|
elif len(args) == 1:
|
|
username = args[0]
|
|
|
|
if username == sender:
|
|
await bot.api.send_markdown_message(room.room_id, "❌ You cannot modify your own karma.")
|
|
logging.info("Sent self-modification warning message to the room")
|
|
return
|
|
|
|
conn = sqlite3.connect('karma.db')
|
|
c = conn.cursor()
|
|
c.execute('''CREATE TABLE IF NOT EXISTS karma
|
|
(username TEXT PRIMARY KEY, points INTEGER)''')
|
|
c.execute('''INSERT OR IGNORE INTO karma (username, points) VALUES (?, ?)''', (username, 0))
|
|
c.execute('''SELECT points FROM karma WHERE username = ?''', (username,))
|
|
row = c.fetchone()
|
|
if row is not None:
|
|
points = row[0]
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {username}'s karma points: {points}")
|
|
logging.info(f"Sent {username}'s karma points ({points}) to the room")
|
|
else:
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {username} does not have any karma points yet.")
|
|
logging.info(f"Sent message that {username} does not have any karma points yet.")
|
|
conn.close()
|
|
elif len(args) == 2:
|
|
username, action = args
|
|
if action not in ['up', 'down']:
|
|
await bot.api.send_markdown_message(room.room_id, "❌ Invalid action. Use 'up' or 'down'.")
|
|
logging.info("Sent invalid action message to the room")
|
|
return
|
|
|
|
if username == sender:
|
|
await bot.api.send_markdown_message(room.room_id, "❌ You cannot modify your own karma.")
|
|
logging.info("Sent self-modification warning message to the room")
|
|
return
|
|
|
|
conn = sqlite3.connect('karma.db')
|
|
c = conn.cursor()
|
|
c.execute('''CREATE TABLE IF NOT EXISTS karma
|
|
(username TEXT PRIMARY KEY, points INTEGER)''')
|
|
|
|
if action == 'up':
|
|
c.execute('''INSERT OR IGNORE INTO karma (username, points) VALUES (?, ?)''', (username, 0))
|
|
c.execute('''UPDATE karma SET points = points + 1 WHERE username = ?''', (username,))
|
|
else:
|
|
c.execute('''INSERT OR IGNORE INTO karma (username, points) VALUES (?, ?)''', (username, 0))
|
|
c.execute('''UPDATE karma SET points = points - 1 WHERE username = ?''', (username,))
|
|
|
|
conn.commit()
|
|
c.execute('''SELECT points FROM karma WHERE username = ?''', (username,))
|
|
row = c.fetchone()
|
|
if row is not None:
|
|
points = row[0]
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {username}'s karma points: {points}")
|
|
logging.info(f"Sent {username}'s karma points ({points}) to the room")
|
|
else:
|
|
await bot.api.send_markdown_message(room.room_id, f"💗 {username} does not have any karma points yet.")
|
|
logging.info(f"Sent message that {username} does not have any karma points yet.")
|
|
conn.close()
|
|
else:
|
|
await bot.api.send_markdown_message(room.room_id, "☯ Usage: !karma [username] [up/down]")
|
|
logging.info("Sent usage message to the room")
|