ソースを参照

Adding feature to command system

Kirk Trombley 6 年 前
コミット
cd8979ab0f
3 ファイル変更37 行追加3 行削除
  1. 1 0
      .gitignore
  2. 3 3
      mkplugin.sh
  3. 33 0
      src/command_system.py

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@ secrets.toml
 __pycache__/*
 *.pyc
 .venv
+.vscode/

+ 3 - 3
mkplugin.sh

@@ -24,13 +24,13 @@ fi
 
 echo "Populating $PLUGIN_FILE with basic plugin called $1"
 
-echo "from command_system import as_plugin, RollbotResponse" >> $PLUGIN_FILE
+echo "from command_system import as_plugin" >> $PLUGIN_FILE
 echo "" >> $PLUGIN_FILE
 echo "" >> $PLUGIN_FILE
 echo "@as_plugin # if you want to change the name of the function below, you can just pass \"$1\" to this decorator" >> $PLUGIN_FILE
 echo "def $1(msg):" >> $PLUGIN_FILE
-echo "    # try adding db, bot, and log args, if you need those for your plugin" >> $PLUGIN_FILE
-echo "    return RollbotResponse(msg, txt=\"My first plugin!\")" >> $PLUGIN_FILE
+echo "    # try adding db, bot, log, and data args, if you need those for your plugin" >> $PLUGIN_FILE
+echo "    return \"My first plugin!\"" >> $PLUGIN_FILE
 
 echo "import plugins.$1" >> ./src/plugins/__init__.py
 

+ 33 - 0
src/command_system.py

@@ -4,6 +4,7 @@ from enum import Enum, auto
 import inspect
 import functools
 
+from sqlalchemy import Column, DateTime, Binary, String, Float, Integer
 from sqlalchemy.ext.declarative import declarative_base
 
 
@@ -12,6 +13,35 @@ BANGS = ('!',)
 ModelBase = declarative_base()
 
 
+class GroupBasedSingleton(ModelBase):
+    __tablename__ = "group_based_singleton"
+    group_id = Column(String, primary_key=True)
+    command_name = Column(String, primary_key=True)
+    subpart_name = Column(String, primary_key=True)
+    integer_data = Column(Integer)
+    float_data = Column(Float)
+    string_data = Column(String)
+    binary_data = Column(Binary)
+    datetime_data = Column(DateTime)
+
+    @staticmethod
+    def get_or_create(db, group_id, command_name, subpart_name):
+        sing = db.query(GroupBasedSingleton).get((group_id, command_name, subpart_name))
+        if sing is None:
+            sing = GroupBasedSingleton(
+                group_id=group_id,
+                command_name=command_name,
+                subpart_name=subpart_name,
+                integer_data=None,
+                float_data=None,
+                string_data=None,
+                binary_data=None,
+                datetime_data=None
+            )
+            db.add(sing)
+        return sing
+
+
 def pop_arg(text):
     if text is None:
         return None, None
@@ -171,6 +201,9 @@ def as_plugin(command):
                 converters.append(lambda self, db, msg: self.logger)
             elif p in ("bot", "rollbot"):
                 converters.append(lambda self, db, msg: self.bot)
+            elif p.startswith("data") or p.endswith("data") or p in ("group_singleton", "singleton"):
+                subp = fn.__annotations__.get(p, "")
+                converters.append(lambda self, db, msg, subp=subp: GroupBasedSingleton.get_or_create(db, msg.group_id, self.command, subp))
             else:
                 raise ValueError(f"Illegal argument name {p} in decorated plugin {command_name}")