瀏覽代碼

Base curse command added

Kirk Trombley 6 年之前
父節點
當前提交
c54f69ba53
共有 2 個文件被更改,包括 49 次插入1 次删除
  1. 2 1
      config/config.toml
  2. 47 0
      src/plugins/curse.py

+ 2 - 1
config/config.toml

@@ -7,6 +7,7 @@ session = [ "session" ]
 roll = [ "roll" ]
 querying = [ "inspire", "shield", "scp", "riddle" ]
 seychelles = [ "seychelles" ]
+curse = [ "curse", "bless" ]
 
 [aliases]
 speemteek = "speamteek"
@@ -16,4 +17,4 @@ horoscope = "inspire"
 
 [teamspeak]
 host = "kirkleon.ddns.net"
-user = "serveradmin"
+user = "serveradmin"

+ 47 - 0
src/plugins/curse.py

@@ -0,0 +1,47 @@
+
+from sqlalchemy import Column, Integer, String
+
+from command_system import RollbotResponse, RollbotFailure, as_plugin, ModelBase, pop_arg
+
+
+class CurseBlessScore(ModelBase):
+    __tablename__ = "curse_bless_score"
+    group_id = Column(String, primary_key=True)
+    person_id = Column(Integer, primary_key=True)
+    curses = Column(Integer)
+    blessings = Column(Integer)
+
+
+def get_score(db, group_id, name):
+    person_id = name.strip().lower()
+    score = db.query(CurseBlessScore).get((group_id, person_id))
+    if not score:
+        score = CurseBlessScore(group_id=group_id, person_id=person_id, curses=0, blessings=0)
+        db.add(score)
+    return score
+
+
+def fmt_times(n):
+    return str(n) + (" time" if n == 1 else " times")
+
+
+def get_response(msg, name, score):
+    return RollbotResponse(msg, txt=f"It is done! {name} has been cursed {fmt_times(score.curses)} and blessed {fmt_times(score.blessings)} in this chat.")
+
+
+@as_plugin
+def curse(db, msg):
+    # TODO might be nice to add subcommands to this later
+    name, _ = pop_arg(msg.raw_args)
+    score = get_score(db, msg.group_id, name)
+    # Note we do this instead of += b/c that can create race conditions in sqlalchemy
+    score.curses = score.curses + 1
+    return get_response(msg, name, score)
+
+
+@as_plugin
+def bless(db, msg):
+    name, _ = pop_arg(msg.raw_args)
+    score = get_score(db, msg.group_id, name)
+    score.blessings = score.blessings + 1
+    return get_response(msg, name, score)