mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 16:42:48 +00:00
audio clips - client done
This commit is contained in:
@@ -28,9 +28,8 @@ const (
|
||||
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
||||
maxSoundQueue int = 10 // max amount of sounds that can be queued at one time
|
||||
|
||||
sampleRate int = 96000 // rate at which wav writer need to make audio up to speed
|
||||
voiceClipQueuePacketSize int = 2000 // this packet size equates to roughly 40 seconds of audio
|
||||
voiceClipDirectory string = "./clips"
|
||||
sampleRate int = 96000 // rate at which wav writer need to make audio up to speed
|
||||
voiceClipQueuePacketSize int = 2000 // this packet size equates to roughly 40 seconds of audio
|
||||
)
|
||||
|
||||
// store our connection objects in a map tied to a guild id
|
||||
@@ -292,13 +291,13 @@ func (conn *audioConnection) clipAudio(m *discordgo.MessageCreate) {
|
||||
func writePacketsToFile(username string, packets chan *discordgo.Packet) {
|
||||
|
||||
// create clips folder if it does not exist
|
||||
if _, err := os.Stat(voiceClipDirectory); os.IsNotExist(err) {
|
||||
os.Mkdir(voiceClipDirectory, os.ModePerm)
|
||||
if _, err := os.Stat(config.Config.ClipsPath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.ClipsPath, os.ModePerm)
|
||||
}
|
||||
|
||||
// construct filename
|
||||
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
|
||||
wavOut, err := os.Create(voiceClipDirectory + "/" + timestamp + "-" + username + ".wav")
|
||||
wavOut, err := os.Create(config.Config.ClipsPath + timestamp + "-" + username + ".wav")
|
||||
|
||||
checkErr(err)
|
||||
defer wavOut.Close()
|
||||
|
||||
@@ -18,6 +18,7 @@ type configFile struct {
|
||||
Token string `json:"Token"`
|
||||
BotPrefix string `json:"BotPrefix"` //prefix to use for bot commands
|
||||
SoundsPath string `json:"SoundsPath"`
|
||||
ClipsPath string `json:"ClipsPath"`
|
||||
UploadPassword string `json:"UploadPassword"`
|
||||
ServerAddr string `json:"ServerAddr`
|
||||
Pubg struct {
|
||||
|
||||
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
@@ -58,3 +59,31 @@ func PopulateSoundList() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClipList -
|
||||
func ClipList(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
clipList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
response.ERR(w, 500, response.DefaultInternalError)
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileName := strings.Split(f.Name(), ".")[0]
|
||||
extension := strings.Split(f.Name(), ".")[1]
|
||||
|
||||
listItem := sound{
|
||||
Name: fileName,
|
||||
Extension: extension,
|
||||
}
|
||||
|
||||
clipList = append(clipList, listItem)
|
||||
}
|
||||
|
||||
response.JSON(w, clipList)
|
||||
}
|
||||
|
||||
@@ -30,18 +30,22 @@ func getRouter() *chi.Mux {
|
||||
workDir, _ := os.Getwd()
|
||||
|
||||
FileServer(r, "/static", http.Dir(filepath.Join(workDir, "./dist/static")))
|
||||
FileServer(r, "/public/sounds", http.Dir(filepath.Join(workDir, "./sounds")))
|
||||
FileServer(r, "/public/sounds", http.Dir(filepath.Join(workDir, config.Config.SoundsPath)))
|
||||
FileServer(r, "/public/youtube", http.Dir(filepath.Join(workDir, "./youtube")))
|
||||
FileServer(r, "/public/clips", http.Dir(filepath.Join(workDir, config.Config.ClipsPath)))
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./dist/index.html")
|
||||
})
|
||||
|
||||
// configure end points
|
||||
r.Get("/soundlist", handlers.SoundList)
|
||||
r.Put("/upload", handlers.FileUpload)
|
||||
r.Get("/ytdownloader", handlers.Downloader)
|
||||
r.Get("/stats/pubg", pubg.Handler)
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
// configure api end points
|
||||
r.Get("/soundlist", handlers.SoundList)
|
||||
r.Get("/cliplist", handlers.ClipList)
|
||||
r.Put("/upload", handlers.FileUpload)
|
||||
r.Get("/ytdownloader", handlers.Downloader)
|
||||
r.Get("/stats/pubg", pubg.Handler)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user