111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"dndmusicbot/youtube"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
"github.com/diamondburned/arikawa/v3/voice"
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
|
"github.com/gopxl/beep"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/kataras/go-events"
|
|
"github.com/spf13/afero"
|
|
"github.com/spf13/viper"
|
|
"github.com/steino/gompd/v2/mpd"
|
|
)
|
|
|
|
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() {
|
|
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)
|
|
}
|
|
|
|
app.plmutex = &sync.Mutex{}
|
|
|
|
log.Println("bot.go done.")
|
|
}
|
|
|
|
type App struct {
|
|
discord *state.State
|
|
voice *voice.Session
|
|
youtube *youtube.Client
|
|
ambiance beep.Mixer
|
|
curamb Ambiance
|
|
events events.EventEmmiter
|
|
db *pgx.Conn
|
|
router *httprouter.Router
|
|
cache *filecache.Cache
|
|
mpdc context.CancelFunc
|
|
mpdw *mpd.Watcher
|
|
mpd *mpd.Client
|
|
plmutex *sync.Mutex
|
|
plcancel context.CancelFunc
|
|
}
|
|
|
|
var cwd string
|
|
|
|
func init() {
|
|
ex, err := os.Executable()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
cwd = filepath.Dir(ex)
|
|
}
|
|
|
|
func main() {
|
|
bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache")
|
|
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
|
|
|
|
prune := time.NewTicker(15 * time.Minute)
|
|
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():
|
|
app.db.Close(ctx)
|
|
app.mpdw.Close()
|
|
app.mpdc()
|
|
app.voice.Leave(ctx)
|
|
app.cache.Prune(false)
|
|
dgvc()
|
|
app.discord.Close()
|
|
return
|
|
case <-ticker.C:
|
|
app.events.Emit("tick")
|
|
case <-prune.C:
|
|
app.cache.Prune(false)
|
|
}
|
|
}
|
|
}
|