mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 09:02:49 +00:00
added docker support
This commit is contained in:
@@ -229,12 +229,12 @@ func (conn *AudioConnection) loadFile(fileName string) error {
|
||||
log.Debug("Loading file: " + fname + fextension)
|
||||
|
||||
// use ffmpeg to convert file into a format we can use
|
||||
cmd := exec.Command("ffmpeg", "-i", config.Config.SoundsPath+fname+fextension, "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "pipe:1")
|
||||
cmd := exec.Command("ffmpeg", "-i", config.Config.SoundsPath+"/"+fname+fextension, "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "pipe:1")
|
||||
|
||||
ffmpegout, err := cmd.StdoutPipe()
|
||||
|
||||
if err != nil {
|
||||
return errors.New("Unable to execute ffmpeg. To set permissions on this file run chmod +x ffmpeg_linux (or ffmpeg_mac depending which operating system you are on)")
|
||||
return err
|
||||
}
|
||||
|
||||
ffmpegbuf := bufio.NewReaderSize(ffmpegout, 16348)
|
||||
@@ -242,7 +242,7 @@ func (conn *AudioConnection) loadFile(fileName string) error {
|
||||
err = cmd.Start()
|
||||
|
||||
if err != nil {
|
||||
return errors.New("Unable to execute ffmpeg. To set permissions on this file run chmod +x ffmpeg_linux (or ffmpeg_mac depending which operating system you are on)")
|
||||
return err
|
||||
}
|
||||
|
||||
// crate encoder to convert audio to opus codec
|
||||
@@ -299,7 +299,7 @@ func writePacketsToFile(username string, packets chan *discordgo.Packet) {
|
||||
|
||||
// construct filename
|
||||
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
|
||||
filename := config.Config.ClipsPath + timestamp + "-" + username + ".wav"
|
||||
filename := config.Config.ClipsPath + "/" + timestamp + "-" + username + ".wav"
|
||||
|
||||
// grab everything from the voice packet channel and dump it to the file
|
||||
// close when there is nothing left
|
||||
|
||||
@@ -2,7 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -10,40 +9,34 @@ import (
|
||||
|
||||
// Variables used for command line parameters
|
||||
var (
|
||||
Config configFile
|
||||
Flags configFlags
|
||||
Config configType
|
||||
)
|
||||
|
||||
type configFile struct {
|
||||
Token string `json:"token"`
|
||||
ClientID string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
RedirectURI string `json:"redirect_uri"`
|
||||
type configType struct {
|
||||
Token string `json:"token"`
|
||||
ClientID string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
RedirectURI string `json:"redirect_uri"`
|
||||
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
||||
AdminEmails []string `json:"admin_emails"`
|
||||
ModEmails []string `json:"mod_emails"`
|
||||
ServerAddr string `json:"server_addr"`
|
||||
JWTSecret string `json:"jwt_secret"`
|
||||
Logger bool `json:"logger"`
|
||||
|
||||
GuildID string `json:"guild_id"`
|
||||
|
||||
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
||||
|
||||
SoundsPath string `json:"sounds_path"`
|
||||
ClipsPath string `json:"clips_path"`
|
||||
|
||||
AdminEmails []string `json:"admin_emails"`
|
||||
ModEmails []string `json:"mod_emails"`
|
||||
ServerAddr string `json:"server_addr"`
|
||||
JWTKey string `json:"jwt_key"`
|
||||
|
||||
Logger bool `json:"logger"`
|
||||
Database string `json:"database"`
|
||||
}
|
||||
|
||||
type configFlags struct {
|
||||
Prod bool
|
||||
// hard coded folder paths
|
||||
SoundsPath string
|
||||
ClipsPath string
|
||||
YoutubePath string
|
||||
}
|
||||
|
||||
// Init -
|
||||
func Init() {
|
||||
parseConfig()
|
||||
parseFlags()
|
||||
|
||||
Config.SoundsPath = "./sounds"
|
||||
Config.ClipsPath = "./clips"
|
||||
Config.YoutubePath = "./youtube"
|
||||
}
|
||||
|
||||
func parseConfig() {
|
||||
@@ -61,22 +54,6 @@ func parseConfig() {
|
||||
err := json.Unmarshal(file, &Config)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func parseFlags() {
|
||||
|
||||
Flags.Prod = false
|
||||
|
||||
prod := flag.Bool("p", false, "Run in production")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
Flags.Prod = *prod
|
||||
|
||||
if Flags.Prod {
|
||||
log.Warn("Running in production mode")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package db
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
|
||||
// database driver for sqlite
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
@@ -14,7 +13,7 @@ var Conn *gorm.DB
|
||||
// Init - initialize database
|
||||
func Init() {
|
||||
var err error
|
||||
Conn, err = gorm.Open("sqlite3", config.Config.Database)
|
||||
Conn, err = gorm.Open("sqlite3", "data.db")
|
||||
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
|
||||
35
server/main.go
Normal file
35
server/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/mgerb/go-discord-bot/server/bot"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/logger"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetFormatter(&log.JSONFormatter{})
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
//read config file
|
||||
config.Init()
|
||||
|
||||
if config.Config.Logger {
|
||||
db.Init()
|
||||
db.Conn.AutoMigrate(&logger.Message{}, &logger.Attachment{}, &logger.User{})
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// start the bot
|
||||
bot.Start(config.Config.Token)
|
||||
|
||||
// start the web server
|
||||
webserver.Start()
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -16,8 +17,8 @@ func Downloader(c *gin.Context) {
|
||||
fileType := c.Query("fileType")
|
||||
|
||||
// create youtube folder if it does not exist
|
||||
if _, err := os.Stat("youtube"); os.IsNotExist(err) {
|
||||
os.Mkdir("youtube", os.ModePerm)
|
||||
if _, err := os.Stat(config.Config.YoutubePath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.YoutubePath, os.ModePerm)
|
||||
}
|
||||
|
||||
// get the video title
|
||||
@@ -41,7 +42,7 @@ func Downloader(c *gin.Context) {
|
||||
cleanTitle := cleanseTitle(titleOut.String())
|
||||
log.Debug(cleanTitle)
|
||||
|
||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", "./youtube/"+cleanTitle+".%(ext)s", url)
|
||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", config.Config.YoutubePath+"/"+cleanTitle+".%(ext)s", url)
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var soundList []sound
|
||||
|
||||
type sound struct {
|
||||
Prefix string `json:"prefix"`
|
||||
Name string `json:"name"`
|
||||
@@ -22,26 +20,39 @@ type sound struct {
|
||||
// SoundList -
|
||||
func SoundList(c *gin.Context) {
|
||||
|
||||
if len(soundList) < 1 {
|
||||
err := PopulateSoundList()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
soundList, err := readSoundsDir(config.Config.SoundsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, soundList)
|
||||
}
|
||||
|
||||
// PopulateSoundList -
|
||||
func PopulateSoundList() error {
|
||||
// ClipList -
|
||||
func ClipList(c *gin.Context) {
|
||||
|
||||
soundList = []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(config.Config.SoundsPath)
|
||||
clipList, err := readSoundsDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, clipList)
|
||||
}
|
||||
|
||||
func readSoundsDir(dir string) ([]sound, error) {
|
||||
|
||||
soundList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
|
||||
if err != nil {
|
||||
return soundList, err
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
@@ -57,33 +68,5 @@ func PopulateSoundList() error {
|
||||
soundList = append(soundList, listItem)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClipList -
|
||||
func ClipList(c *gin.Context) {
|
||||
|
||||
clipList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
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)
|
||||
}
|
||||
|
||||
c.JSON(200, clipList)
|
||||
return soundList, nil
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ func FileUpload(c *gin.Context) {
|
||||
file.Filename = strings.Replace(file.Filename, " ", "", -1)
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + file.Filename); err == nil {
|
||||
if _, err := os.Stat(config.Config.SoundsPath + "/" + file.Filename); err == nil {
|
||||
c.JSON(http.StatusInternalServerError, "File already exists.")
|
||||
return
|
||||
}
|
||||
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+file.Filename)
|
||||
log.Debug("Saving file", config.Config.SoundsPath+file.Filename)
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+"/"+file.Filename)
|
||||
log.Debug("Saving file", config.Config.SoundsPath+"/"+file.Filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -49,14 +49,5 @@ func FileUpload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// repopulate sound list
|
||||
err = PopulateSoundList()
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error populating sound list.")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "Success")
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func GetJWT(user discord.User) (string, error) {
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(config.Config.JWTKey))
|
||||
return token.SignedString([]byte(config.Config.JWTSecret))
|
||||
}
|
||||
|
||||
func checkEmailPermissions(email string, emails []string) bool {
|
||||
@@ -96,7 +96,7 @@ func AuthorizedJWT() gin.HandlerFunc {
|
||||
|
||||
// parse and verify token
|
||||
token, err := jwt.ParseWithClaims(tokenString[1], &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.Config.JWTKey), nil
|
||||
return []byte(config.Config.JWTSecret), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -16,7 +16,7 @@ func getRouter() *gin.Engine {
|
||||
|
||||
router.StaticFS("/static", box)
|
||||
router.Static("/public/sounds", config.Config.SoundsPath)
|
||||
router.Static("/public/youtube", "./youtube")
|
||||
router.Static("/public/youtube", config.Config.YoutubePath)
|
||||
router.Static("/public/clips", config.Config.ClipsPath)
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user