First commit
This commit is contained in:
85
plugins/karma.py
Normal file
85
plugins/karma.py
Normal file
@@ -0,0 +1,85 @@
|
||||
# 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,))
|
||||
points = c.fetchone()[0]
|
||||
conn.close()
|
||||
await bot.api.send_text_message(room.room_id, f"{sender}'s karma points: {points}")
|
||||
logging.info(f"Sent {sender}'s karma points ({points}) to the room")
|
||||
elif len(args) == 1:
|
||||
username = args[0]
|
||||
|
||||
if username == sender:
|
||||
await bot.api.send_text_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,))
|
||||
points = c.fetchone()[0]
|
||||
conn.close()
|
||||
await bot.api.send_text_message(room.room_id, f"{username}'s karma points: {points}")
|
||||
logging.info(f"Sent {username}'s karma points ({points}) to the room")
|
||||
elif len(args) == 2:
|
||||
username, action = args
|
||||
if action not in ['up', 'down']:
|
||||
await bot.api.send_text_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_text_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('''UPDATE karma SET points = points + 1 WHERE username = ?''', (username,))
|
||||
else:
|
||||
c.execute('''UPDATE karma SET points = points - 1 WHERE username = ?''', (username,))
|
||||
|
||||
conn.commit()
|
||||
c.execute('''SELECT points FROM karma WHERE username = ?''', (username,))
|
||||
points = c.fetchone()[0]
|
||||
conn.close()
|
||||
|
||||
await bot.api.send_text_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_text_message(room.room_id, "Usage: !karma [username] [up/down]")
|
||||
logging.info("Sent usage message to the room")
|
Reference in New Issue
Block a user