dndmusicbot/bot.go

68 lines
1.3 KiB
Go

package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/gohugoio/hugo/cache/filecache"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
const (
channels int = 2 // 1 for mono, 2 for stereo
sampleRate int = 48000 // audio sampling rate
frameSize int = 960 // uint16 size of each audio frame
maxBytes int = (frameSize * 2) * 2 // max size of opus data
)
var (
config = viper.GetViper()
bfs = afero.NewBasePathFs(afero.NewOsFs(), "cache")
cache = filecache.NewCache(bfs, 1*time.Hour, "")
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
log.Println("bot.go loading..")
config.SetConfigName("config")
config.SetConfigType("yaml")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
log.Fatal(err)
}
mpd_mutex = &sync.Mutex{}
log.Println("bot.go done.")
}
func main() {
ticker := time.NewTicker(300 * time.Millisecond)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
defer cancel()
for {
select {
case <-ctx.Done():
db.Close(ctx)
mpdw.Close()
mpdcf()
dvoice.Leave(ctx)
dgvc()
dstate.Close()
return
case <-ticker.C:
ev.Emit("tick")
}
}
}