/
discord.go
56 lines (46 loc) · 1.29 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
package notification
import (
"errors"
"github.com/TimothyYe/godns/internal/settings"
"github.com/bwmarrin/discordgo"
)
type DiscordNotification struct {
conf *settings.Settings
}
func NewDiscordNotification(conf *settings.Settings) INotification {
return &DiscordNotification{conf: conf}
}
func (n *DiscordNotification) Send(domain, currentIP string) error {
if n.conf.Notify.Discord.BotAPIToken == "" {
return errors.New("bot api token cannot be empty")
}
if n.conf.Notify.Discord.Channel == "" {
return errors.New("channel id cannot be empty")
}
tpl := n.conf.Notify.Discord.MsgTemplate
if tpl == "" {
tpl = "Your IP address for {{.Domain}} has been updated to {{ .CurrentIP }} "
}
msg := buildTemplate(currentIP, domain, tpl)
//Create discordgo client
d, err := discordgo.New("Bot " + n.conf.Notify.Discord.BotAPIToken)
if err != nil {
return errors.New("error creating discord bot")
}
//Open socket connection
err = d.Open()
if err != nil {
return errors.New("error opening connection,")
}
//Send message
_, err = d.ChannelMessageSend(n.conf.Notify.Discord.Channel, msg)
if err != nil {
return errors.New("error sending message")
}
//Close socket connection
err = d.Close()
if err != nil {
return errors.New("error closing discord connection")
}
return nil
}