This repository has been archived by the owner on Jul 11, 2020. It is now read-only.
forked from ljgago/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 4
/
discord.go
194 lines (171 loc) · 4.61 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"github.com/bwmarrin/discordgo"
"log"
"strings"
"time"
)
// DiscordConnect make a new connection to Discord
func (b *Bot) DiscordConnect() (err error) {
b.dg, err = discordgo.New("Bot " + b.config.DiscordToken)
if err != nil {
log.Println("FATA: error creating Discord session,", err)
return
}
log.Println("INFO: Bot is opening...")
b.dg.AddHandler(b.MessageCreateHandler)
b.dg.AddHandler(b.GuildCreateHandler)
b.dg.AddHandler(b.GuildDeleteHandler)
b.dg.AddHandler(b.ConnectHandler)
// Open Websocket
err = b.dg.Open()
if err != nil {
log.Println("FATA: Error Open():", err)
return
}
// Get current user (testing for successful login)
_, err = b.dg.User("@me")
if err != nil {
log.Println("FATA:", err)
return
}
log.Println("INFO: Bot is now running. Press CTRL-C to exit.")
b.purgeRoutine()
b.initRoutine()
b.dg.UpdateStatus(0, b.config.DiscordStatus)
return nil
}
// SearchVoiceChannel search the voice channel id into from guild.
func (b Bot) SearchVoiceChannel(user string) (voiceChannelID string) {
for _, g := range b.dg.State.Guilds {
for _, v := range g.VoiceStates {
if v.UserID == user {
return v.ChannelID
}
}
}
return ""
}
// SearchGuild search the guild ID
func (b Bot) SearchGuild(textChannelID string) (guildID string) {
channel, _ := b.dg.Channel(textChannelID)
guildID = channel.GuildID
return
}
// ChMessageSend send a message and auto-remove it in a time
func (b *Bot) ChMessageSend(textChannelID, message string) {
for i := 0; i < 10; i++ {
msg, err := b.dg.ChannelMessageSend(textChannelID, message)
if err != nil {
time.Sleep(1 * time.Second)
continue
}
if b.config.DiscordPurgeTime > 0 {
timestamp := time.Now().UTC().Unix()
message := PurgeMessage{
msg.ID,
msg.ChannelID,
timestamp,
}
b.purgeQueue = append(b.purgeQueue, message)
}
break
}
}
// Routinely purges messages older than the purge time specified in the configuration.
func (b *Bot) purgeRoutine() {
go func() {
for {
for k, v := range b.purgeQueue {
if time.Now().Unix()-b.config.DiscordPurgeTime > v.TimeSent {
b.purgeQueue = append(b.purgeQueue[:k], b.purgeQueue[k+1:]...)
b.dg.ChannelMessageDelete(v.ChannelID, v.ID)
// Break at first match to avoid panic, timing isn't that important here
break
}
}
time.Sleep(time.Second * 1)
}
}()
}
// Creates the running routine that manages the radio signal.
func (b *Bot) initRoutine() {
b.radioSignal = make(chan PkgRadio)
go b.GlobalRadio()
}
// ConnectHandler
func (b *Bot) ConnectHandler(s *discordgo.Session, connect *discordgo.Connect) {
log.Println("INFO: Connected!!")
s.UpdateStatus(0, b.config.DiscordStatus)
}
// GuildCreateHandler
func (b *Bot) GuildCreateHandler(s *discordgo.Session, guild *discordgo.GuildCreate) {
log.Println("INFO: Guild Create:", guild.ID)
}
// GuildDeleteHandler
func (b *Bot) GuildDeleteHandler(s *discordgo.Session, guild *discordgo.GuildDelete) {
log.Println("INFO: Guild Delete:", guild.ID)
v := b.voiceInstances[guild.ID]
if v != nil {
v.Stop()
time.Sleep(200 * time.Millisecond)
b.mutex.Lock()
delete(b.voiceInstances, guild.ID)
b.mutex.Unlock()
}
}
// MessageCreateHandler
func (b *Bot) MessageCreateHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if !strings.HasPrefix(m.Content, b.config.DiscordPrefix) {
return
}
// Method with database (persistent)
guildID := b.SearchGuild(m.ChannelID)
v := b.voiceInstances[guildID]
owner, _ := s.Guild(guildID)
content := strings.Replace(m.Content, b.config.DiscordPrefix, "", 1)
command := strings.Fields(content)
if len(command) == 0 {
return
}
if owner.OwnerID == m.Author.ID {
if strings.HasPrefix(command[0], "ignore") {
err := PutDB(m.ChannelID, "true")
if err == nil {
b.ChMessageSend(m.ChannelID, "[**Music**] `Ignoring` comands in this channel!")
} else {
log.Println("FATA: Error writing in DB,", err)
}
}
if strings.HasPrefix(command[0], "unignore") {
err := PutDB(m.ChannelID, "false")
if err == nil {
b.ChMessageSend(m.ChannelID, "[**Music**] `Unignoring` comands in this channel!")
} else {
log.Println("FATA: Error writing in DB,", err)
}
}
}
// Ignore command if it's in one of the "ignore commands from these channels" channels.
if GetDB(m.ChannelID) == "true" {
return
}
switch command[0] {
case "help":
b.HelpReporter(m)
case "join":
b.JoinReporter(v, m, s)
case "leave":
b.LeaveReporter(v, m)
case "play":
b.PlayReporter(v, m)
case "stop":
b.StopReporter(v, m)
case "np":
b.NowPlayingReporter(v, m)
case "vol":
b.VolumeReporter(v, m)
default:
return
}
}