-
Notifications
You must be signed in to change notification settings - Fork 0
/
beatrix.go
122 lines (113 loc) · 2.33 KB
/
beatrix.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
package beatrix
import (
"github.com/bwmarrin/discordgo"
"log"
"sync"
)
type DS struct {
Mutex sync.Mutex
Discord *discordgo.Session
}
var (
Token string
Issuer string
ChannelID string
ErrorMode bool
Discord DS
)
// Function to fire up discord bot, issuer will be in the heading of message, token is bot token and channelId is channelID
func Init(issuer, token, channelID string) {
Token = token
Issuer = issuer
ChannelID = channelID
ErrorMode = false
var err error
Discord.Mutex.Lock()
Discord.Discord, err = discordgo.New("Bot " + Token)
if err != nil {
// Failed to init Beatrix
log.Panic(err)
}
err = Discord.Discord.Open()
if err != nil {
// Failed to init Beatrix
log.Panic(err)
}
_, err = Discord.Discord.ChannelMessageSend(ChannelID, "["+Issuer+" / INIT]")
if err != nil {
log.Panic(err)
}
Discord.Mutex.Unlock()
}
func Reinit() {
var err error
Discord.Mutex.Lock()
Discord.Discord, err = discordgo.New("Bot " + Token)
if err != nil {
// Failed to init Beatrix
ErrorMode = true
log.Println(err)
return
}
err = Discord.Discord.Open()
Discord.Mutex.Unlock()
if err != nil {
// Failed to init Beatrix
ErrorMode = true
log.Println(err)
return
}
ErrorMode = false
return
}
// Simply send a message to main channel
func Message(message string) {
message = "[" + Issuer + "]\n" + message
if ErrorMode {
log.Println(message)
Reinit()
return
}
Discord.Mutex.Lock()
_, err := Discord.Discord.ChannelMessageSend(ChannelID, message)
Discord.Mutex.Unlock()
if err != nil {
// Since we have goroutine, we don't have to return or something
// Better re-init discord
log.Println(err)
Reinit()
}
}
// Send error message to channel
func SendError(message, localIssuer string) {
message = "[" + Issuer + " | " + localIssuer + "]\n" + message
if ErrorMode {
log.Println(message)
Reinit()
return
}
Discord.Mutex.Lock()
_, err := Discord.Discord.ChannelMessageSend(ChannelID, message)
Discord.Mutex.Unlock()
if err != nil {
log.Println(err)
Reinit()
}
return
}
func Panic(message string) {
m := "[" + Issuer + " / PANIC]\n@everyone\n\n" + message
if ErrorMode {
log.Println(message)
Reinit()
return
}
Discord.Mutex.Lock()
_, err := Discord.Discord.ChannelMessageSend(ChannelID, m)
Discord.Mutex.Unlock()
if err != nil {
log.Println(err)
Reinit()
}
return
}