-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiscord_notifier_commands.go
More file actions
113 lines (103 loc) · 3.13 KB
/
discord_notifier_commands.go
File metadata and controls
113 lines (103 loc) · 3.13 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
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func (n *discordNotifier) registerCommands() error {
if n == nil || n.dg == nil {
return nil
}
appID := ""
if n.dg.State != nil && n.dg.State.User != nil {
appID = n.dg.State.User.ID
}
if appID == "" || n.guildID == "" {
return fmt.Errorf("missing appID or guildID")
}
cmds := []*discordgo.ApplicationCommand{
{
Name: "notify",
Description: "Enable goPool notifications using a one-time code",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "code",
Description: "One-time code from goPool",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
},
},
{
Name: "notify-stop",
Description: "Disable goPool notifications",
},
}
_, err := n.dg.ApplicationCommandBulkOverwrite(appID, n.guildID, cmds)
return err
}
func (n *discordNotifier) handleCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
if n == nil || n.s == nil || s == nil || i == nil {
return
}
if strings.TrimSpace(i.GuildID) != "" && n.guildID != "" && i.GuildID != n.guildID {
return
}
if i.Member == nil || i.Member.User == nil {
return
}
name := i.ApplicationCommandData().Name
switch name {
case "notify":
code := ""
for _, opt := range i.ApplicationCommandData().Options {
if opt.Type == discordgo.ApplicationCommandOptionString && opt.Name == "code" {
code = strings.TrimSpace(opt.StringValue())
}
}
if code == "" {
_ = respondEphemeral(s, i, "Missing code.")
return
}
userID, ok := n.s.redeemOneTimeCode(code, time.Now())
if !ok || userID == "" {
_ = respondEphemeral(s, i, "Invalid or expired code. Generate a new one-time code from goPool and try again.")
return
}
if n.s.workerLists != nil {
if err := n.s.workerLists.UpsertDiscordLink(userID, i.Member.User.ID, true, time.Now()); err != nil {
logger.Warn("discord link upsert failed", "error", err)
_ = respondEphemeral(s, i, "Failed to enable notifications (server error).")
return
}
} else {
_ = respondEphemeral(s, i, "Notifications are not enabled on this pool.")
return
}
channelRef := ""
if ch := strings.TrimSpace(n.notifyChannelID); ch != "" {
channelRef = fmt.Sprintf(" in <#%s>", ch)
}
_ = respondEphemeral(s, i, "Enabled. You’ll be pinged"+channelRef+" when a saved worker stays offline for over 2 minutes (and again when it’s back online for 2+ minutes), and when a saved worker finds a block. To turn this off, run `/notify-stop`.")
case "notify-stop":
if n.s.workerLists != nil {
_ = n.s.workerLists.DisableDiscordLinkByDiscordUserID(i.Member.User.ID, time.Now())
}
_ = respondEphemeral(s, i, "Disabled.")
default:
// ignore
}
}
func respondEphemeral(s *discordgo.Session, i *discordgo.InteractionCreate, msg string) error {
if s == nil || i == nil {
return nil
}
return s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
Flags: discordgo.MessageFlagsEphemeral,
},
})
}