Skip to content

Commit 845da8d

Browse files
committed
Add support for a file with guild bans
1 parent 672d8f2 commit 845da8d

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ type Config struct {
7676
DonorBacklogLimit int `yaml:"backlog_limit_donor"`
7777
}
7878

79+
type BansFile struct {
80+
Guilds []string `yaml:"guilds"`
81+
}
82+
7983
type ManagedChannelMarshal struct {
8084
ID string `yaml:"id"`
8185
GuildID string `yaml:"guild_id"`

oauth.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,27 @@ func (b *Bot) HTTPOAuthCallback(w http.ResponseWriter, r *http.Request) {
5757
return
5858
} else if err != nil {
5959
fmt.Printf("%T %v", err, err)
60-
http.Error(w, "bad token", http.StatusUnprocessableEntity)
60+
http.Error(w, "An error occured and the bot could not join the server.", http.StatusUnprocessableEntity)
6161
return
6262
}
63+
if guildInfo, ok := t.Extra("guild").(map[string]interface{}); ok {
64+
if guildID, ok := guildInfo["id"].(string); ok {
65+
if banned, err := b.storage.IsBanned(guildID); banned {
66+
b.s.GuildLeave(guildID)
67+
http.Error(w, "AutoDelete is not available on this server.", http.StatusForbidden)
68+
fmt.Printf("[INFO] join attempt for banned server %s\n", guildID)
69+
return
70+
} else if err != nil {
71+
fmt.Printf("[ERR] Could not check banlist: %T %v", err, err)
72+
http.Error(w, "An error occured and the bot may not have joined the server.", http.StatusUnprocessableEntity)
73+
return
74+
}
75+
} else {
76+
fmt.Printf("[ERR] Unexpected type for guild.id: got %T\n", guildInfo["id"])
77+
}
78+
} else {
79+
fmt.Printf("[ERR] Unexpected type for guild: got %T\n", t.Extra("guild"))
80+
}
6381

6482
fmt.Println(t)
6583
w.WriteHeader(200)

storage.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type Storage interface {
1717
GetChannel(id string) (ManagedChannelMarshal, error)
1818
SaveChannel(conf ManagedChannelMarshal) error
1919
DeleteChannel(id string) error
20+
21+
IsBanned(guildID string) (bool, error)
22+
AddBan(guildID string) (error)
2023
}
2124

2225
/******************
@@ -29,6 +32,7 @@ type DiskStorage struct {
2932

3033
const pathChannelConfDir = "./data"
3134
const pathChannelConfig = "./data/%s.yml"
35+
const pathBanList = "./data/bans.yml"
3236

3337
func (s *DiskStorage) ListChannels() ([]string, error) {
3438
files, err := ioutil.ReadDir(pathChannelConfDir)
@@ -41,6 +45,9 @@ func (s *DiskStorage) ListChannels() ([]string, error) {
4145
if !strings.HasSuffix(n, ".yml") {
4246
continue
4347
}
48+
if strings.HasPrefix(n, "bans.yml") {
49+
continue
50+
}
4451
chID := strings.TrimSuffix(n, ".yml")
4552
channelIDs = append(channelIDs, chID)
4653
}
@@ -96,3 +103,29 @@ func (s *DiskStorage) DeleteChannel(id string) error {
96103
}
97104
return nil
98105
}
106+
107+
func (s *DiskStorage) IsBanned(guildID string) (bool, error) {
108+
by, err := ioutil.ReadFile(pathBanList)
109+
if os.IsNotExist(err) {
110+
return false, nil
111+
} else if err != nil {
112+
return false, err
113+
}
114+
115+
var conf BansFile
116+
err = yaml.Unmarshal(by, &conf)
117+
if err != nil {
118+
return false, err
119+
}
120+
121+
for _, v := range conf.Guilds {
122+
if v == guildID {
123+
return true, nil
124+
}
125+
}
126+
return false, nil
127+
}
128+
129+
func (s *DiskStorage) AddBan(guildID string) (error) {
130+
return fmt.Errorf("unimplemented!")
131+
}

0 commit comments

Comments
 (0)