Skip to content

Commit 633a084

Browse files
feat(bot): add support for ALLOWED_USERS
1 parent 42038de commit 633a084

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ In addition to the mandatory variables, you can also set the following optional
202202

203203
- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`)
204204

205-
- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`.(default: `null`)
205+
- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`. (default: `null`)
206+
207+
- `ALLOWED_USERS` : A list of user IDs separated by comma (`,`). If this is set, only the users in this list will be able to use the bot. (default: `null`)
206208

207209
<hr>
208210

config/config.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,36 @@ import (
2020

2121
var ValueOf = &config{}
2222

23+
type allowedUsers []int64
24+
25+
func (au *allowedUsers) Decode(value string) error {
26+
if value == "" {
27+
return nil
28+
}
29+
ids := strings.Split(string(value), ",")
30+
for _, id := range ids {
31+
idInt, err := strconv.ParseInt(id, 10, 64)
32+
if err != nil {
33+
return err
34+
}
35+
*au = append(*au, idInt)
36+
}
37+
return nil
38+
}
39+
2340
type config struct {
24-
ApiID int32 `envconfig:"API_ID" required:"true"`
25-
ApiHash string `envconfig:"API_HASH" required:"true"`
26-
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
27-
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
28-
Dev bool `envconfig:"DEV" default:"false"`
29-
Port int `envconfig:"PORT" default:"8080"`
30-
Host string `envconfig:"HOST" default:""`
31-
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
32-
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
33-
UserSession string `envconfig:"USER_SESSION"`
34-
UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"true"`
41+
ApiID int32 `envconfig:"API_ID" required:"true"`
42+
ApiHash string `envconfig:"API_HASH" required:"true"`
43+
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
44+
LogChannelID int64 `envconfig:"LOG_CHANNEL" required:"true"`
45+
Dev bool `envconfig:"DEV" default:"false"`
46+
Port int `envconfig:"PORT" default:"8080"`
47+
Host string `envconfig:"HOST" default:""`
48+
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
49+
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
50+
UserSession string `envconfig:"USER_SESSION"`
51+
UsePublicIP bool `envconfig:"USE_PUBLIC_IP" default:"true"`
52+
AllowedUsers allowedUsers `envconfig:"ALLOWED_USERS"`
3553
MultiTokens []string
3654
}
3755

internal/commands/start.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package commands
22

33
import (
4+
"EverythingSuckz/fsb/config"
5+
"EverythingSuckz/fsb/internal/utils"
6+
47
"github.com/celestix/gotgproto/dispatcher"
58
"github.com/celestix/gotgproto/dispatcher/handlers"
69
"github.com/celestix/gotgproto/ext"
@@ -19,6 +22,10 @@ func start(ctx *ext.Context, u *ext.Update) error {
1922
if peerChatId.Type != int(storage.TypeUser) {
2023
return dispatcher.EndGroups
2124
}
25+
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
26+
ctx.Reply(u, "You are not allowed to use this bot.", nil)
27+
return dispatcher.EndGroups
28+
}
2229
ctx.Reply(u, "Hi, send me any file to get a direct streamble link to that file.", nil)
2330
return dispatcher.EndGroups
2431
}

internal/commands/stream.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
4646
if peerChatId.Type != int(storage.TypeUser) {
4747
return dispatcher.EndGroups
4848
}
49+
if len(config.ValueOf.AllowedUsers) != 0 && !utils.Contains(config.ValueOf.AllowedUsers, chatId) {
50+
ctx.Reply(u, "You are not allowed to use this bot.", nil)
51+
return dispatcher.EndGroups
52+
}
4953
supported, err := supportedMediaFilter(u.EffectiveMessage)
5054
if err != nil {
5155
return err

internal/utils/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import (
1616
"go.uber.org/zap"
1717
)
1818

19+
// https://stackoverflow.com/a/70802740/15807350
20+
func Contains[T comparable](s []T, e T) bool {
21+
for _, v := range s {
22+
if v == e {
23+
return true
24+
}
25+
}
26+
return false
27+
}
28+
1929
func GetTGMessage(ctx context.Context, client *gotgproto.Client, messageID int) (*tg.Message, error) {
2030
inputMessageID := tg.InputMessageClass(&tg.InputMessageID{ID: messageID})
2131
channel, err := GetLogChannelPeer(ctx, client.API(), client.PeerStorage)

0 commit comments

Comments
 (0)