Skip to content

Commit

Permalink
improve: not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Jul 26, 2024
1 parent c115449 commit 1ad3d5f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 83 deletions.
49 changes: 49 additions & 0 deletions FallenSub/modules/fSub.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,52 @@ func logError(message string, err error) error {
config.ErrorLog.Printf("[ForceSub] %s: %v", message, err)
return err
}

// unMuteMe unMutes the user if they have joined the channel.
func unMuteMe(b *gotgbot.Bot, ctx *ext.Context) error {
query := ctx.Update.CallbackQuery
user := ctx.EffectiveUser
chat := ctx.EffectiveChat

isMuted, err := db.IsMuted(chat.Id, user.Id)
if !isMuted {
_, _ = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not muted by me.", ShowAlert: true})
return err
}

fSub, err := db.GetFSubSetting(chat.Id)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error getting fSub setting: %s [chatId: %d]", err, chat.Id), err)
}

member, err := b.GetChatMember(fSub.ForceSubChannel, user.Id, nil)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error getting chat member: %s [chatId: %d]", err, chat.Id), err)
}

stats := member.MergeChatMember()

if stats.Status != "member" && stats.Status != "administrator" && stats.Status != "creator" {
_, _ = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not a member of the channel.\nTap on Join Channel Button", ShowAlert: true})
return err
}

c, err := b.GetChat(chat.Id, nil)
if err != nil {
_, _ = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "Error getting chat info.", ShowAlert: true})
return logError(fmt.Sprintf("[unMuteMe]Error getting chat info: %s [chatId: %d]", err, chat.Id), err)
}

_, err = b.RestrictChatMember(chat.Id, user.Id, *c.Permissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: true})
if err != nil {
_, _ = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "Error unMuting you.\nmaybe i am not admin with enough rights.", ShowAlert: true})
return logError(fmt.Sprintf("[unMuteMe]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
}

_ = db.RemoveMuted(chat.Id, user.Id)

_, _ = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are unMuted now.", ShowAlert: true})
_, _, _ = query.Message.EditText(b, "You are now unMuted and can participate in the chat again.", nil)

return ext.EndGroups
}
114 changes: 32 additions & 82 deletions FallenSub/modules/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package modules

import (
"fmt"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/db"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
Expand All @@ -20,54 +19,27 @@ var chatMutePermissions = gotgbot.ChatPermissions{
CanSendVoiceNotes: false,
}

// fSubWatcher checks if the user is a member of the channel and restricts them if not.
func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {
chat := ctx.EffectiveChat
msg := ctx.EffectiveMessage

func isUserExempt(ctx *ext.Context, fSub db.FSub) bool {
user := ctx.EffectiveUser
fSub, _ := db.GetFSubSetting(chat.Id)

if !fSub.ForceSub {
return ext.EndGroups
}

if fSub.ForceSubChannel == 0 || user.IsBot || user.Id == 777000 || user.Id == 1087968824 || ctx.EffectiveSender.IsAnonymousAdmin() {
return ext.EndGroups
}

if isAdmin(ctx.EffectiveChat, ctx.EffectiveUser, b) {
return ext.EndGroups
}

member, err := b.GetChatMember(fSub.ForceSubChannel, user.Id, nil)
if err != nil {
_ = db.SetFSub(chat.Id, false)
text := "Force Sub disabled because I can't get your chat member status. Please add me as an admin."
_, _ = b.SendMessage(chat.Id, text, nil)
return logError(fmt.Sprintf("[fSubWatcher]Error getting chat member: %s [chatId: %d]", err, fSub.ForceSubChannel), err)
}
return fSub.ForceSubChannel == 0 || user.IsBot || user.Id == 777000 || user.Id == 1087968824 || ctx.EffectiveSender.IsAnonymousAdmin()
}

if member.GetStatus() == "member" || member.GetStatus() == "administrator" || member.GetStatus() == "creator" {
return ext.EndGroups
}
func handleGetChatMemberError(b *gotgbot.Bot, chat *gotgbot.Chat, fSub db.FSub, err error) error {
_ = db.SetFSub(chat.Id, false)
text := "Force Sub disabled because I can't get your chat member status. Please add me as an admin."
_, _ = b.SendMessage(chat.Id, text, nil)
return logError(fmt.Sprintf("[fSubWatcher]Error getting chat member: %s [chatId: %d]", err, fSub.ForceSubChannel), err)
}

_, err = b.RestrictChatMember(chat.Id, user.Id, chatMutePermissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: false})
func restrictUser(b *gotgbot.Bot, chat *gotgbot.Chat, user *gotgbot.User) error {
_, err := b.RestrictChatMember(chat.Id, user.Id, chatMutePermissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: false})
if err != nil {
return logError(fmt.Sprintf("[fSubWatcher]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
}
return db.UpdateMuted(chat.Id, user.Id)
}

err = db.UpdateMuted(chat.Id, user.Id)
if err != nil {
return logError(fmt.Sprintf("[fSubWatcher]Error updating muted user: %s [chatId: %d]", err, chat.Id), err)
}

channel, err := b.GetChat(fSub.ForceSubChannel, nil)
if err != nil {
return logError(fmt.Sprintf("[fSubWatcher]Error getting channel info: %s [chatId: %d]", err, chat.Id), err)
}

inviteLink := channel.InviteLink
func sendJoinChannelMessage(b *gotgbot.Bot, msg *gotgbot.Message, chat *gotgbot.Chat, user *gotgbot.User, inviteLink string) error {
text := "You must join the channel to continue using this group."
button := gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{{
Expand All @@ -81,65 +53,43 @@ func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {
},
}},
}
_, err = msg.Reply(b, text, &gotgbot.SendMessageOpts{ReplyMarkup: button, ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true}})
_, err := msg.Reply(b, text, &gotgbot.SendMessageOpts{ReplyMarkup: button, ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true}})
if err != nil {
return logError(fmt.Sprintf("[fSubWatcher]Error replying to message: %s [chatId: %d]", err, chat.Id), err)
}

return ext.EndGroups
return nil
}

// unMuteMe unMutes the user if they have joined the channel.
func unMuteMe(b *gotgbot.Bot, ctx *ext.Context) error {
query := ctx.Update.CallbackQuery
user := ctx.EffectiveUser
func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {
chat := ctx.EffectiveChat
msg := ctx.EffectiveMessage
user := ctx.EffectiveUser
fSub, _ := db.GetFSubSetting(chat.Id)

isMuted, err := db.IsMuted(chat.Id, user.Id)
if !isMuted {
_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not muted by me.", ShowAlert: true})
return err
}

fSub, err := db.GetFSubSetting(chat.Id)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error getting fSub setting: %s [chatId: %d]", err, chat.Id), err)
if !fSub.ForceSub || isUserExempt(ctx, *fSub) || isAdmin(ctx.EffectiveChat, ctx.EffectiveUser, b) {
return ext.EndGroups
}

member, err := b.GetChatMember(fSub.ForceSubChannel, user.Id, nil)
if err != nil {
return err
return handleGetChatMemberError(b, chat, *fSub, err)
}

stats := member.MergeChatMember()
config.InfoLog.Printf("status: %s", stats.Status)

if stats.Status != "member" && stats.Status != "administrator" && stats.Status != "creator" {
_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not a member of the channel.\nTap on Join Channel Button", ShowAlert: true})
return err
}

c, err := b.GetChat(chat.Id, nil)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error getting chat info: %s [chatId: %d]", err, chat.Id), err)
}

_, err = b.RestrictChatMember(chat.Id, user.Id, *c.Permissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: true})
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
if member.GetStatus() == "member" || member.GetStatus() == "administrator" || member.GetStatus() == "creator" {
return ext.EndGroups
}

err = db.RemoveMuted(chat.Id, user.Id)
err = restrictUser(b, chat, user)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error removing muted user: %s [chatId: %d]", err, chat.Id), err)
return logError(fmt.Sprintf("[fSubWatcher]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
}

_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are unMuted now.", ShowAlert: true})
channel, err := b.GetChat(fSub.ForceSubChannel, nil)
if err != nil {
return logError(fmt.Sprintf("[unMuteMe]Error answering callback: %s [chatId: %d]", err, chat.Id), err)
text := fmt.Sprintf("Something went wrong. Looks like I am not admin In Your Fsub Channel: %d\nDo <code>/fsub off</code>\nError: %s", fSub.ForceSubChannel, err)
_, _ = b.SendMessage(chat.Id, text, nil)
return logError(fmt.Sprintf("[fSubWatcher]Error getting channel info: %s [chatId: %d]", err, chat.Id), err)
}

_, _, _ = query.Message.EditText(b, "You are now unMuted and can participate in the chat again.", nil)

return ext.EndGroups
return sendJoinChannelMessage(b, msg, chat, user, channel.InviteLink)
}
2 changes: 1 addition & 1 deletion api/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Bot(w http.ResponseWriter, r *http.Request) {

body, err := io.ReadAll(r.Body)
if err != nil {
_, err = fmt.Fprintf(w, "Error reading request body: %v", err)
_, _ = fmt.Fprintf(w, "Error reading request body: %v", err)
w.WriteHeader(statusCodeSuccess)
return
}
Expand Down

0 comments on commit 1ad3d5f

Please sign in to comment.