This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
teams.go
129 lines (100 loc) · 2.48 KB
/
teams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package db
import (
"context"
"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/bson"
"sync"
"time"
)
var (
defaultLogo = "" // TODO: add this late
mu sync.RWMutex
TeamOptions []*discordgo.ApplicationCommandOptionChoice
)
func UpdateTeamOptions() error {
teams, err := GetAllTeams()
if err != nil {
return err
}
var wg sync.WaitGroup
var options []*discordgo.ApplicationCommandOptionChoice
for _, team := range teams {
wg.Add(1)
go func(t Team) {
defer wg.Done()
options = append(options, &discordgo.ApplicationCommandOptionChoice{
Name: t.Name,
Value: t.ID,
})
}(team)
}
wg.Wait()
mu.Lock()
TeamOptions = options
mu.Unlock()
return nil
}
func GetAllTeams() ([]Team, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c := client.Database("aafl").Collection("teams") // replace with your actual database name
cursor, err := c.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
var teams []Team
err = cursor.All(ctx, &teams)
if err != nil {
return nil, err
}
return teams, nil
}
func SaveTeamData(teamData *Team) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c := client.Database("aafl").Collection("teams") // replace with your actual database name
_, err := c.InsertOne(ctx, teamData)
if err != nil {
return err
}
err = UpdateTeamOptions()
if err != nil {
return err
}
return nil
}
func GetTeamData(id string) (Team, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c := client.Database("aafl").Collection("teams") // replace with your actual database name
var team Team
err := c.FindOne(ctx, bson.M{"RoleID": id}).Decode(&team)
if err != nil {
return Team{}, err
}
return team, nil
}
func AddTeamPlaceholder(team Team) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
c := client.Database("aafl").Collection("teams") // replace with your actual database name
_, err := c.InsertOne(ctx, team)
if err != nil {
return err
}
return nil
}
func HasManagePermission(userID string, team Team) bool {
// Check if the user is the team owner
if userID == team.Owner {
return true
}
// Check if the user is one of the coaches
for _, coach := range team.Coaches {
if userID == coach {
return true
}
}
// If the user is neither the owner nor a coach, they do not have permission
return false
}