Skip to content

Commit

Permalink
soundboard: move away from gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Jan 3, 2019
1 parent 4ded4d4 commit 628ea9a
Show file tree
Hide file tree
Showing 19 changed files with 2,190 additions and 137 deletions.
6 changes: 3 additions & 3 deletions soundboard/assets/soundboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ <h2 class="card-title">Existing sounds</h2>
</div>
<div class="card-body">
{{$dot := .}}
{{range .Config.Sounds}}
{{range .SoundboardSounds}}
<form id="sound-item-{{.ID}}" data-async-form method="post" action="/manage/{{$dot.ActiveGuild.ID}}/soundboard/update"><input type="text" class="hidden form-control" name="ID" value="{{.ID}}"></form>{{end}}

<table class="table table-responsive-md table-sm mb-0">
Expand All @@ -64,7 +64,7 @@ <h2 class="card-title">Existing sounds</h2>
{{$guildmember := .ActiveGuild.ID}}
{{$roles := .ActiveGuild.Roles}}
{{$highest := .HighestRole}}
{{range .Config.Sounds}}
{{range .SoundboardSounds}}
<tr>
<td>
<input form="sound-item-{{.ID}}" type="text" class="form-control" name="Name" placeholder="" value="{{.Name}}">
Expand All @@ -77,7 +77,7 @@ <h2 class="card-title">Existing sounds</h2>
</td>

<td>
<p class="form-control-static">{{.Status}}</p>
<p class="form-control-static">{{if eq .Status 0}}Queued{{else if eq .Status 1}}Ready{{else if eq .Status 2}}Processing{{else if eq .Status 3}}Too long{{else if eq .Status 4}}Failed, contact support{{end}}</p>
</td>

<td>
Expand Down
35 changes: 15 additions & 20 deletions soundboard/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package soundboard
import (
"github.com/jonas747/dcmd"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common/configstore"
"github.com/jonas747/yagpdb/soundboard/models"
"github.com/pkg/errors"
"golang.org/x/net/context"
"strings"
)

Expand All @@ -21,33 +19,30 @@ func (p *Plugin) AddCommands() {
&dcmd.ArgDef{Name: "Name", Type: dcmd.String},
},
RunFunc: func(data *dcmd.Data) (interface{}, error) {
config := &SoundboardConfig{}
err := configstore.Cached.GetGuildConfig(context.Background(), data.GS.ID, config)
sounds, err := GetSoundboardSounds(data.GS.ID, data.Context())
if err != nil {
return nil, errors.WithMessage(err, "GetGuildConfig")
return nil, errors.WithMessage(err, "GetSoundboardSounds")
}

// Get member from api or state
member, err := bot.GetMember(data.GS.ID, data.Msg.Author.ID)
if err != nil {
return nil, errors.WithMessage(err, "GetMember")
}
member := commands.ContextMS(data.Context())

if data.Args[0].Str() == "" {
return ListSounds(config, member), nil
return ListSounds(sounds, member), nil
}

var sound *SoundboardSound
for _, v := range config.Sounds {
if strings.ToLower(v.Name) == strings.ToLower(data.Args[0].Str()) {
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(config, member), nil
} else if !sound.CanPlay(member.Roles) {
return "You can't play that sound, " + ListSounds(config, member), nil
return "Sound not found, " + ListSounds(sounds, member), nil
} else if !CanPlaySound(sound, member.Roles) {
return "You can't play that sound, " + ListSounds(sounds, member), nil
}

data.GS.RLock()
Expand All @@ -72,12 +67,12 @@ func (p *Plugin) AddCommands() {
})
}

func ListSounds(config *SoundboardConfig, ms *dstate.MemberState) string {
func ListSounds(sounds []*models.SoundboardSound, ms *dstate.MemberState) string {
canPlay := ""
restricted := ""

for _, sound := range config.Sounds {
if sound.CanPlay(ms.Roles) {
for _, sound := range sounds {
if CanPlaySound(sound, ms.Roles) {
canPlay += "`" + sound.Name + "`, "
} else {
restricted += "`" + sound.Name + "`, "
Expand Down
119 changes: 119 additions & 0 deletions soundboard/models/boil_main_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions soundboard/models/boil_queries.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions soundboard/models/boil_queries_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 628ea9a

Please sign in to comment.