Skip to content

bilibilipush添加艾特全体功能 #758

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

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions plugin/bilibili/bilibilipush.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func init() {
"- 取消b站动态订阅[uid|name]\n" +
"- 取消b站直播订阅[uid|name]\n" +
"- b站推送列表\n" +
"- [开启|关闭]艾特全体\n" +
"Tips: 需要配合job一起使用, 全局只需要设置一个, 无视响应状态推送, 下为例子\n" +
"记录在\"@every 5m\"触发的指令)\n" +
"拉取b站推送",
Expand All @@ -74,6 +75,23 @@ func init() {
dbpath := en.DataFolder()
dbfile := dbpath + "push.db"
bdb = initializePush(dbfile)
en.OnFullMatch(`开启艾特全体`, zero.UserOrGrpAdmin, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
if err := changeAtAll(gid, 1); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("已开启艾特全体Oo"))
})

en.OnFullMatch(`关闭艾特全体`, zero.UserOrGrpAdmin, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
if err := changeAtAll(gid, 0); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("已关闭艾特全体Oo"))
})

en.OnRegex(`^添加[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
Expand All @@ -92,6 +110,7 @@ func init() {
}
ctx.SendChain(message.Text("已添加" + name + "的订阅"))
})

en.OnRegex(`^取消[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
name, err := getName(buid)
Expand Down Expand Up @@ -143,6 +162,7 @@ func init() {
}
ctx.SendChain(message.Text("已取消" + name + "的直播订阅"))
})

en.OnRegex(`^[B|b]站推送列表$`, zero.UserOrGrpAdmin).SetBlock(true).Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
if gid == 0 {
Expand Down Expand Up @@ -189,6 +209,14 @@ func init() {
})
}

func changeAtAll(groupId int64, b int) (err error) {
bpMap := map[string]any{
"group_id": groupId,
"at_all": b,
}
return bdb.updateAtAll(bpMap)
}

// 取得uid的名字
func getName(buid int64) (name string, err error) {
var ok bool
Expand Down Expand Up @@ -466,6 +494,9 @@ func sendLive(ctx *zero.Ctx) error {
time.Sleep(time.Millisecond * 100)
switch {
case gid > 0:
if res := bdb.getAtAll(gid); res == 1 {
msg = append([]message.MessageSegment{message.AtAll()}, msg...)
}
ctx.SendGroupMessage(gid, msg)
case gid < 0:
ctx.SendPrivateMessage(-gid, msg)
Expand Down
40 changes: 39 additions & 1 deletion plugin/bilibili/bilibilipushmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func (bilibiliup) TableName() string {
return "bilibili_up"
}

type bilibiliAt struct {
GroupID int64 `gorm:"column:group_id;primary_key" json:"group_id"`
AtAll int64 `gorm:"column:at_all;default:0" json:"at_all"`
}

func (bilibiliAt) TableName() string {
return "bilibili_at"
}

// initializePush 初始化bilibilipushdb数据库
func initializePush(dbpath string) *bilibilipushdb {
var err error
Expand All @@ -48,7 +57,7 @@ func initializePush(dbpath string) *bilibilipushdb {
if err != nil {
panic(err)
}
gdb.AutoMigrate(&bilibilipush{}).AutoMigrate(&bilibiliup{})
gdb.AutoMigrate(&bilibilipush{}).AutoMigrate(&bilibiliup{}).AutoMigrate(&bilibiliAt{})
return (*bilibilipushdb)(gdb)
}

Expand Down Expand Up @@ -130,6 +139,35 @@ func (bdb *bilibilipushdb) getAllPushByGroup(groupID int64) (bpl []bilibilipush)
return
}

func (bdb *bilibilipushdb) getAtAll(groupID int64) (res int64) {
db := (*gorm.DB)(bdb)
var bpl bilibiliAt
db.Model(&bilibilipush{}).Find(&bpl, "group_id = ?", groupID)
res = bpl.AtAll
return
}

func (bdb *bilibilipushdb) updateAtAll(bpMap map[string]any) (err error) {
db := (*gorm.DB)(bdb)
bp := bilibiliAt{}
data, err := json.Marshal(&bpMap)
if err != nil {
return
}
err = json.Unmarshal(data, &bp)
if err != nil {
return
}
if err = db.Model(&bilibiliAt{}).First(&bp, "group_id = ?", bp.GroupID).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
err = db.Model(&bilibiliAt{}).Create(&bp).Error
}
} else {
err = db.Model(&bilibiliAt{}).Where("group_id = ?", bp.GroupID).Update(bpMap).Error
}
return
}

func (bdb *bilibilipushdb) insertBilibiliUp(buid int64, name string) {
db := (*gorm.DB)(bdb)
bu := bilibiliup{
Expand Down