Skip to content

Commit

Permalink
fix: determine GroupAnonymousBot as admin (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww committed May 30, 2023
1 parent 30234e6 commit fdbf909
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions internal/bots/telegram/handlers/recap/callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (h *CallbackQueryHandler) handleCallbackQueryToggle(c *tgbot.Context) (tgbo
return nil, nil
}

err = checkToggle(c, chatID, fromID)
err = checkToggle(c, chatID, c.Update.CallbackQuery.From)
if err != nil {
if errors.Is(err, errOperationCanNotBeDone) {
return nil, tgbot.
Expand Down Expand Up @@ -281,7 +281,7 @@ func (h *CallbackQueryHandler) handleCallbackQueryAssignMode(c *tgbot.Context) (
WithReplyMarkup(tgbotapi.NewInlineKeyboardMarkup(msg.ReplyMarkup.InlineKeyboard...))
}

err = checkAssignMode(c, chatID, fromID)
err = checkAssignMode(c, chatID, c.Update.CallbackQuery.From)
if err != nil {
if errors.Is(err, errOperationCanNotBeDone) {
return nil, tgbot.
Expand Down Expand Up @@ -366,7 +366,7 @@ func (h *CallbackQueryHandler) handleCallbackQueryComplete(c *tgbot.Context) (tg
WithEdit(msg).
WithReplyMarkup(tgbotapi.NewInlineKeyboardMarkup(msg.ReplyMarkup.InlineKeyboard...))
}
if !is {
if !is && !c.Bot.IsGroupAnonymousBot(c.Update.Message.From) {
return nil, tgbot.
NewMessageError(fmt.Errorf("%w,%s", errOperationCanNotBeDone, "开启/关闭聊天记录回顾功能需要<b>管理员</b>权限执行 /configure_recap 命令。").Error()).
WithEdit(msg).
Expand Down
23 changes: 16 additions & 7 deletions internal/bots/telegram/handlers/recap/configure_recap_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ var (
errOperationCanNotBeDone = errors.New("抱歉,此操作无法进行")
)

func checkToggle(ctx *tgbot.Context, chatID int64, fromID int64) error {
func checkToggle(ctx *tgbot.Context, chatID int64, user *tgbotapi.User) error {
if !lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(ctx.Update.FromChat().Type)) {
return fmt.Errorf("%w,%s", errOperationCanNotBeDone, "聊天记录回顾功能只有<b>群组</b>和<b>超级群组</b>的管理员可以配置哦!\n请将 Bot 添加到群组中,并配置 Bot 为管理员后使用管理员权限的用户账户为 Bot 进行配置吧。")
}
if user == nil {
return fmt.Errorf("%w,%s", errOperationCanNotBeDone, "开启/关闭聊天记录回顾功能需要<b>管理员</b>权限执行 /configure_recap 命令。")
}

is, err := ctx.IsUserMemberStatus(fromID, []telegram.MemberStatus{
is, err := ctx.IsUserMemberStatus(user.ID, []telegram.MemberStatus{
telegram.MemberStatusCreator,
telegram.MemberStatusAdministrator,
})
if err != nil {
return err
}
if !is {
if !is && !ctx.Bot.IsGroupAnonymousBot(user) {
return fmt.Errorf("%w,%s", errOperationCanNotBeDone, "开启/关闭聊天记录回顾功能需要<b>管理员</b>权限执行 /configure_recap 命令。")
}

Expand All @@ -45,12 +48,15 @@ func checkToggle(ctx *tgbot.Context, chatID int64, fromID int64) error {
return nil
}

func checkAssignMode(ctx *tgbot.Context, chatID int64, fromID int64) error {
func checkAssignMode(ctx *tgbot.Context, chatID int64, user *tgbotapi.User) error {
if !lo.Contains([]telegram.ChatType{telegram.ChatTypeGroup, telegram.ChatTypeSuperGroup}, telegram.ChatType(ctx.Update.FromChat().Type)) {
return fmt.Errorf("%w,%s", errOperationCanNotBeDone, "聊天记录回顾功能只有<b>群组</b>和<b>超级群组</b>的管理员可以配置哦!\n请将 Bot 添加到群组中,并配置 Bot 为管理员后使用管理员权限的用户账户为 Bot 进行配置吧。")
}
if user == nil {
return fmt.Errorf("%w,%s", errOperationCanNotBeDone, "配置聊天记录回顾功能的模式需要<b>群组创建者</b>权限执行 /configure_recap 命令。")
}

is, err := ctx.IsUserMemberStatus(fromID, []telegram.MemberStatus{
is, err := ctx.IsUserMemberStatus(user.ID, []telegram.MemberStatus{
telegram.MemberStatusCreator,
})
if err != nil {
Expand Down Expand Up @@ -116,14 +122,17 @@ func (h *CommandHandler) handleConfigureRecapCommand(c *tgbot.Context) (tgbot.Re
return nil, tgbot.NewMessageError("只有在群组和超级群组内猜可以配置聊天记录回顾功能哦!").WithReply(c.Update.Message)
}

is, err := c.IsUserMemberStatus(c.Update.Message.From.ID, []telegram.MemberStatus{telegram.MemberStatusCreator, telegram.MemberStatusAdministrator})
is, err := c.IsUserMemberStatus(c.Update.Message.From.ID, []telegram.MemberStatus{
telegram.MemberStatusCreator,
telegram.MemberStatusAdministrator,
})
if err != nil {
return nil, tgbot.
NewExceptionError(err).
WithMessage("暂时无法配置聊天记录回顾功能,请稍后再试!").
WithReply(c.Update.Message)
}
if !is {
if !is && !c.Bot.IsGroupAnonymousBot(c.Update.Message.From) {
return nil, tgbot.
NewMessageError(fmt.Errorf("%w,%s", errOperationCanNotBeDone, "开启/关闭聊天记录回顾功能需要<b>管理员</b>权限执行 /configure_recap 命令。").Error()).
WithReply(c.Update.Message).
Expand Down
8 changes: 8 additions & 0 deletions pkg/bots/tgbot/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ func (b *Bot) IsUserMemberStatus(chatID int64, userID int64, status []telegram.M
return false, nil
}

func (b *Bot) IsGroupAnonymousBot(user *tgbotapi.User) bool {
if user == nil {
return false
}

return user.ID == 1087968824 && user.IsBot && user.UserName == "GroupAnonymousBot" && user.FirstName == "Group"
}

func (b *Bot) PushOneDeleteLaterMessage(forUserID int64, chatID int64, messageID int) error {
if forUserID == 0 || chatID == 0 || messageID == 0 {
return nil
Expand Down

0 comments on commit fdbf909

Please sign in to comment.