-
Notifications
You must be signed in to change notification settings - Fork 910
/
soundboard.go
155 lines (127 loc) · 3.45 KB
/
soundboard.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
package soundboard
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/configstore"
"github.com/jonas747/yagpdb/premium"
"golang.org/x/net/context"
"os"
)
type Plugin struct{}
// GetGuildConfig returns a GuildConfig item from db
func (p *Plugin) GetGuildConfig(ctx context.Context, guildID int64, dest configstore.GuildConfig) (err error) {
cast := dest.(*SoundboardConfig)
err = common.GORM.Where(guildID).First(cast).Error
if err != nil {
// Return default config if not found
if err == gorm.ErrRecordNotFound {
*cast = SoundboardConfig{
GuildConfigModel: configstore.GuildConfigModel{
GuildID: guildID,
},
}
} else {
return err
}
}
err = common.GORM.Where("guild_id = ?", guildID).Find(&cast.Sounds).Error
return err
}
// SetGuildConfig saves the GuildConfig struct
func (p *Plugin) SetGuildConfig(ctx context.Context, conf configstore.GuildConfig) error {
return common.GORM.Save(conf).Error
}
// SetIfLatest saves it only if the passedLatest time is the latest version
func (p *Plugin) SetIfLatest(ctx context.Context, conf configstore.GuildConfig) (updated bool, err error) {
err = p.SetGuildConfig(ctx, conf)
return true, err
}
func (p *Plugin) Name() string {
return "Soundboard"
}
func RegisterPlugin() {
p := &Plugin{}
common.RegisterPlugin(p)
configstore.RegisterConfig(p, &SoundboardConfig{})
common.GORM.AutoMigrate(SoundboardConfig{}, SoundboardSound{})
// Setup directories
err := os.MkdirAll("soundboard/queue", 0755)
if err != nil {
if !os.IsExist(err) {
panic(err)
}
}
os.MkdirAll("soundboard/ready", 0755)
}
type SoundboardConfig struct {
configstore.GuildConfigModel
Sounds []*SoundboardSound `gorm:"ForeignKey:GuildID;AssociationForeignKey:GuildID"`
}
func (sc *SoundboardConfig) GetName() string {
return "soundboard"
}
type TranscodingStatus int
const (
// In the transcoding queue
TranscodingStatusQueued TranscodingStatus = iota
// Done transcoding and ready to be played
TranscodingStatusReady
// Currently transcoding
TranscodingStatusTranscoding
// Failed transcoding, too long
TranscodingStatusFailedLong
// Failed transcofing, contact support
TranscodingStatusFailedOther
)
func (s TranscodingStatus) String() string {
switch s {
case TranscodingStatusQueued:
return "Queued"
case TranscodingStatusReady:
return "Ready"
case TranscodingStatusFailedLong:
return "Failed: Too long (max 10sec)"
case TranscodingStatusFailedOther:
return "Failed, contact support"
}
return "Unknown"
}
type SoundboardSound struct {
common.SmallModel
GuildID int64 `gorm:"index"`
RequiredRole string `valid:"role,true"`
Name string `valid:",1,100"`
Status TranscodingStatus
}
func (s *SoundboardSound) CanPlay(roles []int64) bool {
if s.RequiredRole == "" {
return true
}
for _, v := range roles {
if discordgo.StrID(v) == s.RequiredRole {
return true
}
}
return false
}
func KeySoundLock(id uint) string {
return fmt.Sprintf("soundboard_soundlock:%d", id)
}
func SoundFilePath(id uint, status TranscodingStatus) string {
if status == TranscodingStatusReady {
return fmt.Sprintf("soundboard/ready/%d.dca", id)
}
return fmt.Sprintf("soundboard/queue/%d", id)
}
const (
MaxGuildSounds = 50
MaxGuildSoundsPremium = 250
)
func MaxSoundsForContext(ctx context.Context) int {
if premium.ContextPremium(ctx) {
return MaxGuildSoundsPremium
}
return MaxGuildSounds
}