dndmusicbot/bot.go

102 lines
2.0 KiB
Go
Raw Permalink Normal View History

2022-11-18 21:18:12 +00:00
package main
import (
"context"
"log"
"os"
"os/signal"
2023-11-02 20:30:29 +00:00
"path/filepath"
2022-11-18 21:18:12 +00:00
"syscall"
"time"
"dndmusicbot/filecache"
"dndmusicbot/youtube"
2022-12-05 17:24:06 +00:00
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/gopxl/beep"
"github.com/jackc/pgx/v5/pgxpool"
2022-11-18 21:18:12 +00:00
"github.com/julienschmidt/httprouter"
"github.com/kataras/go-events"
"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 (
app = new(App)
config = viper.GetViper()
)
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-11-18 21:18:12 +00:00
log.Println("bot.go done.")
}
type App struct {
2022-12-05 17:24:06 +00:00
discord *state.State
voice *voice.Session
youtube *youtube.Client
ambiance beep.Mixer
2022-11-25 13:06:26 +00:00
curamb Ambiance
2022-11-18 21:18:12 +00:00
events events.EventEmmiter
db *pgxpool.Pool
2022-11-18 21:18:12 +00:00
router *httprouter.Router
cache *filecache.Cache
2022-11-18 21:18:12 +00:00
}
2023-11-02 20:30:29 +00:00
var cwd string
func init() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
cwd = filepath.Dir(ex)
}
2022-11-18 21:18:12 +00:00
func main() {
bfs := afero.NewBasePathFs(afero.NewOsFs(), config.GetString("cache.path"))
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
2023-11-02 20:30:29 +00:00
prune := time.NewTicker(15 * time.Minute)
2022-11-18 21:18:12 +00:00
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():
app.db.Close()
//app.mpdw.Close()
//app.mpdc()
2022-12-05 17:24:06 +00:00
app.voice.Leave(ctx)
2023-11-02 20:30:29 +00:00
app.cache.Prune(false)
2022-12-05 17:24:06 +00:00
dgvc()
2022-11-18 21:18:12 +00:00
app.discord.Close()
return
case <-ticker.C:
app.events.Emit("tick")
2023-11-02 20:30:29 +00:00
case <-prune.C:
app.cache.Prune(false)
2022-11-18 21:18:12 +00:00
}
}
}