-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
207 lines (185 loc) · 5.13 KB
/
discord.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"database/sql"
"fmt"
"log"
"net/url"
"github.com/bwmarrin/discordgo"
)
type DiscordBot struct {
Token string
GuildId string
AdminRoleId string
StudentRoleId string
ClientId string
ClientSecret string
Db *sql.DB
Session *discordgo.Session
}
var (
commands = []*discordgo.ApplicationCommand{
// All commands and options must have a description
// Commands/options without description will fail the registration
// of the command.
{
Name: "about",
Description: "Lookup information about a discord user who has linked their OSU account",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionUser,
Name: "user",
Description: "User",
Required: true,
},
},
},
}
commandHandlers = map[string]func(b *DiscordBot, i *discordgo.InteractionCreate){
"about": func(b *DiscordBot, i *discordgo.InteractionCreate) {
if !b.requireAdmin(i) {
return
}
options := i.ApplicationCommandData().Options
user := options[0].UserValue(nil)
discordId := user.ID
row := b.Db.QueryRow(`SELECT name_num, display_name, last_seen_timestamp, student, alum, employee, faculty FROM users WHERE discord_id = ?`, discordId)
var (
nameNum string
displayName string
lastSeen int
student bool
alum bool
employee bool
faculty bool
)
err := row.Scan(&nameNum, &displayName, &lastSeen, &student, &alum, &employee, &faculty)
if err != nil {
log.Println("/about command: discordId =", discordId, err)
_ = b.Session.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "User has not linked their OSU account",
},
})
return
}
content := fmt.Sprintf("**[%s (%s)](<https://www.osu.edu/search/?query=%s>)**\nLast seen: <t:%d:f>\n",
displayName,
nameNum,
url.QueryEscape(nameNum),
lastSeen,
)
sep := ""
if student {
content += sep + "Student"
sep = ", "
}
if alum {
content += sep + "Alum"
sep = ", "
}
if employee {
content += sep + "Employee"
sep = ", "
}
if faculty {
content += sep + "Faculty"
}
_ = b.Session.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
},
})
},
}
)
func (b *DiscordBot) isAdmin(m *discordgo.Member) bool {
for _, role := range m.Roles {
if role == b.AdminRoleId {
return true
}
}
return false
}
func (b *DiscordBot) requireAdmin(i *discordgo.InteractionCreate) bool {
if !b.isAdmin(i.Member) {
_ = b.Session.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "This command requires admin",
},
})
return false
}
return true
}
func (b *DiscordBot) Connect() {
if b.Token == "" {
log.Println("Failed to connect to discord: Missing token")
return
}
if b.AdminRoleId == "" {
log.Println("Failed to connect to discord: Missing admin role id")
return
}
if b.GuildId == "" {
log.Println("Failed to connect to discord: Missing guild id")
return
}
if b.ClientId == "" {
log.Println("Failed to connect to discord: Missing client id")
return
}
if b.ClientSecret == "" {
log.Println("Failed to connect to discord: Missing client secret")
return
}
s, err := discordgo.New("Bot " + b.Token)
if err != nil {
log.Println("Failed to connect to discord:", err)
return
}
b.Session = s
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Logged in as", r.User.String())
})
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(b, i)
}
})
err = s.Open()
if err != nil {
log.Fatalln("Failed to open session", err)
}
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, b.GuildId, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
}
func (b *DiscordBot) GiveStudentRole(discordId string) error {
if b.Session == nil {
return fmt.Errorf("discord bot not connected")
}
return b.Session.GuildMemberRoleAdd(b.GuildId, discordId, b.StudentRoleId)
}
func (b *DiscordBot) AddStudentToGuild(discordId string, accessToken string) error {
if b.Session == nil {
return fmt.Errorf("discord bot not connected")
}
return b.Session.GuildMemberAdd(b.GuildId, discordId, &discordgo.GuildMemberAddParams{
AccessToken: accessToken,
Roles: []string{b.StudentRoleId},
})
}
func (b *DiscordBot) RemoveStudentRole(discordId string) error {
if b.Session == nil {
return fmt.Errorf("discord bot not connected")
}
return b.Session.GuildMemberRoleRemove(b.GuildId, discordId, b.StudentRoleId)
}