Skip to content

Commit ba7c6cb

Browse files
authored
bilibilipush添加艾特全体功能 (#758)
* 添加艾特全体功能 * bilibilipush添加艾特全体功能
1 parent d3e587f commit ba7c6cb

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

plugin/bilibili/bilibilipush.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func init() {
6464
"- 取消b站动态订阅[uid|name]\n" +
6565
"- 取消b站直播订阅[uid|name]\n" +
6666
"- b站推送列表\n" +
67+
"- [开启|关闭]艾特全体\n" +
6768
"Tips: 需要配合job一起使用, 全局只需要设置一个, 无视响应状态推送, 下为例子\n" +
6869
"记录在\"@every 5m\"触发的指令)\n" +
6970
"拉取b站推送",
@@ -74,6 +75,23 @@ func init() {
7475
dbpath := en.DataFolder()
7576
dbfile := dbpath + "push.db"
7677
bdb = initializePush(dbfile)
78+
en.OnFullMatch(`开启艾特全体`, zero.UserOrGrpAdmin, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
79+
gid := ctx.Event.GroupID
80+
if err := changeAtAll(gid, 1); err != nil {
81+
ctx.SendChain(message.Text("ERROR: ", err))
82+
return
83+
}
84+
ctx.SendChain(message.Text("已开启艾特全体Oo"))
85+
})
86+
87+
en.OnFullMatch(`关闭艾特全体`, zero.UserOrGrpAdmin, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
88+
gid := ctx.Event.GroupID
89+
if err := changeAtAll(gid, 0); err != nil {
90+
ctx.SendChain(message.Text("ERROR: ", err))
91+
return
92+
}
93+
ctx.SendChain(message.Text("已关闭艾特全体Oo"))
94+
})
7795

7896
en.OnRegex(`^添加[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
7997
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
@@ -92,6 +110,7 @@ func init() {
92110
}
93111
ctx.SendChain(message.Text("已添加" + name + "的订阅"))
94112
})
113+
95114
en.OnRegex(`^取消[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
96115
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
97116
name, err := getName(buid)
@@ -143,6 +162,7 @@ func init() {
143162
}
144163
ctx.SendChain(message.Text("已取消" + name + "的直播订阅"))
145164
})
165+
146166
en.OnRegex(`^[B|b]站推送列表$`, zero.UserOrGrpAdmin).SetBlock(true).Handle(func(ctx *zero.Ctx) {
147167
gid := ctx.Event.GroupID
148168
if gid == 0 {
@@ -189,6 +209,14 @@ func init() {
189209
})
190210
}
191211

212+
func changeAtAll(groupId int64, b int) (err error) {
213+
bpMap := map[string]any{
214+
"group_id": groupId,
215+
"at_all": b,
216+
}
217+
return bdb.updateAtAll(bpMap)
218+
}
219+
192220
// 取得uid的名字
193221
func getName(buid int64) (name string, err error) {
194222
var ok bool
@@ -466,6 +494,9 @@ func sendLive(ctx *zero.Ctx) error {
466494
time.Sleep(time.Millisecond * 100)
467495
switch {
468496
case gid > 0:
497+
if res := bdb.getAtAll(gid); res == 1 {
498+
msg = append([]message.MessageSegment{message.AtAll()}, msg...)
499+
}
469500
ctx.SendGroupMessage(gid, msg)
470501
case gid < 0:
471502
ctx.SendPrivateMessage(-gid, msg)

plugin/bilibili/bilibilipushmodel.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func (bilibiliup) TableName() string {
3333
return "bilibili_up"
3434
}
3535

36+
type bilibiliAt struct {
37+
GroupID int64 `gorm:"column:group_id;primary_key" json:"group_id"`
38+
AtAll int64 `gorm:"column:at_all;default:0" json:"at_all"`
39+
}
40+
41+
func (bilibiliAt) TableName() string {
42+
return "bilibili_at"
43+
}
44+
3645
// initializePush 初始化bilibilipushdb数据库
3746
func initializePush(dbpath string) *bilibilipushdb {
3847
var err error
@@ -48,7 +57,7 @@ func initializePush(dbpath string) *bilibilipushdb {
4857
if err != nil {
4958
panic(err)
5059
}
51-
gdb.AutoMigrate(&bilibilipush{}).AutoMigrate(&bilibiliup{})
60+
gdb.AutoMigrate(&bilibilipush{}).AutoMigrate(&bilibiliup{}).AutoMigrate(&bilibiliAt{})
5261
return (*bilibilipushdb)(gdb)
5362
}
5463

@@ -130,6 +139,35 @@ func (bdb *bilibilipushdb) getAllPushByGroup(groupID int64) (bpl []bilibilipush)
130139
return
131140
}
132141

142+
func (bdb *bilibilipushdb) getAtAll(groupID int64) (res int64) {
143+
db := (*gorm.DB)(bdb)
144+
var bpl bilibiliAt
145+
db.Model(&bilibilipush{}).Find(&bpl, "group_id = ?", groupID)
146+
res = bpl.AtAll
147+
return
148+
}
149+
150+
func (bdb *bilibilipushdb) updateAtAll(bpMap map[string]any) (err error) {
151+
db := (*gorm.DB)(bdb)
152+
bp := bilibiliAt{}
153+
data, err := json.Marshal(&bpMap)
154+
if err != nil {
155+
return
156+
}
157+
err = json.Unmarshal(data, &bp)
158+
if err != nil {
159+
return
160+
}
161+
if err = db.Model(&bilibiliAt{}).First(&bp, "group_id = ?", bp.GroupID).Error; err != nil {
162+
if gorm.IsRecordNotFoundError(err) {
163+
err = db.Model(&bilibiliAt{}).Create(&bp).Error
164+
}
165+
} else {
166+
err = db.Model(&bilibiliAt{}).Where("group_id = ?", bp.GroupID).Update(bpMap).Error
167+
}
168+
return
169+
}
170+
133171
func (bdb *bilibilipushdb) insertBilibiliUp(buid int64, name string) {
134172
db := (*gorm.DB)(bdb)
135173
bu := bilibiliup{

0 commit comments

Comments
 (0)