-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
170 lines (148 loc) · 4.31 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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/kisielk/sqlstruct"
"github.com/nelsonleduc/calmanbot/config"
"github.com/bwmarrin/discordgo"
"github.com/nelsonleduc/calmanbot/cache"
"github.com/nelsonleduc/calmanbot/handlers"
"github.com/nelsonleduc/calmanbot/handlers/models"
"github.com/nelsonleduc/calmanbot/service/discord"
)
// Variables used for command line parameters
var (
token string
discordService discord.DSService
)
type statusTuple struct {
gameType discordgo.ActivityType
status string
}
type dbStatus struct {
Text string `sql:"text"`
Type int `sql:"type"`
}
func queryDBStatus() []statusTuple {
queryStr := fmt.Sprintf("SELECT %s FROM discord_status", sqlstruct.Columns(dbStatus{}))
rows, err := config.DB().Query(queryStr)
if err != nil {
return []statusTuple{}
}
defer rows.Close()
if config.Configuration().SuperVerboseMode() {
fmt.Println("Loaded status list:")
}
groupedPosts := []statusTuple{}
for rows.Next() {
var status dbStatus
err := sqlstruct.Scan(&status, rows)
if err == nil {
var statusType discordgo.ActivityType
switch status.Type {
case 0:
statusType = discordgo.ActivityTypeGame
case 1:
statusType = discordgo.ActivityTypeListening
case 2:
statusType = discordgo.ActivityTypeWatching
default:
continue
}
convertedStatus := statusTuple{statusType, status.Text}
groupedPosts = append(groupedPosts, convertedStatus)
if config.Configuration().SuperVerboseMode() {
fmt.Printf(" %+v\n", convertedStatus)
}
}
}
return groupedPosts
}
func init() {
token = os.Getenv("discord_token")
}
func randomStatus(excluding statusTuple) statusTuple {
choice := excluding
statusOptions := queryDBStatus()
if len(statusOptions) == 0 {
return statusTuple{discordgo.ActivityTypeWatching, "for questions"}
}
for choice == excluding {
idx := rand.Intn(len(statusOptions))
choice = statusOptions[idx]
}
return choice
}
func postStatus(s *discordgo.Session, statusTuple statusTuple) {
log.Printf("[Tick] Setting status \"%+v\"\n", statusTuple)
err := s.UpdateStatusComplex(discordgo.UpdateStatusData{
IdleSince: nil,
Activities: []*discordgo.Activity{
{
Name: statusTuple.status,
Type: statusTuple.gameType,
URL: "",
CreatedAt: time.Now(),
ApplicationID: "",
State: "",
Details: "",
Timestamps: discordgo.TimeStamps{},
Emoji: discordgo.Emoji{},
Party: discordgo.Party{},
Assets: discordgo.Assets{},
Secrets: discordgo.Secrets{},
Instance: false,
Flags: 0,
},
},
AFK: false,
Status: "online",
})
if config.Configuration().VerboseMode() && err != nil {
log.Fatalln("failed to update status: ", err)
}
}
func CreateWebhook() {
log.Println("Creating discord webhook")
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatalln("error creating Discord session,", err)
return
}
discordService = discord.NewDSService(dg)
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Fatalln("error opening connection,", err)
return
}
status := statusTuple{}
for ; true; <-time.Tick(30 * time.Minute) {
status = randomStatus(status)
postStatus(dg, status)
}
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
message := discordService.MessageFromSessionAndMessage(s, m.Message)
monitor, _ := discordService.ServiceMonitor()
cache := cache.NewSmartCache(monitor)
if config.Configuration().SuperVerboseMode() {
fmt.Printf("\n[MessageCreate fired] dssession: %+v\n", s)
fmt.Printf("[MessageCreate fired] dsmessage: %+v\n", *m)
fmt.Printf("[MessageCreate fired] message: %+v\n\n", message)
}
handlers.HandleCalman(message, discordService, cache, models.PostGresRepo())
}