-
Notifications
You must be signed in to change notification settings - Fork 927
/
bot.go
126 lines (108 loc) · 3.63 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package soundboard
import (
"strings"
"emperror.dev/errors"
"github.com/jonas747/dcmd/v4"
"github.com/jonas747/dstate/v4"
"github.com/jonas747/yagpdb/analytics"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/soundboard/models"
)
func (p *Plugin) AddCommands() {
commands.AddRootCommands(p,
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "Soundboard",
Aliases: []string{"sb"},
Description: "Play, or list soundboard sounds",
Arguments: []*dcmd.ArgDef{
{Name: "Name", Type: dcmd.String},
},
SlashCommandEnabled: true,
DefaultEnabled: true,
RunFunc: func(data *dcmd.Data) (interface{}, error) {
sounds, err := GetSoundboardSounds(data.GuildData.GS.ID, data.Context())
if err != nil {
return nil, errors.WithMessage(err, "GetSoundboardSounds")
}
// Get member from api or state
member := data.GuildData.MS
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.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
}
}
var voiceChannel int64
vs := data.GuildData.GS.GetVoiceState(data.Author.ID)
if vs != nil {
voiceChannel = vs.ChannelID
}
if voiceChannel == 0 {
return "You're not in a voice channel", nil
}
go analytics.RecordActiveUnit(data.GuildData.GS.ID, p, "playing sound")
if RequestPlaySound(data.GuildData.GS.ID, voiceChannel, data.ChannelID, sound.ID) {
return "Queued up", nil
}
return "Playing it now", nil
},
},
&commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "SoundboardReset",
Aliases: []string{"sbclose", "sbReset"},
Description: "Reset Soundboard Player",
SlashCommandEnabled: true,
DefaultEnabled: true,
RunFunc: func(data *dcmd.Data) (interface{}, error) {
response := resetPlayerServer(data.GuildData.GS.ID)
if response != "" {
return response, nil
}
return "Reset Complete!", nil
},
})
}
func ListSounds(sounds []*models.SoundboardSound, ms *dstate.MemberState) string {
canPlay := ""
restricted := ""
for _, sound := range sounds {
if CanPlaySound(sound, ms.Member.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
}