2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2022-11-19 16:37:49 +00:00
|
|
|
"github.com/faiface/beep"
|
2022-11-23 07:37:59 +00:00
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2022-11-19 16:37:49 +00:00
|
|
|
"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"
|
2022-11-19 16:37:49 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
log.Println("bot.go done.")
|
|
|
|
}
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
discord *discordgo.Session
|
|
|
|
voice *discordgo.VoiceConnection
|
|
|
|
youtube *youtube.Service
|
|
|
|
queue *Queue
|
2022-11-19 16:37:49 +00:00
|
|
|
ambiance beep.Mixer
|
2022-11-18 21:18:12 +00:00
|
|
|
curamb string
|
|
|
|
events events.EventEmmiter
|
|
|
|
next bool
|
|
|
|
db *pgx.Conn
|
|
|
|
router *httprouter.Router
|
|
|
|
active []string
|
|
|
|
plidx int
|
2022-11-19 16:37:49 +00:00
|
|
|
cache *filecache.Cache
|
2022-11-23 07:37:59 +00:00
|
|
|
mpdc context.CancelFunc
|
|
|
|
mpdw *mpd.Watcher
|
|
|
|
mpd *mpd.Client
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-11-19 16:37:49 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
sc := make(chan os.Signal, 1)
|
2022-11-20 16:30:22 +00:00
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sc:
|
|
|
|
app.db.Close(context.Background())
|
2022-11-23 07:37:59 +00:00
|
|
|
app.mpdw.Close()
|
|
|
|
app.mpdc()
|
2022-11-18 21:18:12 +00:00
|
|
|
app.voice.Close()
|
|
|
|
app.discord.Close()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
app.events.Emit("tick")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|