-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent.go
More file actions
226 lines (197 loc) · 6.84 KB
/
event.go
File metadata and controls
226 lines (197 loc) · 6.84 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package helpbot
import (
"errors"
"fmt"
"github.com/Raytar/helpbot/database"
"github.com/Raytar/helpbot/models"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func (bot *HelpBot) initEvents() {
// create a handler and bind it to new message events
bot.client.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
// middleware
user := getMember(s, i)
if i.Member == nil || i.GuildID == "" {
sendMsg(s, i.User, "This bot only works in a server.")
return
}
bot.log.Infof("Received interaction: %+v from user: %s", i, user.User.Username)
if i.Type != discordgo.InteractionApplicationCommand {
return
}
// ignore bot messages
if i.Member.User.Bot {
return
}
bot.discordMessageCreate(s, i)
})
bot.client.AddHandler(bot.discordServerJoin)
bot.client.AddHandler(bot.discordServerUpdate)
}
func (bot *HelpBot) discordServerUpdate(s *discordgo.Session, e *discordgo.GuildUpdate) {
bot.log.Infof("Server updated: %s", e.Name)
}
var (
RoleStudent = "Student"
RoleAssistant = "Teaching Assistant"
Hoist = true
)
func (bot *HelpBot) discordServerJoin(s *discordgo.Session, e *discordgo.GuildCreate) {
bot.log.Infof("Joined server: %s, id: %s, channel: %s", e.Name, e.ID, e.SystemChannelID)
// Check if the server has been registered with a course
// If not, send a message to the server owner to let them know
// that the server needs to be registered with a course
// and that the bot will not work until the server is registered
_, err := bot.db.GetCourse(&models.Course{GuildID: e.ID})
if errors.Is(err, gorm.ErrRecordNotFound) {
bot.log.Errorf("Failed to get course: %s", err)
//bot.client.ChannelMessageSend(e.SystemChannelID, "This server has not been configured with a course. Please contact the server owner to configure this server for a course.")
choices := courseChoices(bot.db)
if len(choices) == 0 {
//bot.client.ChannelMessageSend(e.SystemChannelID, "There are no courses available to configure this server with. Please contact the server owner to add a course.")
return
}
// TODO: Add a command to configure the server with a course
if _, err := bot.client.ApplicationCommandCreate(bot.cfg.AppID, e.ID, &discordgo.ApplicationCommand{
Name: "configure",
Description: "Configure this server with a course",
DefaultMemberPermissions: &permAdmin,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "course",
Description: "The course to configure this server with",
Choices: choices,
Required: true,
},
},
}); err != nil {
bot.log.Errorf("Failed to create command: %s", err)
}
return
} else if err != nil {
bot.log.Errorf("Failed to get course: %s", err)
// TODO: Send a message to the server owner to let them know that the bot failed to get the course
return
}
// Create roles and commands for the server
_ = bot.initServer(s, e.ID)
// Announce that the bot is online and ready to help
// TODO: Might be best to send this to the server owner
// bot.client.ChannelMessageSend(e.SystemChannelID, "HelpBot is online! :robot:")
}
func (bot *HelpBot) discordMessageCreate(s *discordgo.Session, m *discordgo.InteractionCreate) {
command := m.ApplicationCommandData().Name
gm := getMember(s, m)
if gm == nil {
bot.log.Infoln("messageCreate: Failed to get guild member:")
return
}
if cmdFunc, ok := bot.commands[command]; ok {
cmdFunc(m)
return
}
replyMsg(bot.client, m, fmt.Sprintf("'%s' is not a recognized command. See /help for available commands.",
command))
}
func getMember(s *discordgo.Session, i *discordgo.InteractionCreate) *discordgo.Member {
if i.Member != nil {
return i.Member
}
return nil
}
// initServer creates the roles and commands for a server. Roles are created if they do not already exist.
// Commands are created if they do not already exist. If a command already exists, it will be updated.
func (bot *HelpBot) initServer(s *discordgo.Session, guildID string) error {
course, err := bot.db.GetCourse(&models.Course{GuildID: guildID})
if err != nil {
return err
}
commands := GetCommands(course)
// Register slash commands. If a command already exists, it will be updated.
for _, cmd := range commands {
log.Info("Registering command: ", cmd.Name, " in server with id: ", guildID)
_, err := bot.client.ApplicationCommandCreate(bot.cfg.AppID, guildID, cmd)
if err != nil {
log.Errorln("Failed to create global command:", cmd.Name, err)
}
}
// Get all roles in the server.
roles, err := bot.client.GuildRoles(guildID)
if err != nil {
log.Errorln("Failed to get roles:", err)
}
// Create a map of role name to role ID.
roleMap := make(map[string]string)
for _, role := range roles {
if role.Name == RoleStudent || role.Name == RoleAssistant {
// Student or Teaching Assistant role already exists.
log.Info("Role already exists: ", role.Name, " with id: ", role.ID)
roleMap[role.Name] = role.ID
// Skip creating the role.
}
}
// Create roles that don't exist.
for _, roleName := range []string{RoleStudent, RoleAssistant} {
if _, ok := roleMap[roleName]; ok {
// Role already exists.
continue
}
permission := &NoPermission
if roleName == RoleStudent {
permission = &permStudent
} else if roleName == RoleAssistant {
permission = &permAssistant
}
log.Info("Creating role: ", roleName, " in server with id: ", guildID)
role, err := bot.client.GuildRoleCreate(guildID, &discordgo.RoleParams{
Name: roleName,
Hoist: &Hoist,
Permissions: permission,
})
if err != nil {
log.Errorln("Failed to create role:", err)
}
roleMap[roleName] = role.ID
}
bot.roles[guildID] = roleMap
return nil
}
func (bot *HelpBot) createChannel(s *discordgo.Session, guildID, name string, roles ...string) error {
channel, err := s.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: "test",
Type: discordgo.ChannelTypeGuildText,
PermissionOverwrites: []*discordgo.PermissionOverwrite{
{
ID: bot.GetRole(guildID, RoleStudent),
Type: discordgo.PermissionOverwriteTypeRole,
},
{
ID: guildID, // Everyone
Type: discordgo.PermissionOverwriteTypeRole,
Deny: discordgo.PermissionViewChannel,
},
},
})
if err != nil {
bot.log.Errorln("Failed to create channel:", err)
return err
}
bot.log.Infof("Created channel: %s", channel.Name)
return nil
}
func courseChoices(db *database.Database) (choices []*discordgo.ApplicationCommandOptionChoice) {
courses, err := db.GetCourses()
if err != nil {
log.Errorln("Failed to get courses:", err)
}
for _, course := range courses {
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: fmt.Sprintf("%s %d", course.Name, course.Year),
Value: fmt.Sprintf("%d", course.CourseID),
})
}
return choices
}