-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord.go
143 lines (131 loc) · 4.03 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
package main
import (
"log"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
// Some Discord environment variables.
var discordToken string
var discordStatusChannel string
var discordUpdateChannel string
var currentStatus Status
var currentUpdate Update
// Current status of the website.
type Status struct {
Type string `json:"type"`
Text string `json:"text"`
}
// Latest update from the Discord 'updates' channel.
type Update struct {
ID string `json:"id"`
GuildID string `json:"guildId"`
ChannelID string `json:"channelId"`
CreatedTs int64 `json:"createdTimestamp"`
EditedTs int64 `json:"editedTimestamp"`
AuthorID string `json:"authorId"`
AuthorName string `json:"authorName"`
AuthorImage string `json:"authorImage"`
Content string `json:"content"`
CleanContent string `json:"cleanContent"`
Image string `json:"image"`
}
// Start the Discord bot responsible for the site status and 'What's new?' card.
func startDiscordBot() {
// create Discord session
dg, err := discordgo.New("Bot " + discordToken)
if err != nil {
log.Fatalf("failed to create Discord session: %s", err)
}
// set intents (all we want is guild messages)
dg.Identify.Intents = discordgo.IntentsGuildMessages
go func() {
for {
// get current status
statusMessages, err := dg.ChannelMessages(discordStatusChannel, 0, "", "", "")
if err != nil {
log.Fatalf("failed getting current status: %s", err)
}
for i := 0; i < len(statusMessages); i++ {
m := statusMessages[i]
if strings.HasPrefix(m.Content, "--status-set") {
currentStatus = Status{
Type: "warn",
Text: strings.TrimSpace(strings.Replace(m.Content, "--status-set", "", 1)),
}
break
} else if strings.HasPrefix(m.Content, "--status-remove") {
currentStatus = Status{
Type: "empty",
}
break
}
}
// get latest update
updateMessages, err := dg.ChannelMessages(discordUpdateChannel, 0, "", "", "")
if err != nil {
log.Fatalf("failed getting latest update: %s", err)
}
for i := 0; i < len(updateMessages); i++ {
m := updateMessages[i]
if len(m.Attachments) > 0 {
currentUpdate = Update{
ID: m.ID,
GuildID: m.GuildID,
ChannelID: m.ChannelID,
CreatedTs: m.Timestamp.UnixMilli(),
EditedTs: m.Timestamp.UnixMilli(),
AuthorID: m.Author.ID,
AuthorName: m.Author.Username,
AuthorImage: m.Author.AvatarURL(""),
Content: m.Content,
CleanContent: m.ContentWithMentionsReplaced(),
Image: m.Attachments[0].URL,
}
break
}
}
// pause for 30 mins
time.Sleep(time.Minute * 30)
}
}()
// messageCreate handler
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID == discordStatusChannel {
log.Printf("updating status by request of %s", m.Author.Username)
if strings.HasPrefix(m.Content, "--status-set") {
currentStatus = Status{
Type: "warn",
Text: strings.TrimSpace(strings.Replace(m.Content, "--status-set", "", 1)),
}
s.MessageReactionAdd(m.ChannelID, m.ID, "<:good:1118293837773807657>")
} else if strings.HasPrefix(m.Content, "--status-remove") {
currentStatus = Status{
Type: "empty",
}
s.MessageReactionAdd(m.ChannelID, m.ID, "<:good:1118293837773807657>")
}
} else if m.ChannelID == discordUpdateChannel && len(m.Attachments) > 0 {
log.Printf("updating latest update by request of %s", m.Author.Username)
currentUpdate = Update{
ID: m.ID,
GuildID: m.GuildID,
ChannelID: m.ChannelID,
CreatedTs: m.Timestamp.UnixMilli(),
EditedTs: m.Timestamp.UnixMilli(),
AuthorID: m.Author.ID,
AuthorName: m.Author.Username,
AuthorImage: m.Author.AvatarURL(""),
Content: m.Content,
CleanContent: m.ContentWithMentionsReplaced(),
Image: m.Attachments[0].URL,
}
}
})
// connect to Discord
err = dg.Open()
if err != nil {
log.Fatalf("failed to open Discord connection: %s", err)
}
defer dg.Close()
}