forked from Southclaws/cj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_discord.go
166 lines (143 loc) · 4.35 KB
/
bot_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
package main
import (
"time"
"go.uber.org/zap"
"github.com/bwmarrin/discordgo"
)
// ChannelDM is a direct message channel
type ChannelDM struct {
ChannelID string `json:"id"`
Private bool `json:"is_private"`
Recipient discordgo.User `json:"recipient"`
LastMessageID string `json:"last_message_id"`
}
// ConnectDiscord sets up the Discord API and event listeners
func (app *App) ConnectDiscord() {
var err error
app.discordClient, err = discordgo.New("Bot " + app.config.DiscordToken)
if err != nil {
logger.Fatal("failed to connect to Discord API",
zap.Error(err))
}
app.discordClient.AddHandler(app.onReady)
app.discordClient.AddHandler(app.onMessage)
app.discordClient.AddHandler(app.onJoin)
err = app.discordClient.Open()
if err != nil {
logger.Fatal("failed to start Discord client",
zap.Error(err))
}
}
// nolint:gocyclo
func (app *App) onReady(s *discordgo.Session, event *discordgo.Ready) {
found := 0
roles, err := s.GuildRoles(app.config.GuildID)
if err != nil {
logger.Fatal("failed to get guild roles",
zap.Error(err))
}
for _, role := range roles {
if role.ID == app.config.VerifiedRole || role.ID == app.config.NormalRole {
found++
}
}
if found != 2 {
logger.Fatal("role not found.",
zap.String("role", app.config.VerifiedRole))
}
var member bool
users, err := s.GuildMembers(app.config.GuildID, "", 1000)
if err != nil {
logger.Fatal("failed to get guild members",
zap.Error(err))
}
for _, user := range users {
member = false
for i := range user.Roles {
if user.Roles[i] == app.config.NormalRole {
member = true
}
}
if !member {
err = app.discordClient.GuildMemberRoleAdd(app.config.GuildID, user.User.ID, app.config.NormalRole)
if err != nil {
logger.Fatal("failed to add role to guild member",
zap.Error(err))
}
}
}
ticker := time.NewTicker(time.Minute * time.Duration(app.config.Heartbeat))
for t := range ticker.C {
app.onHeartbeat(t)
}
app.ready <- true
}
// nolint:gocyclo
func (app *App) onMessage(s *discordgo.Session, event *discordgo.MessageCreate) {
if len(app.ready) > 0 {
<-app.ready
}
if event.Message.Author.ID == app.config.BotID {
return
}
if app.config.DebugUser != "" {
if event.Message.Author.ID != app.config.DebugUser {
return
}
}
_, source, errors := app.commandManager.Process(*event.Message)
for _, err := range errors {
if err != nil {
err = app.WarnUserError(event.Message.ChannelID, err.Error())
if err != nil {
logger.Warn("failed to warn user of error", zap.Error(err))
}
}
}
if source != CommandSourcePRIVATE && source != CommandSourceADMINISTRATIVE {
err := app.RecordChatLog(event.Message.Author.ID, event.Message.ChannelID, event.Message.Content)
if err != nil {
logger.Warn("failed to record chat log", zap.Error(err))
}
for i := range event.Message.Mentions {
if event.Message.Mentions[i].ID == app.config.BotID {
err := app.HandleSummon(*event.Message)
if err != nil {
logger.Warn("failed to handle summon", zap.Error(err))
}
}
}
}
}
func (app *App) onJoin(s *discordgo.Session, event *discordgo.GuildMemberAdd) {
verified, err := app.IsUserVerified(event.Member.User.ID)
if err != nil {
logger.Warn("failed to check if user verified", zap.Error(err))
}
err = app.discordClient.GuildMemberRoleAdd(app.config.GuildID, event.Member.User.ID, app.config.NormalRole)
if err != nil {
logger.Warn("failed to add normal role to member", zap.Error(err))
}
if verified {
err = app.discordClient.GuildMemberRoleAdd(app.config.GuildID, event.Member.User.ID, app.config.VerifiedRole)
if err != nil {
logger.Warn("failed to add verified role to member", zap.Error(err))
}
} else {
ch, err := s.UserChannelCreate(event.Member.User.ID)
if err != nil {
logger.Warn("failed to create user channel", zap.Error(err))
return
}
_, err = app.discordClient.ChannelMessageSend(ch.ID, app.locale.GetLangString("en", "AskUserVerify"))
if err != nil {
logger.Warn("failed to send message", zap.Error(err))
}
}
}
func (app *App) onHeartbeat(t time.Time) {
err := app.HandleHeartbeatEvent(t)
if err != nil {
logger.Warn("failed to handle heartbeat", zap.Error(err))
}
}