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"
|
|
|
|
|
2023-11-25 19:05:26 +00:00
|
|
|
"dndmusicbot/filecache"
|
2023-10-23 18:24:52 +00:00
|
|
|
"dndmusicbot/youtube"
|
|
|
|
|
2022-12-05 17:24:06 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
|
|
|
"github.com/diamondburned/arikawa/v3/voice"
|
2023-10-26 20:49:39 +00:00
|
|
|
"github.com/gopxl/beep"
|
2023-11-25 19:05:26 +00:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/kataras/go-events"
|
2022-11-19 16:37:49 +00:00
|
|
|
"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-28 13:02:17 +00:00
|
|
|
|
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
|
2023-10-23 18:24:52 +00:00
|
|
|
youtube *youtube.Client
|
2022-11-19 16:37:49 +00:00
|
|
|
ambiance beep.Mixer
|
2022-11-25 13:06:26 +00:00
|
|
|
curamb Ambiance
|
2022-11-18 21:18:12 +00:00
|
|
|
events events.EventEmmiter
|
2023-11-25 19:05:26 +00:00
|
|
|
db *pgxpool.Pool
|
2022-11-18 21:18:12 +00:00
|
|
|
router *httprouter.Router
|
2022-11-19 16:37:49 +00:00
|
|
|
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() {
|
2023-11-25 19:05:26 +00:00
|
|
|
bfs := afero.NewBasePathFs(afero.NewOsFs(), config.GetString("cache.path"))
|
2022-11-19 16:37:49 +00:00
|
|
|
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():
|
2023-11-25 19:05:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|