mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 09:02:49 +00:00
major file refactor - fix disconnecting bug
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,3 +3,5 @@ dist
|
|||||||
node_modules
|
node_modules
|
||||||
yarn-error*
|
yarn-error*
|
||||||
vendor
|
vendor
|
||||||
|
bot
|
||||||
|
sounds
|
||||||
|
|||||||
18
.vscode/launch.json
vendored
Normal file
18
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Launch",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "debug",
|
||||||
|
"remotePath": "",
|
||||||
|
"port": 2345,
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"program": "${workspaceRoot}",
|
||||||
|
"env": {},
|
||||||
|
"args": [],
|
||||||
|
"showLog": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
Before Width: | Height: | Size: 434 KiB After Width: | Height: | Size: 434 KiB |
@@ -9,7 +9,7 @@ module.exports = {
|
|||||||
vendor: ['react', 'react-dom']
|
vendor: ['react', 'react-dom']
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
path: path.resolve(__dirname, 'dist'),
|
path: path.resolve(__dirname, '../dist'),
|
||||||
filename: '/static/[name].[hash].js'
|
filename: '/static/[name].[hash].js'
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
0
ffmpeg_linux
Normal file → Executable file
0
ffmpeg_linux
Normal file → Executable file
22
makefile
22
makefile
@@ -1,20 +1,16 @@
|
|||||||
run:
|
run:
|
||||||
go run ./server/main.go
|
go run ./main.go
|
||||||
|
|
||||||
install:
|
install:
|
||||||
go get ./server && yarn install
|
go get && cd client && yarn install
|
||||||
|
|
||||||
build:
|
build-server:
|
||||||
go build -o ./dist/bot ./server/main.go
|
go build -o bot ./main.go
|
||||||
|
|
||||||
|
build-client:
|
||||||
|
cd client && yarn run build
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf ./dist
|
rm -rf bot ./dist
|
||||||
|
|
||||||
copyfiles:
|
all: install build-server build-client
|
||||||
cp config.template.json ./dist/config.template.json
|
|
||||||
cp ffmpeg_linux ./dist/
|
|
||||||
cp ffmpeg_mac ./dist/
|
|
||||||
cp ffmpeg_windows.exe ./dist/
|
|
||||||
|
|
||||||
all: install build copyfiles
|
|
||||||
yarn run build
|
|
||||||
|
|||||||
@@ -15,23 +15,21 @@ NOTE: Currently the binaries in the release package only run on linux. Check the
|
|||||||
|
|
||||||
### NOTE
|
### NOTE
|
||||||
|
|
||||||
If you get a permissions error with ffmpeg:
|
If you get a permissions error with ffmpeg on mac or linux:
|
||||||
`sudo chmod +x dist/ffmpeg`
|
`sudo chmod +x dist/ffmpeg_linux`
|
||||||
|
|
||||||
Sounds are stored in the `dist/sounds` directory. You may copy files directly to this folder rather than uploading through the site.
|
Sounds are stored in the `sounds` directory. You may copy files directly to this folder rather than uploading through the site.
|
||||||
|
|
||||||
## Building from Source
|
## Building from Source
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
- Go
|
- Go
|
||||||
- Godep - [Godep package manager](https://github.com/tools/godep)
|
|
||||||
- Yarn (or npm - makefile will need to be adjusted)
|
- Yarn (or npm - makefile will need to be adjusted)
|
||||||
- make
|
- make
|
||||||
|
|
||||||
### Compiling
|
### Compiling
|
||||||
- Make sure dependencies are installed
|
- Make sure dependencies are installed
|
||||||
- `make all`
|
- `make all`
|
||||||
- cd into ./dist
|
|
||||||
- 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`
|
||||||
- run the executable
|
- run the executable
|
||||||
|
|||||||
@@ -26,23 +26,50 @@ const (
|
|||||||
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
maxBytes int = (frameSize * 2) * 2 // max size of opus data
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// store our connection objects in a map tied to a guild id
|
||||||
|
var activeConnections = make(map[string]*audioConnection)
|
||||||
|
|
||||||
|
type audioConnection struct {
|
||||||
|
guild *discordgo.Guild
|
||||||
|
sounds map[string]*audioClip
|
||||||
|
soundQueue chan string
|
||||||
|
voiceConnection *discordgo.VoiceConnection
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sounds = make(map[string]*AudioClip, 0)
|
sounds = make(map[string]*audioClip, 0)
|
||||||
soundQueue = []string{}
|
soundQueue = []string{}
|
||||||
soundPlayingLock = false
|
soundPlayingLock = false
|
||||||
voiceConnection *discordgo.VoiceConnection
|
voiceConnection *discordgo.VoiceConnection
|
||||||
)
|
)
|
||||||
|
|
||||||
type AudioClip struct {
|
type audioClip struct {
|
||||||
Name string
|
Name string
|
||||||
Extension string
|
Extension string
|
||||||
Content [][]byte
|
Content [][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
const SOUNDS_DIR string = "./sounds/"
|
// SoundsHandler -
|
||||||
|
|
||||||
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
|
// get guild ID and check for connection instance
|
||||||
|
c, err := s.State.Channel(m.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
// Could not find channel.
|
||||||
|
fmt.Println("Unable to find channel.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := activeConnections[c.GuildID]; !ok {
|
||||||
|
newConnectionInstance, err := getNewConnectionInstance(s, m)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
activeConnections[c.GuildID] = newConnectionInstance
|
||||||
|
}
|
||||||
|
|
||||||
// check if valid command
|
// check if valid command
|
||||||
if strings.HasPrefix(m.Content, config.Config.BotPrefix) {
|
if strings.HasPrefix(m.Content, config.Config.BotPrefix) {
|
||||||
|
|
||||||
@@ -62,6 +89,10 @@ func SoundsHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getNewConnectionInstance(s *discordgo.Session, m *discordgo.MessageCreate) (*audioConnection, error) {
|
||||||
|
return &audioConnection{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func dismiss() {
|
func dismiss() {
|
||||||
if voiceConnection != nil {
|
if voiceConnection != nil {
|
||||||
voiceConnection.Disconnect()
|
voiceConnection.Disconnect()
|
||||||
@@ -213,7 +244,7 @@ func loadFile(fileName string) error {
|
|||||||
return errors.New("NewEncoder error.")
|
return errors.New("NewEncoder error.")
|
||||||
}
|
}
|
||||||
|
|
||||||
sounds[fileName] = &AudioClip{
|
sounds[fileName] = &audioClip{
|
||||||
Content: make([][]byte, 0),
|
Content: make([][]byte, 0),
|
||||||
Name: fileName,
|
Name: fileName,
|
||||||
Extension: fextension,
|
Extension: fextension,
|
||||||
@@ -249,7 +280,7 @@ func playSounds(s *discordgo.Session, guildID, channelID string) (err error) {
|
|||||||
soundPlayingLock = true
|
soundPlayingLock = true
|
||||||
|
|
||||||
// Join the channel the user issued the command from if not in it
|
// Join the channel the user issued the command from if not in it
|
||||||
if voiceConnection == nil || voiceConnection.ChannelID != channelID {
|
if voiceConnection == nil || !voiceConnection.Ready {
|
||||||
var err error
|
var err error
|
||||||
voiceConnection, err = s.ChannelVoiceJoin(guildID, channelID, false, false)
|
voiceConnection, err = s.ChannelVoiceJoin(guildID, channelID, false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -28,11 +28,11 @@ func registerRoutes(router *fasthttprouter.Router) {
|
|||||||
router.GET("/soundlist", handlers.SoundList)
|
router.GET("/soundlist", handlers.SoundList)
|
||||||
router.PUT("/upload", handlers.FileUpload)
|
router.PUT("/upload", handlers.FileUpload)
|
||||||
|
|
||||||
router.ServeFiles("/static/*filepath", "./static")
|
router.ServeFiles("/static/*filepath", "./dist/static")
|
||||||
router.ServeFiles("/sounds/*filepath", config.Config.SoundsPath)
|
router.ServeFiles("/sounds/*filepath", config.Config.SoundsPath)
|
||||||
|
|
||||||
router.NotFound = func(ctx *fasthttp.RequestCtx) {
|
router.NotFound = func(ctx *fasthttp.RequestCtx) {
|
||||||
fasthttp.ServeFile(ctx, "./index.html")
|
fasthttp.ServeFile(ctx, "./dist/index.html")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user