Skip to content

Commit

Permalink
feat(spock): added ids for each audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Dec 17, 2023
1 parent 1849297 commit 9b929bc
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ install: build
cp -r assets $(DEST_DIR)
cp build/komputer $(DEST_DIR)

run:
go run cmd/komputer/main.go

uninstall:
rm -r $(DEST_DIR)
Expand Down
Binary file added assets/bomba1.mp4
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 19 additions & 13 deletions internal/assets/audio.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
package assets

import (
"errors"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
)

const assertDir = "assets"

func GetAudioPaths(filename string) ([]string, error) {
func Audios() ([]string, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}

basePath := path.Join(dir, assertDir, filename)
dir = filepath.Dir(basePath)
name := filepath.Base(basePath)

ls, err := os.ReadDir(dir)
files, err := os.ReadDir(path.Join(dir, assertDir))
if err != nil {
return nil, err
}

var paths []string
if len(files) <= 0 {
return nil, errors.New("assert directory is empty")
}

for _, d := range ls {
fName := d.Name()
if !d.IsDir() && strings.Contains(fName, name) {
paths = append(paths, filepath.Join(dir, fName))
}
paths := make([]string, len(files))
for i, f := range files {
paths[i] = filepath.Join(dir, assertDir, f.Name())
}

return paths, nil
}

func RandomAudio() (string, error) {
paths, err := Audios()
if err != nil {
return "", err
}

return paths[rand.Int()%len(paths)], nil
}
63 changes: 53 additions & 10 deletions internal/command/spock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/wittano/komputer/internal/interaction"
"github.com/wittano/komputer/internal/log"
"github.com/wittano/komputer/internal/voice"
"math/rand"
"os"
"path/filepath"
"strings"
)

var SpockMusicStopCh = map[string]chan bool{}
Expand All @@ -21,10 +22,36 @@ var SpockCommand = DiscordCommand{
Description: "Say funny world",
GuildID: os.Getenv("SERVER_GUID"),
Type: discordgo.ChatApplicationCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Required: false,
Type: discordgo.ApplicationCommandOptionString,
Name: "id",
Choices: audioIdOptions(),
Description: "Id of audio asset",
},
},
},
Execute: execSpookSpeak,
}

func audioIdOptions() []*discordgo.ApplicationCommandOptionChoice {
list, err := assets.Audios()
if err != nil {
log.Fatal(context.Background(), "Failed to get audios form assets folder", err)
}

result := make([]*discordgo.ApplicationCommandOptionChoice, len(list))
for i, v := range list {
result[i] = &discordgo.ApplicationCommandOptionChoice{
Name: strings.TrimSuffix(filepath.Base(v), filepath.Ext(v)),
Value: v,
}
}

return result
}

func execSpookSpeak(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) {
if _, ok := s.VoiceConnections[i.GuildID]; ok {
interaction.CreateDiscordInteractionResponse(ctx, i, s, interaction.CreateDiscordMsg("Kurwa Spock"))
Expand All @@ -48,18 +75,34 @@ func execSpookSpeak(ctx context.Context, s *discordgo.Session, i *discordgo.Inte
}
defer voiceJoin.Close()

path, err := assets.GetAudioPaths("spock")
if err != nil || len(path) == 0 {
log.Error(ctx, "Failed find any assert \"spock\" in assets directory", err)
return
var songPath string

for _, o := range i.Data.(discordgo.ApplicationCommandInteractionData).Options {
switch o.Name {
case "id":
songPath = o.Value.(string)
default:
songPath, err = assets.RandomAudio()
if err != nil {
log.Error(ctx, "Failed find any audio in assets directory", err)
return
}
}
}

if songPath == "" {
songPath, err = assets.RandomAudio()
if err != nil {
log.Error(ctx, "Failed find any audio in assets directory", err)
return
}
}

ch := make(chan bool)
SpockMusicStopCh[i.GuildID] = ch
stop := make(chan bool)
SpockMusicStopCh[i.GuildID] = stop

songPath := path[rand.Int()%len(path)]
if err = voice.PlayAudio(voiceJoin, songPath, ch); err != nil {
log.Error(ctx, fmt.Sprintf("Failed play '%s' audio", songPath), err)
if err = voice.PlayAudio(voiceJoin, songPath, stop); err != nil {
log.Error(ctx, fmt.Sprintf("Failed play '%s' songPath", songPath), err)
}

voiceJoin.Disconnect()
Expand Down
18 changes: 9 additions & 9 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [
go
ffmpeg
rnix-lsp
nixfmt
nixpkgs-fmt
];
{ pkgs ? import <nixpkgs> { } }:
pkgs.mkShell {
buildInputs = with pkgs; [
gopls
ffmpeg
rnix-lsp
nixfmt
nixpkgs-fmt
];
}

0 comments on commit 9b929bc

Please sign in to comment.