Skip to content

Commit

Permalink
fix: adopt APIV7
Browse files Browse the repository at this point in the history
  • Loading branch information
MoYoez committed Mar 11, 2024
1 parent a80b4ff commit d6718e5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
19 changes: 19 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (ctx *Ctx) CheckSession() Rule {
msg := ctx.Value.(*tgba.Message)
return func(ctx2 *Ctx) bool {
msg2, ok := ctx.Value.(*tgba.Message)
if !ok || msg.From == nil || msg.Chat == nil || msg2.From == nil || msg2.Chat == nil { // 确保无空
if !ok || msg.From == nil || msg.Chat.ID == 0 || msg2.From == nil || msg2.Chat.ID == 0 { // 确保无空
return false
}
return msg.From.ID == msg2.From.ID && msg.Chat.ID == msg2.Chat.ID
Expand All @@ -93,7 +93,7 @@ func (ctx *Ctx) Send(replytosender bool, c tgba.Chattable) (tgba.Message, error)
func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (tgba.Message, error) {
msg := tgba.NewMessage(ctx.Message.Chat.ID, fmt.Sprint(printable...))
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
msg.ReplyParameters.MessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(&msg)
}
Expand All @@ -102,13 +102,13 @@ func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (tgba.Mes
func (ctx *Ctx) SendMessage(replytosender bool, text string, entities ...tgba.MessageEntity) (tgba.Message, error) {
msg := &tgba.MessageConfig{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
ChatConfig: tgba.ChatConfig{ChatID: ctx.Message.Chat.ID},
},
Text: text,
Entities: entities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
msg.ReplyParameters.MessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(msg)
}
Expand All @@ -118,15 +118,15 @@ func (ctx *Ctx) SendPhoto(file tgba.RequestFileData, replytosender bool, caption
msg := &tgba.PhotoConfig{
BaseFile: tgba.BaseFile{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
ChatConfig: tgba.ChatConfig{ChatID: ctx.Message.Chat.ID},
},
File: file,
},
Caption: caption,
CaptionEntities: captionentities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
msg.ReplyParameters.MessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(msg)
}
Expand All @@ -136,15 +136,15 @@ func (ctx *Ctx) SendAudio(file tgba.RequestFileData, replytosender bool, caption
msg := tgba.AudioConfig{
BaseFile: tgba.BaseFile{
BaseChat: tgba.BaseChat{
ChatID: ctx.Message.Chat.ID,
ChatConfig: tgba.ChatConfig{ChatID: ctx.Message.Chat.ID},
},
File: file,
},
Caption: caption,
CaptionEntities: captionentities,
}
if replytosender {
msg.ReplyToMessageID = ctx.Message.MessageID
msg.ReplyParameters.MessageID = ctx.Message.MessageID
}
return ctx.Caller.Send(&msg)
}
Expand All @@ -154,7 +154,7 @@ func (ctx *Ctx) Block() {
ctx.ma.SetBlock(true)
}

// Block 在 pre, rules, mid 阶段阻止后续触发
// Break Block 在 pre, rules, mid 阶段阻止后续触发
func (ctx *Ctx) Break() {
ctx.ma.Break = true
}
4 changes: 2 additions & 2 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func init() {
service.Permit(uid, grp)
msg += "\n+ 已允许" + usr
} else {
member, err := ctx.Caller.GetChatMember(tgba.GetChatMemberConfig{ChatConfigWithUser: tgba.ChatConfigWithUser{ChatID: ctx.Message.Chat.ID, UserID: uid}})
member, err := ctx.Caller.GetChatMember(tgba.GetChatMemberConfig{ChatConfigWithUser: tgba.ChatConfigWithUser{ChatConfig: tgba.ChatConfig{ChatID: ctx.Message.Chat.ID}, UserID: uid}})
if err == nil && !member.HasLeft() && !member.WasKicked() {
service.Permit(uid, grp)
msg += "\n+ 已允许" + usr
Expand All @@ -207,7 +207,7 @@ func init() {
service.Ban(uid, grp)
msg += "\n- 已禁止" + usr
} else {
member, err := ctx.Caller.GetChatMember(tgba.GetChatMemberConfig{ChatConfigWithUser: tgba.ChatConfigWithUser{ChatID: ctx.Message.Chat.ID, UserID: uid}})
member, err := ctx.Caller.GetChatMember(tgba.GetChatMemberConfig{ChatConfigWithUser: tgba.ChatConfigWithUser{ChatConfig: tgba.ChatConfig{ChatID: ctx.Message.Chat.ID}, UserID: uid}})
if err == nil && !member.HasLeft() && !member.WasKicked() {
service.Ban(uid, grp)
msg += "\n- 已禁止" + usr
Expand Down
40 changes: 20 additions & 20 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func CheckChat(chatId ...int64) Rule {
return func(ctx *Ctx) bool {
switch msg := ctx.Value.(type) {
case *tgba.Message:
if msg.Chat == nil { // 确保无空
if msg.Chat.ID == 0 { // 确保无空
return false
}
for _, cid := range chatId {
Expand All @@ -324,7 +324,7 @@ func CheckChat(chatId ...int64) Rule {
}
return false
case *tgba.CallbackQuery:
if msg.Message == nil || msg.Message.Chat == nil {
if msg.Message == nil || msg.Message.Chat.ID == 0 {
return false
}
for _, cid := range chatId {
Expand All @@ -342,7 +342,7 @@ func CheckChat(chatId ...int64) Rule {
// OnlyPrivate requires that the ctx.Event is private message
func OnlyPrivate(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "private"
Expand All @@ -351,7 +351,7 @@ func OnlyPrivate(ctx *Ctx) bool {
// OnlyGroup requires that the ctx.Event is group message
func OnlyGroup(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "group"
Expand All @@ -360,7 +360,7 @@ func OnlyGroup(ctx *Ctx) bool {
// OnlyGroupOrSuperGroup Tips: many groups use supergroup and only a few groups use Group Type.
func OnlyGroupOrSuperGroup(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "group" || msg.Chat.Type == "supergroup"
Expand All @@ -369,7 +369,7 @@ func OnlyGroupOrSuperGroup(ctx *Ctx) bool {
// OnlySuperGroup requires that the ctx.Event is supergroup message
func OnlySuperGroup(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "supergroup"
Expand All @@ -378,7 +378,7 @@ func OnlySuperGroup(ctx *Ctx) bool {
// OnlyPublic requires that the ctx.Event is group or supergroup message
func OnlyPublic(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "supergroup" || msg.Chat.Type == "group"
Expand All @@ -387,7 +387,7 @@ func OnlyPublic(ctx *Ctx) bool {
// OnlyChannel requires that the ctx.Event is channel message
func OnlyChannel(ctx *Ctx) bool {
msg, ok := ctx.Value.(*tgba.Message)
if !ok || msg.Chat == nil { // 确保无空
if !ok || msg.Chat.ID == 0 { // 确保无空
return false
}
return msg.Chat.Type == "channel"
Expand Down Expand Up @@ -428,14 +428,14 @@ func CreaterPermission(ctx *Ctx) bool {
}
switch msg := ctx.Value.(type) {
case *tgba.Message:
if msg.From == nil || msg.Chat == nil { // 确保无空
if msg.From == nil || msg.Chat.ID == 0 { // 确保无空
return false
}
m, err := ctx.Caller.GetChatMember(
tgba.GetChatMemberConfig{
ChatConfigWithUser: tgba.ChatConfigWithUser{
ChatID: msg.Chat.ID,
UserID: msg.From.ID,
ChatConfig: tgba.ChatConfig{ChatID: msg.Chat.ID},
UserID: msg.From.ID,
},
},
)
Expand All @@ -444,14 +444,14 @@ func CreaterPermission(ctx *Ctx) bool {
}
return m.IsCreator()
case *tgba.CallbackQuery:
if msg.From == nil || msg.Message == nil || msg.Message.Chat == nil {
if msg.From == nil || msg.Message == nil || msg.Message.Chat.ID == 0 {
return false
}
m, err := ctx.Caller.GetChatMember(
tgba.GetChatMemberConfig{
ChatConfigWithUser: tgba.ChatConfigWithUser{
ChatID: msg.Message.Chat.ID,
UserID: msg.From.ID,
ChatConfig: tgba.ChatConfig{ChatID: msg.Message.Chat.ID},
UserID: msg.From.ID,
},
},
)
Expand All @@ -471,14 +471,14 @@ func AdminPermission(ctx *Ctx) bool {
}
switch msg := ctx.Value.(type) {
case *tgba.Message:
if msg.From == nil || msg.Chat == nil { // 确保无空
if msg.From == nil || msg.Chat.ID == 0 { // 确保无空
return false
}
m, err := ctx.Caller.GetChatMember(
tgba.GetChatMemberConfig{
ChatConfigWithUser: tgba.ChatConfigWithUser{
ChatID: msg.Chat.ID,
UserID: msg.From.ID,
ChatConfig: tgba.ChatConfig{ChatID: msg.Chat.ID},
UserID: msg.From.ID,
},
},
)
Expand All @@ -487,14 +487,14 @@ func AdminPermission(ctx *Ctx) bool {
}
return m.IsCreator() || m.IsAdministrator()
case *tgba.CallbackQuery:
if msg.From == nil || msg.Message == nil || msg.Message.Chat == nil {
if msg.From == nil || msg.Message == nil || msg.Message.Chat.ID == 0 {
return false
}
m, err := ctx.Caller.GetChatMember(
tgba.GetChatMemberConfig{
ChatConfigWithUser: tgba.ChatConfigWithUser{
ChatID: msg.Message.Chat.ID,
UserID: msg.From.ID,
ChatConfig: tgba.ChatConfig{ChatID: msg.Message.Chat.ID},
UserID: msg.From.ID,
},
},
)
Expand Down

0 comments on commit d6718e5

Please sign in to comment.