Skip to content

Commit eb20f0b

Browse files
committed
✨ feat(plugin): 键值对存储
1 parent 40d7527 commit eb20f0b

File tree

9 files changed

+288
-20
lines changed

9 files changed

+288
-20
lines changed

blockly_file/library.xml

+141
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,145 @@
473473
<field name="HUE">180</field>
474474
</block>
475475
</value>
476+
</block><block type="factory_base" id="w[lxcc0D3v~gGP|d@`9%" deletable="false" movable="false" x="10" y="10">
477+
<mutation connections="BOTH"></mutation>
478+
<field name="NAME">DataSet</field>
479+
<field name="INLINE">AUTO</field>
480+
<field name="CONNECTIONS">BOTH</field>
481+
<statement name="INPUTS">
482+
<block type="input_dummy" id="AB.4J4`[Jlw?B(`EocQA">
483+
<field name="ALIGN">LEFT</field>
484+
<statement name="FIELDS">
485+
<block type="field_static" id="c-1F2{XH_gU8!51{T@[O">
486+
<field name="TEXT">设置值</field>
487+
</block>
488+
</statement>
489+
<next>
490+
<block type="input_value" id="TG;!j(r)k,FbM|y^]S22">
491+
<field name="INPUTNAME">pluginName</field>
492+
<field name="ALIGN">LEFT</field>
493+
<statement name="FIELDS">
494+
<block type="field_static" id="bvy8|lwQBhwos$Aum8tu">
495+
<field name="TEXT">插件名</field>
496+
</block>
497+
</statement>
498+
<value name="TYPE">
499+
<shadow type="type_null" id="aMG%l`}k_ybh6tsTX6}/"></shadow>
500+
</value>
501+
<next>
502+
<block type="input_value" id="XM:wWf`{^+=y#M@_(n.7">
503+
<field name="INPUTNAME">name</field>
504+
<field name="ALIGN">LEFT</field>
505+
<statement name="FIELDS">
506+
<block type="field_static" id="~g6!goV7_,IsI0Z}IEi5">
507+
<field name="TEXT">名</field>
508+
</block>
509+
</statement>
510+
<value name="TYPE">
511+
<shadow type="type_null" id="@(!.S4IW_T:JvE`j1;ba"></shadow>
512+
</value>
513+
<next>
514+
<block type="input_value" id="FhzP9oSyxI1X47{3#=l/">
515+
<field name="INPUTNAME">value</field>
516+
<field name="ALIGN">LEFT</field>
517+
<statement name="FIELDS">
518+
<block type="field_static" id="=fm8:{+h%Yn:+B`V3mjn">
519+
<field name="TEXT">值</field>
520+
</block>
521+
</statement>
522+
<value name="TYPE">
523+
<shadow type="type_null" id="Kw$v0^wzki5I3MjxhQtd"></shadow>
524+
</value>
525+
</block>
526+
</next>
527+
</block>
528+
</next>
529+
</block>
530+
</next>
531+
</block>
532+
</statement>
533+
<value name="TOOLTIP">
534+
<block type="text" id="AAN#C:HulroBL^sPUU,O" deletable="false" movable="false">
535+
<field name="TEXT">持久化存储,设置值,数据存储在本地数据库内</field>
536+
</block>
537+
</value>
538+
<value name="HELPURL">
539+
<block type="text" id=";6yAs{sHl22T!Y/-(Liq" deletable="false" movable="false">
540+
<field name="TEXT"></field>
541+
</block>
542+
</value>
543+
<value name="TOPTYPE">
544+
<shadow type="type_null" id="0[$+0um@@#)?akzgA$94"></shadow>
545+
</value>
546+
<value name="BOTTOMTYPE">
547+
<shadow type="type_null" id="q|bmp[9c=a6cJ^6lo47a"></shadow>
548+
</value>
549+
<value name="COLOUR">
550+
<block type="colour_hue" id=":pD1WL$Sr73z3[v7H*vK">
551+
<mutation colour="#80a55b"></mutation>
552+
<field name="HUE">90</field>
553+
</block>
554+
</value>
555+
</block><block type="factory_base" id="rHlr(SM_Jr9jIvN+xlkN" deletable="false" movable="false" x="10" y="10">
556+
<mutation connections="LEFT"></mutation>
557+
<field name="NAME">DataGet</field>
558+
<field name="INLINE">AUTO</field>
559+
<field name="CONNECTIONS">LEFT</field>
560+
<statement name="INPUTS">
561+
<block type="input_dummy" id="IS.a0rVZ^J@xIV6pE,|7">
562+
<field name="ALIGN">LEFT</field>
563+
<statement name="FIELDS">
564+
<block type="field_static" id="Q13wejVii~{Z;w;.etPs">
565+
<field name="TEXT">取值</field>
566+
</block>
567+
</statement>
568+
<next>
569+
<block type="input_value" id="6QO[FdKy]]seb{`OU65K">
570+
<field name="INPUTNAME">pluginName</field>
571+
<field name="ALIGN">LEFT</field>
572+
<statement name="FIELDS">
573+
<block type="field_static" id="w4Tg$agA3H*UXGVb+n}V">
574+
<field name="TEXT">插件名</field>
575+
</block>
576+
</statement>
577+
<value name="TYPE">
578+
<shadow type="type_null" id="ODa,t{J3Qn$]CY9%s*3q"></shadow>
579+
</value>
580+
<next>
581+
<block type="input_value" id="{qMdpJ/*62mCSfF`9GPD">
582+
<field name="INPUTNAME">name</field>
583+
<field name="ALIGN">LEFT</field>
584+
<statement name="FIELDS">
585+
<block type="field_static" id="oOLZ^fOGu{15yw|Cnx%M">
586+
<field name="TEXT">名</field>
587+
</block>
588+
</statement>
589+
<value name="TYPE">
590+
<shadow type="type_null" id="a.*6rS5aA+DZl;fO*E[@"></shadow>
591+
</value>
592+
</block>
593+
</next>
594+
</block>
595+
</next>
596+
</block>
597+
</statement>
598+
<value name="TOOLTIP">
599+
<block type="text" id="wmMzU5{s-dxhu|R`?dpk" deletable="false" movable="false">
600+
<field name="TEXT">持久化存储,取值,和设置值配对使用</field>
601+
</block>
602+
</value>
603+
<value name="HELPURL">
604+
<block type="text" id="@66#TKHQD#fF^:fuD6hh" deletable="false" movable="false">
605+
<field name="TEXT"></field>
606+
</block>
607+
</value>
608+
<value name="OUTPUTTYPE">
609+
<shadow type="type_null" id="dcJH[9tYcA+dL)KY8)9("></shadow>
610+
</value>
611+
<value name="COLOUR">
612+
<block type="colour_hue" id="=Puq8A^,BxL7@%a)H/y{">
613+
<mutation colour="#80a55b"></mutation>
614+
<field name="HUE">90</field>
615+
</block>
616+
</value>
476617
</block></xml>

blockly_file/toolbox.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304
<block type="plugininfo"></block>
305305
<block type="groupmessageevent"></block>
306306
<block type="privatemessageevent"></block>
307-
<block type="grouprecallevent" deletable="false" movable="false" editable="false"></block>
307+
<block type="grouprecallevent"></block>
308308
</category>
309309
<category name="参数" colour="#5b80a5">
310310
<block type="message"></block>
@@ -317,4 +317,8 @@
317317
<block type="sendgroupmessage"></block>
318318
<block type="sendprivatemessage"></block>
319319
</category>
320+
<category name="键值对存储" colour="#5ba58c">
321+
<block type="dataset"></block>
322+
<block type="dataget"></block>
323+
</category>
320324
</xml>

bot/groupmessage.go

-17
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
package bot
22

33
import (
4-
"fmt"
54
"strconv"
65
"strings"
76

87
"github.com/LagrangeDev/LagrangeGo/client"
98
"github.com/LagrangeDev/LagrangeGo/message"
109
"github.com/dingdinglz/dingbot/appconfig"
1110
"github.com/dingdinglz/dingbot/database"
12-
"github.com/dingdinglz/dingbot/tool"
1311
"github.com/robertkrimen/otto"
1412
)
1513

16-
func AddVMFuncs(Vm *otto.Otto) {
17-
Vm.Set("SendGroupMessage", func(call otto.FunctionCall) otto.Value {
18-
DingQQBot.SendGroupMessage(tool.StringToUint32(call.Argument(0).String()), []message.IMessageElement{message.NewText(call.Argument(1).String())})
19-
return otto.Value{}
20-
})
21-
Vm.Set("SendPrivateMessage", func(call otto.FunctionCall) otto.Value {
22-
DingQQBot.SendPrivateMessage(tool.StringToUint32(call.Argument(0).String()), []message.IMessageElement{message.NewText(call.Argument(1).String())})
23-
return otto.Value{}
24-
})
25-
Vm.Set("DingbotPrint", func(call otto.FunctionCall) otto.Value {
26-
fmt.Println("[plugin] " + call.Argument(0).String())
27-
return otto.Value{}
28-
})
29-
}
30-
3114
func BotGroupMessageEvent(client *client.QQClient, event *message.GroupMessage) {
3215
if !database.OpenHave(event.GroupUin, "group") {
3316
return

bot/plugin.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
package bot
22

33
import (
4+
"fmt"
45
"io/fs"
56
"os"
67
"path/filepath"
78

9+
"github.com/LagrangeDev/LagrangeGo/message"
10+
"github.com/dingdinglz/dingbot/database"
11+
"github.com/dingdinglz/dingbot/tool"
812
"github.com/robertkrimen/otto"
913
)
1014

15+
func AddVMFuncs(Vm *otto.Otto) {
16+
Vm.Set("SendGroupMessage", func(call otto.FunctionCall) otto.Value {
17+
DingQQBot.SendGroupMessage(tool.StringToUint32(call.Argument(0).String()), []message.IMessageElement{message.NewText(call.Argument(1).String())})
18+
return otto.Value{}
19+
})
20+
Vm.Set("SendPrivateMessage", func(call otto.FunctionCall) otto.Value {
21+
DingQQBot.SendPrivateMessage(tool.StringToUint32(call.Argument(0).String()), []message.IMessageElement{message.NewText(call.Argument(1).String())})
22+
return otto.Value{}
23+
})
24+
Vm.Set("DingbotPrint", func(call otto.FunctionCall) otto.Value {
25+
fmt.Println("[plugin] " + call.Argument(0).String())
26+
return otto.Value{}
27+
})
28+
Vm.Set("DataSet", func(call otto.FunctionCall) otto.Value {
29+
database.PluginDataSet(call.Argument(0).String(), call.Argument(1).String(), call.Argument(2).String())
30+
return otto.Value{}
31+
})
32+
Vm.Set("DataGet", func(call otto.FunctionCall) otto.Value {
33+
v, _ := Vm.ToValue(database.PluginDataGet(call.Argument(0).String(), call.Argument(1).String()))
34+
return v
35+
})
36+
}
37+
1138
func RunPlugin(fRun func(*otto.Otto)) {
1239
rootPath, _ := os.Getwd()
1340
filepath.Walk(filepath.Join(rootPath, "data", "plugin"), func(path string, info fs.FileInfo, err error) error {

database/init.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func DatabaseInit() {
1616
if err != nil {
1717
tool.ErrorOut("database", "open error", err)
1818
}
19-
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{}, &StopTable{}, &MessageTable{})
19+
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{}, &StopTable{}, &MessageTable{}, &PluginData{})
20+
21+
// 清空message表的内容,message表用于存seq与message对应,便于取撤回内容
2022
DB.Delete(&MessageTable{}, "1=1")
2123
}

database/plugin.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package database
2+
3+
func PluginDataExist(pluginName string, Name string) bool {
4+
var i int64
5+
DB.Model(&PluginData{}).Where("plugin = ?", pluginName).Where("name = ?", Name).Count(&i)
6+
return i != 0
7+
}
8+
9+
func PluginDataSet(pluginName string, Name string, Value string) {
10+
var i PluginData
11+
if PluginDataExist(pluginName, Name) {
12+
DB.Delete(&PluginData{}, "plugin = ? and name = ?", pluginName, Name)
13+
}
14+
i.Plugin = pluginName
15+
i.Name = Name
16+
i.Value = Value
17+
DB.Create(&i)
18+
}
19+
20+
func PluginDataGet(pluginName string, Name string) string {
21+
if !PluginDataExist(pluginName, Name) {
22+
return ""
23+
}
24+
var i PluginData
25+
DB.Model(&PluginData{}).Where("plugin = ?", pluginName).Where("name = ?", Name).First(&i)
26+
return i.Value
27+
}

database/plugin_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package database
2+
3+
import (
4+
"testing"
5+
6+
"gorm.io/driver/sqlite"
7+
"gorm.io/gorm"
8+
)
9+
10+
func DatabaseInitTest() {
11+
DB, _ = gorm.Open(sqlite.Open("../data/data.db"))
12+
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{}, &StopTable{}, &MessageTable{}, &PluginData{})
13+
DB.Delete(&MessageTable{}, "1=1")
14+
}
15+
16+
func TestPluginInsert(t *testing.T) {
17+
DatabaseInitTest()
18+
PluginDataSet("test", "test", "2")
19+
}

database/tables.go

+10
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,13 @@ type MessageTable struct {
3636
func (MessageTable) TableName() string {
3737
return "message"
3838
}
39+
40+
type PluginData struct {
41+
Plugin string
42+
Name string
43+
Value string
44+
}
45+
46+
func (PluginData) TableName() string {
47+
return "plugin"
48+
}

web/index/plugin-edit.html

+56-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
<block type="plugininfo"></block>
318318
<block type="groupmessageevent"></block>
319319
<block type="privatemessageevent"></block>
320-
<block type="grouprecallevent" deletable="false" movable="false" editable="false"></block>
320+
<block type="grouprecallevent"></block>
321321
</category>
322322
<category name="参数" colour="#5b80a5">
323323
<block type="message"></block>
@@ -330,6 +330,10 @@
330330
<block type="sendgroupmessage"></block>
331331
<block type="sendprivatemessage"></block>
332332
</category>
333+
<category name="键值对存储" colour="#5ba58c">
334+
<block type="dataset"></block>
335+
<block type="dataget"></block>
336+
</category>
333337
</xml>
334338
<script>
335339
var toolbox = document.getElementById("toolbox");
@@ -557,6 +561,57 @@
557561
return [code, javascript.Order.NONE];
558562
};
559563

564+
Blockly.Blocks['dataset'] = {
565+
init: function() {
566+
this.appendDummyInput()
567+
.appendField("设置值");
568+
this.appendValueInput("pluginName")
569+
.setCheck(null)
570+
.appendField("插件名");
571+
this.appendValueInput("name")
572+
.setCheck(null)
573+
.appendField("名");
574+
this.appendValueInput("value")
575+
.setCheck(null)
576+
.appendField("值");
577+
this.setPreviousStatement(true, null);
578+
this.setNextStatement(true, null);
579+
this.setColour(90);
580+
this.setTooltip("持久化存储,设置值,数据存储在本地数据库内");
581+
this.setHelpUrl("");
582+
}
583+
};
584+
javascript.javascriptGenerator.forBlock['dataset'] = function(block, generator) {
585+
var value_pluginname = generator.valueToCode(block, 'pluginName', javascript.Order.ATOMIC);
586+
var value_name = generator.valueToCode(block, 'name', javascript.Order.ATOMIC);
587+
var value_value = generator.valueToCode(block, 'value', javascript.Order.ATOMIC);
588+
var code = '\nDataSet('+value_pluginname+','+value_name+','+value_value+');\n';
589+
return code;
590+
};
591+
592+
Blockly.Blocks['dataget'] = {
593+
init: function() {
594+
this.appendDummyInput()
595+
.appendField("取值");
596+
this.appendValueInput("pluginName")
597+
.setCheck(null)
598+
.appendField("插件名");
599+
this.appendValueInput("name")
600+
.setCheck(null)
601+
.appendField("名");
602+
this.setOutput(true, null);
603+
this.setColour(90);
604+
this.setTooltip("持久化存储,取值,和设置值配对使用");
605+
this.setHelpUrl("");
606+
}
607+
};
608+
javascript.javascriptGenerator.forBlock['dataget'] = function(block, generator) {
609+
var value_pluginname = generator.valueToCode(block, 'pluginName', javascript.Order.ATOMIC);
610+
var value_name = generator.valueToCode(block, 'name', javascript.Order.ATOMIC);
611+
var code = 'DataGet('+value_pluginname+','+value_name+")";
612+
return [code, javascript.Order.NONE];
613+
};
614+
560615
function LoadPage(data)
561616
{
562617
Blockly.serialization.workspaces.load(JSON.parse(data), workspace);

0 commit comments

Comments
 (0)