Skip to content

Commit

Permalink
Only unban users not previously banned. /help bugfix.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopaganini committed Jan 30, 2021
1 parent da90861 commit c910423
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,16 @@ func banUser(bot kickChatMemberer, chatID int64, userID int) error {
return err
}

// unBanUser removed the bans on a user using a ChatID and UserID.
func unBanUser(bot unbanChatMemberer, chatID int64, userID int) error {
memberConfig := tgbotapi.ChatMemberConfig{ChatID: chatID, UserID: userID}
_, err := bot.UnbanChatMember(memberConfig)
return err
}

// kickUser kicks (not ban) a user using a ChatID and UserID.
func kickUser(bot kickUnbanChatMemberer, chatID int64, userID int) error {
func kickUser(bot kickChatMemberer, chatID int64, userID int) error {
memberConfig := tgbotapi.ChatMemberConfig{ChatID: chatID, UserID: userID}
_, err := bot.KickChatMember(tgbotapi.KickChatMemberConfig{ChatMemberConfig: memberConfig})
if err != nil {
return err
}
_, err = bot.UnbanChatMember(memberConfig)
return err
}
14 changes: 14 additions & 0 deletions src/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,20 @@ func isAdmin(bot getChatMemberer, chatID int64, userID int) (bool, error) {
return (chatmember.IsAdministrator() || chatmember.IsCreator()), nil
}

// isBanned returns true if the user was previously banned (kick/banned).
func isBanned(bot getChatMemberer, chatID int64, userID int) (bool, error) {
q := tgbotapi.ChatConfigWithUser{
ChatID: chatID,
UserID: userID,
}

chatmember, err := bot.GetChatMember(q)
if err != nil {
return false, err
}
return chatmember.WasKicked(), nil
}

// stringInSlice returns true if a given string is in a string slice, false otherwise.
func stringInSlice(str string, list []string) bool {
for _, s := range list {
Expand Down
2 changes: 1 addition & 1 deletion src/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestHelpHandler(t *testing.T) {
pvtOnly: true,
},
},
wantStr: "/bar_admin: mock_bar_admin_command (Admin)\n/foo: mock_foo_command",
wantStr: "/bar\\_admin: mock_bar_admin_command (Admin)\n/foo: mock_foo_command",
},
}

Expand Down
17 changes: 17 additions & 0 deletions src/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,28 @@ func (x *opBot) captchaReaper(bot tgbotInterface, update tgbotapi.Update, user t
log.Printf("Warning: Unable to send 'invalid captcha' message.")
}

// Do nothing if user is already kicked (probably by an admin).
// Proced to kick+unban if that's not the case. We need to get
// the status before kickUser (below) for obvious reasons.
banned, err := isBanned(bot, update.Message.Chat.ID, user.ID)
if err != nil {
log.Printf("Warning: Unable to get information for user %s (uid=%d): %v", name, user.ID, err)
}

// Kick user and remove from list.
if err = kickUser(bot, update.Message.Chat.ID, user.ID); err != nil {
log.Printf("Warning: Unable to kick user %s (uid=%d) out of the channel.", name, user.ID)
}

x.pendingCaptcha.del(user.ID)

if banned {
log.Printf("User %s (uid=%d) has already been banned (by admin?) Not unbanning.", name, user.ID)
return
}
if err = unBanUser(bot, update.Message.Chat.ID, user.ID); err != nil {
log.Printf("Warning: Unable to UNBAN user %s (uid=%d) (May be locked out.)", name, user.ID)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (x *opBot) helpHandler(bot tgbotInterface, update tgbotapi.Update) error {
admin = " (Admin)"
}
if bcmd.enabled {
helpMsg = append(helpMsg, fmt.Sprintf("/%s: %s%s", c, bcmd.desc, admin))
helpMsg = append(helpMsg, fmt.Sprintf("/%s: %s%s", markdownEscape(c), bcmd.desc, admin))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type kickChatMemberer interface {
KickChatMember(tgbotapi.KickChatMemberConfig) (tgbotapi.APIResponse, error)
}

type kickUnbanChatMemberer interface {
KickChatMember(tgbotapi.KickChatMemberConfig) (tgbotapi.APIResponse, error)
type unbanChatMemberer interface {
UnbanChatMember(tgbotapi.ChatMemberConfig) (tgbotapi.APIResponse, error)
}
4 changes: 2 additions & 2 deletions translations/en-us-all.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ register_help = "Command help"
notifications_help = "Enables/disables notifications"
ban_help = "Reports message to admins. Remember to send the command in response to a message"
new_user_probation_time_help = "User must be in the group for this long to gain full privileges (E.g, 24h, 0 = disable feature)"
welcome_message_ttl_help = "Set the time-to-live for the welcome messages (E.g: /welcome_message_ttl 5m)"
captcha_time_help = "Set the time new users have to correctly answer the captcha (E.g: /captcha_time 1m, 0 = disable feature)"
welcome_message_ttl_help = "Set the time-to-live for the welcome messages (E.g: /welcome\\_message\\_ttl 5m)"
captcha_time_help = "Set the time new users have to correctly answer the captcha (E.g: /captcha\\_time 1m, 0 = disable feature)"

# Error messages

Expand Down
4 changes: 2 additions & 2 deletions translations/pt-br-all.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ register_help = "Help dos comandos"
notifications_help = "Ativa/desativa notificações"
ban_help = "Reporta mensagem para os admins. Use este comando em resposta a uma mensagem"
new_user_probation_time_help = "Configura o tempo minimo de permanência no grupo para eliminar restrições de novos usuários (Ex: 24h, 0 = desabilita restrições)"
welcome_message_ttl_help = "Configura o tempo de vida das mensagens de boas-vindas (Ex: /welcome_message_ttl 5m)"
captcha_time_help = "Configura o tempo máximo para responder ao captcha. (Ex: /captcha_time 1m, 0 = desabilita captcha)"
welcome_message_ttl_help = "Configura o tempo de vida das mensagens de boas-vindas (Ex: /welcome\\_message\\_ttl 5m)"
captcha_time_help = "Configura o tempo máximo para responder ao captcha. (Ex: /captcha\\_time 1m, 0 = desabilita captcha)"

# Error messages

Expand Down

0 comments on commit c910423

Please sign in to comment.