Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin: create and use lists of items #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions limbo/plugins/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Add items to a list and get items from a list or get existing lists, use !list add|get|getlists [listname] [item]"""
import re


def add(server, listname, item, msg):
server.query("INSERT INTO lists VALUES (?, ?, ?, ?, ?, ?)",
listname, item, msg["user"], msg["ts"], msg["team"], msg["channel"])


def get(server, list):
result = server.query("SELECT msg FROM lists WHERE list == ? ORDER BY random() LIMIT 1", listname)
if len(result) == 1:
return result[0][0]
else:
return "List not found"

def get_lists(server):
result = server.query("SELECT DISTINCT list FROM lists ORDER BY list")
if len(result) > 1:
# todo: better formatting
res = ""
for r in result:
res+=r[0]+"\n"
return res[:-1]
else:
return "No lists found"

def on_message(msg, server):
"""
:type server: limbo.server.LimboServer
:return:
"""
text = msg.get("text", "")
match = re.findall(r"!list\s+(.*)", text)
if not match:
return

command = match[0].split(" ")

if command[0] == "add":
if len(command) > 1:
item = " ".join(command[2:])
return add(server, command[1], item, msg)
elif command[0] == "get":
if len(command) == 2:
return get(server, command[1])
elif command[0] == "getlists":
return get_lists(server)
return "Command wrongly formated, use 'add <list> msg' or 'get <list>' to get random element form list or 'get_lists' to get existing lists"

def on_reaction(msg, server):
print("here")

def on_init(server):
server.query("""
CREATE TABLE IF NOT EXISTS lists
(list STRING, msg STRING, sender STRING, time STRING, team STRING, channel STRING)
""")