1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 16:42:48 +00:00

Merge pull request #20 from mgerb/development

added go dep - removed ffmpeg for saving audio clips
This commit is contained in:
2018-10-07 22:16:13 -05:00
committed by GitHub
6 changed files with 415 additions and 19 deletions

View File

@@ -13,7 +13,10 @@ COPY --from=0 /home/dist /go/src/github.com/mgerb/go-discord-bot/dist
ADD ./server . ADD ./server .
RUN apk add --no-cache git alpine-sdk RUN apk add --no-cache git alpine-sdk
RUN go get -u github.com/gobuffalo/packr/... RUN go get -u github.com/gobuffalo/packr/...
RUN go get RUN go get -u github.com/golang/dep/cmd/dep
RUN dep ensure
# need to manually get this dependency because go dep doesn't work well with the C bindings
RUN go get layeh.com/gopus
RUN packr build -o /build/server RUN packr build -o /build/server

View File

@@ -1,8 +1,6 @@
run:
go run ./main.go
install: install:
go get ./server && cd client && npm install cd server && dep ensure
cd client && npm install
build-server: build-server:
cd ./server && packr build -o ../bot ./main.go cd ./server && packr build -o ../bot ./main.go

View File

@@ -1,6 +1,6 @@
# Discord Sound Bot # Discord Sound Bot
A soundboard bot for discord with a Go back end and React front end. A soundboard bot for discord. Build with Go/React.
![Image](./screenshots/sound-bot.png) ![Image](./screenshots/sound-bot.png)
@@ -39,6 +39,7 @@ services:
- `summon` - summons the bot to your current channel - `summon` - summons the bot to your current channel
- `dismiss` - dismisses the bot from the server - `dismiss` - dismisses the bot from the server
- `<audio clip>` - play a named audio clip - `<audio clip>` - play a named audio clip
- `random` - play a random audio clip
### Uploading files ### Uploading files
@@ -79,22 +80,19 @@ Check it out in the "Stats" page on the site.
### Dependencies ### Dependencies
- Go - Go (1.10+)
- node/npm - node/npm (node 8)
- make - make
### Compiling ### Compiling
- Make sure dependencies are installed - Install dependencies
- install packr - `go get -u github.com/gobuffalo/packr/...` - packr - `go get -u github.com/gobuffalo/packr/...`
- [dep](https://github.com/golang/dep) - `go get -u github.com/golang/dep/cmd/dep`
- Rename the `config.template.json` to `config.json` - Rename the `config.template.json` to `config.json`
- add configurations to `config.json` - add configurations to `config.json`
- `cd client && npm run dev` - run `make all`
- `go run main.go`
- open a browser `localhost:<config_port>` - open a browser `localhost:<config_port>`
[Packr](https://github.com/gobuffalo/packr) is used to bundle the static web assets into the binary. [Packr](https://github.com/gobuffalo/packr) is used to bundle the static web assets into the binary.
Use these commands to compile the project. The client must be built first. Use these commands to compile the project. The client must be built first.
- `packr build`
- `packr install`

289
server/Gopkg.lock generated Normal file
View File

@@ -0,0 +1,289 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:a62f6ed230a8cd138a9efbe718e7d0b0294f139266f5f55cd942769a9aac8de2"
name = "github.com/PuerkitoBio/goquery"
packages = ["."]
pruneopts = "UT"
revision = "dc2ec5c7ca4d9aae063b79b9f581dd3ea6afd2b2"
version = "v1.4.1"
[[projects]]
digest = "1:66b3310cf22cdc96c35ef84ede4f7b9b370971c4025f394c89a2638729653b11"
name = "github.com/andybalholm/cascadia"
packages = ["."]
pruneopts = "UT"
revision = "901648c87902174f774fac311d7f176f8647bdaa"
version = "v1.0.0"
[[projects]]
digest = "1:4fd5ce7844c22e194005b9e12fee8adc70fb5ba0bbba9e1964d2e3d1f301d789"
name = "github.com/bwmarrin/discordgo"
packages = ["."]
pruneopts = "UT"
revision = "4a33b9bc7c56cfdb9bb244e33e83cb3941fe2bdc"
version = "v0.18.0"
[[projects]]
branch = "master"
digest = "1:36fe9527deed01d2a317617e59304eb2c4ce9f8a24115bcc5c2e37b3aee5bae4"
name = "github.com/gin-contrib/sse"
packages = ["."]
pruneopts = "UT"
revision = "22d885f9ecc78bf4ee5d72b937e4bbcdc58e8cae"
[[projects]]
digest = "1:9a20e959f4a4b52a79155282a67046fa49869993732ef6d5bc3c48b3e18692cc"
name = "github.com/gin-gonic/gin"
packages = [
".",
"binding",
"json",
"render",
]
pruneopts = "UT"
revision = "b869fe1415e4b9eb52f247441830d502aece2d4d"
version = "v1.3.0"
[[projects]]
branch = "master"
digest = "1:602a78a9834f7b1f67186a69970dc0f5f6f9557ef52fd786586e5be14f5a6ed6"
name = "github.com/go-audio/audio"
packages = ["."]
pruneopts = "UT"
revision = "b697a35b5608fcf1c4f90a9d4f33d76bd70e8fe6"
[[projects]]
branch = "master"
digest = "1:505ceb99060820f1c99a687795774a1df5a6fd4a3a5dbb4eb915bff3ac3a94d7"
name = "github.com/go-audio/wav"
packages = ["."]
pruneopts = "UT"
revision = "87c13ac40f038b0316963807a7d476544c7e9136"
[[projects]]
digest = "1:c8ad4ba32ae8b71ef2bf5e1bad402aeb99ce77a1240f9b684db8b1ff5353e7ab"
name = "github.com/gobuffalo/packr"
packages = ["."]
pruneopts = "UT"
revision = "5a2cbb54c4e7d482e3f518c56f1f86f133d5204f"
version = "v1.13.7"
[[projects]]
digest = "1:97df918963298c287643883209a2c3f642e6593379f97ab400c2a2e219ab647d"
name = "github.com/golang/protobuf"
packages = ["proto"]
pruneopts = "UT"
revision = "aa810b61a9c79d51363740d207bb46cf8e620ed5"
version = "v1.2.0"
[[projects]]
digest = "1:7b5c6e2eeaa9ae5907c391a91c132abfd5c9e8a784a341b5625e750c67e6825d"
name = "github.com/gorilla/websocket"
packages = ["."]
pruneopts = "UT"
revision = "66b9c49e59c6c48f0ffce28c2d8b8a5678502c6d"
version = "v1.4.0"
[[projects]]
digest = "1:111ff38c0376c091da851d34ad991fbce6a679a8b53fcc5ebd61b52f07ed197d"
name = "github.com/jinzhu/gorm"
packages = [
".",
"dialects/sqlite",
]
pruneopts = "UT"
revision = "6ed508ec6a4ecb3531899a69cbc746ccf65a4166"
version = "v1.9.1"
[[projects]]
branch = "master"
digest = "1:fd97437fbb6b7dce04132cf06775bd258cce305c44add58eb55ca86c6c325160"
name = "github.com/jinzhu/inflection"
packages = ["."]
pruneopts = "UT"
revision = "04140366298a54a039076d798123ffa108fff46c"
[[projects]]
digest = "1:3e551bbb3a7c0ab2a2bf4660e7fcad16db089fdcfbb44b0199e62838038623ea"
name = "github.com/json-iterator/go"
packages = ["."]
pruneopts = "UT"
revision = "1624edc4454b8682399def8740d46db5e4362ba4"
version = "v1.1.5"
[[projects]]
digest = "1:0a69a1c0db3591fcefb47f115b224592c8dfa4368b7ba9fae509d5e16cdc95c8"
name = "github.com/konsorten/go-windows-terminal-sequences"
packages = ["."]
pruneopts = "UT"
revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242"
version = "v1.0.1"
[[projects]]
branch = "master"
digest = "1:8c2ec3f7fa853d374b340831554f225ead9ab4cc6932256f0cffbfef8c78d9f5"
name = "github.com/mattetti/audio"
packages = ["riff"]
pruneopts = "UT"
revision = "01576cde1f212c296c0b8ef5fa857169770c75b6"
[[projects]]
digest = "1:0981502f9816113c9c8c4ac301583841855c8cf4da8c72f696b3ebedf6d0e4e5"
name = "github.com/mattn/go-isatty"
packages = ["."]
pruneopts = "UT"
revision = "6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c"
version = "v0.0.4"
[[projects]]
digest = "1:3cafc6a5a1b8269605d9df4c6956d43d8011fc57f266ca6b9d04da6c09dee548"
name = "github.com/mattn/go-sqlite3"
packages = ["."]
pruneopts = "UT"
revision = "25ecb14adfc7543176f7d85291ec7dba82c6f7e4"
version = "v1.9.0"
[[projects]]
digest = "1:33422d238f147d247752996a26574ac48dcf472976eda7f5134015f06bf16563"
name = "github.com/modern-go/concurrent"
packages = ["."]
pruneopts = "UT"
revision = "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94"
version = "1.0.3"
[[projects]]
digest = "1:e32bdbdb7c377a07a9a46378290059822efdce5c8d96fe71940d87cb4f918855"
name = "github.com/modern-go/reflect2"
packages = ["."]
pruneopts = "UT"
revision = "4b7aa43c6742a2c18fdef89dd197aaae7dac7ccd"
version = "1.0.1"
[[projects]]
digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = "UT"
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
branch = "master"
digest = "1:d10708438642755d6fd37c570b9d01cb5263e7e4248f813b079b031cdd7cf618"
name = "github.com/rylio/ytdl"
packages = ["."]
pruneopts = "UT"
revision = "0227c2bacb82a434f2332d7d8c64093615c08a40"
[[projects]]
digest = "1:dc2d85c13ac22c22a1f3170a41a8e1b897fa05134aaf533f16df44f66a25b4a1"
name = "github.com/sirupsen/logrus"
packages = ["."]
pruneopts = "UT"
revision = "a67f783a3814b8729bd2dac5780b5f78f8dbd64d"
version = "v1.1.0"
[[projects]]
digest = "1:2cab41f59638fdb77dda96ab4f9b5860ef30f48967557254eba127e43c756f2e"
name = "github.com/tidwall/gjson"
packages = ["."]
pruneopts = "UT"
revision = "1e3f6aeaa5bad08d777ea7807b279a07885dd8b2"
version = "v1.1.3"
[[projects]]
branch = "master"
digest = "1:d3f968e2a2c9f8506ed44b01b605ade0176ba6cf73ff679073e77cfdef2c0d55"
name = "github.com/tidwall/match"
packages = ["."]
pruneopts = "UT"
revision = "1731857f09b1f38450e2c12409748407822dc6be"
[[projects]]
digest = "1:03aa6e485e528acb119fb32901cf99582c380225fc7d5a02758e08b180cb56c3"
name = "github.com/ugorji/go"
packages = ["codec"]
pruneopts = "UT"
revision = "b4c50a2b199d93b13dc15e78929cfb23bfdf21ab"
version = "v1.1.1"
[[projects]]
branch = "master"
digest = "1:a6c91777916f37c288a9f2e352feb7567c3ff4c47a3880b391070740d3357e4f"
name = "golang.org/x/crypto"
packages = [
"internal/subtle",
"nacl/secretbox",
"poly1305",
"salsa20/salsa",
"ssh/terminal",
]
pruneopts = "UT"
revision = "e3636079e1a4c1f337f212cc5cd2aca108f6c900"
[[projects]]
branch = "master"
digest = "1:17a5cb5dbf1abd5193243c18a6d7edcad08fb01a3b75c96ed050623ce69efb16"
name = "golang.org/x/net"
packages = [
"html",
"html/atom",
]
pruneopts = "UT"
revision = "146acd28ed5894421fb5aac80ca93bc1b1f46f87"
[[projects]]
branch = "master"
digest = "1:19b2eb89a60dafcfe5c699fa21ef51143d9bc4825c155608f81e56e0a9a4bba4"
name = "golang.org/x/sys"
packages = [
"unix",
"windows",
]
pruneopts = "UT"
revision = "4497e2df6f9e69048a54498c7affbbec3294ad47"
[[projects]]
digest = "1:76dc72490af7174349349838f2fe118996381b31ea83243812a97e5a0fd5ed55"
name = "gopkg.in/dgrijalva/jwt-go.v3"
packages = ["."]
pruneopts = "UT"
revision = "06ea1031745cb8b3dab3f6a236daf2b0aa468b7e"
version = "v3.2.0"
[[projects]]
digest = "1:cbc72c4c4886a918d6ab4b95e347ffe259846260f99ebdd8a198c2331cf2b2e9"
name = "gopkg.in/go-playground/validator.v8"
packages = ["."]
pruneopts = "UT"
revision = "5f1438d3fca68893a817e4a66806cea46a9e4ebf"
version = "v8.18.2"
[[projects]]
digest = "1:342378ac4dcb378a5448dd723f0784ae519383532f5e70ade24132c4c8693202"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = "UT"
revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183"
version = "v2.2.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/bwmarrin/discordgo",
"github.com/gin-gonic/gin",
"github.com/go-audio/audio",
"github.com/go-audio/wav",
"github.com/gobuffalo/packr",
"github.com/jinzhu/gorm",
"github.com/jinzhu/gorm/dialects/sqlite",
"github.com/rylio/ytdl",
"github.com/sirupsen/logrus",
"github.com/tidwall/gjson",
"gopkg.in/dgrijalva/jwt-go.v3",
]
solver-name = "gps-cdcl"
solver-version = 1

70
server/Gopkg.toml Normal file
View File

@@ -0,0 +1,70 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
ignored = ["layeh.com/gopus"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
name = "github.com/bwmarrin/discordgo"
version = "0.18.0"
[[constraint]]
name = "github.com/gin-gonic/gin"
version = "1.3.0"
[[constraint]]
branch = "master"
name = "github.com/go-audio/audio"
[[constraint]]
branch = "master"
name = "github.com/go-audio/wav"
[[constraint]]
name = "github.com/gobuffalo/packr"
version = "1.13.7"
[[constraint]]
name = "github.com/jinzhu/gorm"
version = "1.9.1"
[[constraint]]
branch = "master"
name = "github.com/rylio/ytdl"
[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.1.0"
[[constraint]]
name = "github.com/tidwall/gjson"
version = "1.1.3"
[[constraint]]
name = "gopkg.in/dgrijalva/jwt-go.v3"
version = "3.2.0"
[prune]
go-tests = true
unused-packages = true

View File

@@ -18,6 +18,8 @@ import (
"layeh.com/gopus" "layeh.com/gopus"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
"github.com/mgerb/go-discord-bot/server/config" "github.com/mgerb/go-discord-bot/server/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -376,18 +378,54 @@ loop:
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix())) timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
filename := config.Config.ClipsPath + "/" + timestamp + "-" + strconv.Itoa(int(key)) + "-" + username + ".wav" filename := config.Config.ClipsPath + "/" + timestamp + "-" + strconv.Itoa(int(key)) + "-" + username + ".wav"
cmd := exec.Command("ffmpeg", "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "-i", "pipe:0", filename) out, err := os.Create(filename)
if err != nil {
log.Error(err)
continue
}
// 8 kHz, 16 bit, 2 channel, WAV.
e := wav.NewEncoder(out, sampleRate, 16, channels, 1)
output := new(bytes.Buffer) output := new(bytes.Buffer)
binary.Write(output, binary.LittleEndian, pcmData) binary.Write(output, binary.LittleEndian, pcmData)
cmd.Stdin = bytes.NewReader(output.Bytes()) newReader := bytes.NewReader(output.Bytes())
err := cmd.Run() // Create new audio.IntBuffer.
audioBuf, err := newAudioIntBuffer(newReader)
if err != nil { // Write buffer to output file. This writes a RIFF header and the PCM chunks from the audio.IntBuffer.
if err := e.Write(audioBuf); err != nil {
log.Error(err)
out.Close()
continue
}
if err := e.Close(); err != nil {
log.Error(err) log.Error(err)
} }
out.Close()
}
}
func newAudioIntBuffer(r io.Reader) (*audio.IntBuffer, error) {
buf := &audio.IntBuffer{
Format: &audio.Format{
NumChannels: 1,
SampleRate: 8000,
},
}
for {
var sample int16
err := binary.Read(r, binary.LittleEndian, &sample)
switch {
case err == io.EOF:
return buf, nil
case err != nil:
return nil, err
}
buf.Data = append(buf.Data, int(sample))
} }
} }