/
bot.go
106 lines (90 loc) · 2.95 KB
/
bot.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
package soundboard
import (
"github.com/jonas747/dcmd"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/soundboard/models"
"github.com/pkg/errors"
"strings"
)
func (p *Plugin) AddCommands() {
commands.AddRootCommands(&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "Soundboard",
Aliases: []string{"sb"},
Description: "Play, or list soundboard sounds",
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Name", Type: dcmd.String},
},
RunFunc: func(data *dcmd.Data) (interface{}, error) {
sounds, err := GetSoundboardSounds(data.GS.ID, data.Context())
if err != nil {
return nil, errors.WithMessage(err, "GetSoundboardSounds")
}
// Get member from api or state
member := commands.ContextMS(data.Context())
if data.Args[0].Str() == "" {
return ListSounds(sounds, member), nil
}
var sound *models.SoundboardSound
for _, v := range sounds {
if strings.EqualFold(v.Name, data.Args[0].Str()) {
sound = v
break
}
}
if sound == nil {
return "Sound not found, " + ListSounds(sounds, member), nil
} else if !CanPlaySound(sound, member.Roles) {
return "You can't play that sound, either you have a blacklisted role or missing a required role for this sound", nil
}
status := TranscodingStatus(sound.Status)
if status != TranscodingStatusReady {
switch status {
case TranscodingStatusQueued:
return "This sound has yet to be transcoded, if it appear to be stuck in this state then contact support", nil
case TranscodingStatusFailedLong:
return "This sound is too long", nil
case TranscodingStatusFailedOther:
return "This sound failed transcoding, which means you linked or uploaded a invalid media file. You cannot link youtube videos or web pages, has to be direct links to a media file.", nil
case TranscodingStatusTranscoding:
return "This sound is in the process of being converted, please try again in a couple seconds...", nil
}
}
data.GS.RLock()
defer data.GS.RUnlock()
var voiceChannel int64
vs := data.GS.VoiceState(false, data.Msg.Author.ID)
if vs != nil {
voiceChannel = vs.ChannelID
}
if voiceChannel == 0 {
return "You're not in a voice channel", nil
}
if RequestPlaySound(data.GS.ID, voiceChannel, data.Msg.ChannelID, sound.ID) {
return "Queued up", nil
}
return "Playing it now", nil
},
})
}
func ListSounds(sounds []*models.SoundboardSound, ms *dstate.MemberState) string {
canPlay := ""
restricted := ""
for _, sound := range sounds {
if CanPlaySound(sound, ms.Roles) {
canPlay += "`" + sound.Name + "`, "
} else {
restricted += "`" + sound.Name + "`, "
}
}
out := "Soundboard sounds:\n"
if canPlay != "" {
out += "Can Play: " + canPlay[:len(canPlay)-2] + "\n"
}
if restricted != "" {
out += "No access: " + restricted[:len(restricted)-2] + "\n"
}
out += "\nPlay a sound with `sb <soundname>`"
return out
}