dndmusicbot/bot.go

97 lines
2.0 KiB
Go
Raw 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"
2022-12-05 17:24:06 +00:00
"github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/voice"
"github.com/faiface/beep"
2022-11-23 07:37:59 +00:00
"github.com/fhs/gompd/v2/mpd"
"github.com/gohugoio/hugo/cache/filecache"
2022-11-18 21:18:12 +00:00
"github.com/jackc/pgx/v5"
"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"
"google.golang.org/api/youtube/v3"
)
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)
}
app.plmutex = &sync.Mutex{}
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
2022-11-18 21:18:12 +00:00
youtube *youtube.Service
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 *pgx.Conn
router *httprouter.Router
active []string
plidx int
cache *filecache.Cache
2022-11-23 07:37:59 +00:00
mpdc context.CancelFunc
mpdw *mpd.Watcher
mpd *mpd.Client
plmutex *sync.Mutex
plcancel context.CancelFunc
2022-11-18 21:18:12 +00:00
}
func main() {
bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache")
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
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(ctx)
2022-11-23 07:37:59 +00:00
app.mpdw.Close()
app.mpdc()
2022-12-05 17:24:06 +00:00
app.voice.Leave(ctx)
dgvc()
2022-11-18 21:18:12 +00:00
app.discord.Close()
return
case <-ticker.C:
app.events.Emit("tick")
}
}
}