-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
142 lines (121 loc) · 3.22 KB
/
handler.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
package main
import (
"bytes"
"fmt"
"strings"
"text/tabwriter"
log "github.com/Sirupsen/logrus"
"github.com/bwmarrin/discordgo"
)
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if len(m.Content) <= 0 || (m.Content[0] != '!' && len(m.Mentions) < 1) {
return
}
msg := strings.Replace(m.ContentWithMentionsReplaced(), s.State.Ready.User.Username, "username", 1)
parts := strings.Split(strings.ToLower(msg), " ")
channel, _ := discord.State.Channel(m.ChannelID)
if channel == nil {
log.WithFields(log.Fields{
"channel": m.ChannelID,
"message": m.ID,
}).Warning("Failed to grab channel")
return
}
guild, _ := discord.State.Guild(channel.GuildID)
if guild == nil {
log.WithFields(log.Fields{
"guild": channel.GuildID,
"channel": channel,
"message": m.ID,
}).Warning("Failed to grab guild")
return
}
// If this is a mention, it should come from the owner (otherwise we don't care)
if len(m.Mentions) > 0 && m.Author.ID == OWNER && len(parts) > 0 {
mentioned := false
for _, mention := range m.Mentions {
mentioned = (mention.ID == s.State.Ready.User.ID)
if mentioned {
break
}
}
if mentioned {
handleBotControlMessages(s, m, parts, guild)
}
return
}
if guilds[m.GuildID] == nil {
guilds[m.GuildID] = &Guild{
Guild: guild,
VoiceConnection: nil,
Queue: nil,
SkipPending: false,
DisconnectPending: false,
State: 0,
}
}
guildData := guilds[m.GuildID]
if parts[0] == "!dd" {
if guildData.VoiceConnection != nil {
guildData.DisconnectPending = true
return
}
} else if parts[0] == "!skip" {
guildData.SkipPending = true
} else if parts[0] == "!history" {
if guildData.History[0] == nil {
discord.ChannelMessageSend(channel.ID, "This guild has no history since last bot restart.")
return
}
w := &tabwriter.Writer{}
buf := &bytes.Buffer{}
w.Init(buf, 0, 4, 0, ' ', 0)
fmt.Fprintf(w, ">>> Recently played sounds for current guild:\n")
for i, el := range guildData.History {
if el != nil {
styling := ""
if el.Skipped {
styling += "~~"
}
if el.Forced {
styling += "**"
}
fmt.Fprintf(w, "%s%d. %s !%s%s\n", styling, i+1, el.Sound.Name, el.Sound.Collection.Prefix, Reverse(styling))
}
}
w.Flush()
discord.ChannelMessageSend(channel.ID, buf.String())
}
// Find the collection for the command we got
for _, coll := range COLLECTIONS {
if scontains(parts[0], coll.Commands...) {
if len(parts) >= 2 && parts[1] == "rng4ever" {
guildData.State = RNG4EVER
parts = parts[0:1]
}
// If they passed a specific sound effect, find and select that (otherwise play nothing)
var sound *Sound
if len(parts) > 1 {
for _, s := range coll.Sounds {
if strings.Join(parts[1:len(parts)], " ") == s.Name {
sound = s
}
}
if sound == nil {
return
}
}
go enqueuePlay(m.Author, guild, coll, sound)
return
}
}
}
// Reverse the string
// Source: https://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}