dndmusicbot/bot.go

68 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-11-18 21:18:12 +00:00
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
2022-11-18 21:18:12 +00:00
"syscall"
"time"
"github.com/gohugoio/hugo/cache/filecache"
"github.com/spf13/afero"
2022-11-18 21:18:12 +00:00
"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()
2022-12-22 11:43:40 +00:00
bfs = afero.NewBasePathFs(afero.NewOsFs(), "cache")
cache = filecache.NewCache(bfs, 1*time.Hour, "")
2022-11-18 21:18:12 +00:00
)
func init() {
2022-11-23 22:31:31 +00:00
log.SetFlags(log.Ltime | log.Lshortfile)
2022-11-23 07:37:59 +00:00
2022-11-18 21:18:12 +00:00
log.Println("bot.go loading..")
config.SetConfigName("config")
config.SetConfigType("yaml")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
log.Fatal(err)
}
2022-12-22 11:43:40 +00:00
mpd_mutex = &sync.Mutex{}
2022-11-18 21:18:12 +00:00
log.Println("bot.go done.")
}
func main() {
ticker := time.NewTicker(300 * time.Millisecond)
2022-12-05 17:24:06 +00:00
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
defer cancel()
2022-11-18 21:18:12 +00:00
for {
select {
2022-12-05 17:24:06 +00:00
case <-ctx.Done():
2022-12-22 11:43:40 +00:00
db.Close(ctx)
mpdw.Close()
mpdcf()
dvoice.Leave(ctx)
2022-12-05 17:24:06 +00:00
dgvc()
2022-12-22 11:43:40 +00:00
dstate.Close()
2022-11-18 21:18:12 +00:00
return
case <-ticker.C:
2022-12-22 11:43:40 +00:00
ev.Emit("tick")
2022-11-18 21:18:12 +00:00
}
}
}