92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/faiface/beep"
|
|
"github.com/fhs/gompd/v2/mpd"
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/kataras/go-events"
|
|
"github.com/spf13/afero"
|
|
"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() {
|
|
log.SetOutput(os.Stdout)
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
log.Println("bot.go loading..")
|
|
config.SetConfigName("config")
|
|
config.SetConfigType("yaml")
|
|
config.AddConfigPath(".")
|
|
err := config.ReadInConfig()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Println("bot.go done.")
|
|
}
|
|
|
|
type App struct {
|
|
discord *discordgo.Session
|
|
voice *discordgo.VoiceConnection
|
|
youtube *youtube.Service
|
|
queue *Queue
|
|
ambiance beep.Mixer
|
|
curamb string
|
|
events events.EventEmmiter
|
|
next bool
|
|
db *pgx.Conn
|
|
router *httprouter.Router
|
|
active []string
|
|
plidx int
|
|
cache *filecache.Cache
|
|
mpdc context.CancelFunc
|
|
mpdw *mpd.Watcher
|
|
mpd *mpd.Client
|
|
}
|
|
|
|
func main() {
|
|
bfs := afero.NewBasePathFs(afero.NewOsFs(), "cache")
|
|
app.cache = filecache.NewCache(bfs, 1*time.Hour, "")
|
|
|
|
ticker := time.NewTicker(300 * time.Millisecond)
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
|
|
for {
|
|
select {
|
|
case <-sc:
|
|
app.db.Close(context.Background())
|
|
app.mpdw.Close()
|
|
app.mpdc()
|
|
app.voice.Close()
|
|
app.discord.Close()
|
|
return
|
|
case <-ticker.C:
|
|
app.events.Emit("tick")
|
|
}
|
|
}
|
|
}
|