This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
bot.go
133 lines (108 loc) · 3.25 KB
/
bot.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
package bot
import (
"fmt"
"github.com/7cav/discord-sync/bot/extensions"
"github.com/7cav/discord-sync/cavDiscord"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"os/signal"
)
type Bot struct {
conn *discordgo.Session
}
func New(token string) (Bot, error) {
conn, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Could not start discord bot: %v", err)
}
return Bot{
conn: conn,
}, nil
}
func (b Bot) SpecialClearOldUsers(guildId string) {
conn := b.conn
conn.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Debug("Bot is up!")
})
err := conn.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
members, err := conn.GuildMembers(guildId, "", 0)
if err != nil {
log.Printf("error getting all guild members: %v", err)
return
}
removeRole := func(member *discordgo.Member, roleToRemove string) {
for _, role := range member.Roles {
if role == roleToRemove {
break
}
return
}
err := conn.GuildMemberRoleRemove(guildId, member.User.ID, roleToRemove)
if err != nil {
log.Printf("error removing role: %v, from user: %s, err: %v", roleToRemove, member.Nick, err)
return
}
return
}
for _, member := range members {
removeRole(member, string(cavDiscord.Discord7CavActive))
removeRole(member, string(cavDiscord.Discord7CavDisch))
removeRole(member, string(cavDiscord.Discord7CavRet))
removeRole(member, string(cavDiscord.Discord7CavELOA))
}
}
func (b Bot) Start(appId string, guildId string) {
conn := b.conn
conn.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Debugf("Bot is up!")
})
err := conn.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
commands := []*discordgo.ApplicationCommand{
MilpacCommand(),
HelloCommand(),
SyncCommand(),
}
handlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
MilpacCommandName: HandleMilpac,
HelloCommandName: HandleHello,
SyncCommandName: HandleSync,
}
_, err = conn.ApplicationCommandBulkOverwrite(appId, guildId, commands)
if err != nil {
log.Fatalf("Cannot create commands: %v", err)
}
conn.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if handler, found := handlers[i.ApplicationCommandData().Name]; found {
handler(s, i)
}
})
extensions.BootstrapCavDiscordCache(conn)
defer conn.Close()
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Debugf("Gracefully shutting down")
}
func AskToConnectDiscord(s *discordgo.Session, i *discordgo.InteractionCreate) {
mention := fmt.Sprintf("<@%s>", i.Member.User.ID)
kcUrl := fmt.Sprintf("%s/auth/realms/%s/account/identity", viper.GetString("keycloak.host"), viper.GetString("keycloak.realm"))
reply := fmt.Sprintf(`Hey %s, we don't have a valid discord connection for you. Please go to %s and click 'add' to connect your discord account. Then try again!`, mention, kcUrl)
// this was called because no KC user was found for the given discord ID
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: reply,
},
})
if err != nil {
return
}
}