1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-10 09:02:49 +00:00

working on audio clip sound bot

This commit is contained in:
2017-01-17 07:17:44 +01:00
parent 9a720a6261
commit 0673524c31
13 changed files with 205 additions and 62 deletions

4
config.template.json Normal file
View File

@@ -0,0 +1,4 @@
{
"Token": "",
"Activator": "#"
}

BIN
dist/GoBot-linux vendored Executable file

Binary file not shown.

BIN
dist/GoBot-windows.exe vendored Executable file

Binary file not shown.

4
dist/config.json vendored Normal file
View File

@@ -0,0 +1,4 @@
{
"Token": "",
"Activator": "#"
}

20
main.go
View File

@@ -1,20 +0,0 @@
package main
import (
"./bot"
"./config"
"./serverstatus"
)
// Variables used for command line parameters
var (
BotID string
)
func main() {
config.Configure()
bot.Connect(config.Config.Token)
serverstatus.Start()
bot.Start()
}

17
makefile Normal file
View File

@@ -0,0 +1,17 @@
run: clean linux copyconfig
@./dist/GoBot-linux
windows:
@GOOS=windows GOARCH=386 go build -o ./dist/GoBot-windows.exe ./src/main.go
linux:
@go build -o ./dist/GoBot-linux ./src/main.go
clean:
@rm -rf ./dist
copyconfig:
@cp config.template.json ./dist/config.json
build: clean windows linux copyconfig

View File

@@ -1,7 +1,3 @@
# GoBot # GoBot
My experimental Discord bot My experimental Discord bot
## Server Status
This scans the Elysium PvP server checking to see if it is up.

View File

@@ -1,36 +0,0 @@
package serverstatus
import (
"../bot"
"../config"
"github.com/anvie/port-scanner"
"time"
)
const serverAddr string = "149.202.207.235"
func Start() {
go loop()
}
func loop() {
prevServerUp := true
elysiumPvP := portscanner.NewPortScanner(serverAddr, time.Second*2)
for {
serverUp := elysiumPvP.IsOpen(8099)
if serverUp && serverUp != prevServerUp {
sendMessage("@here Elysium PVP is now online!")
} else if !serverUp && serverUp != prevServerUp {
sendMessage("@here Elysium PVP is now offline!")
}
prevServerUp = serverUp
time.Sleep(time.Second * 5)
}
}
func sendMessage(message string) {
bot.Session.ChannelMessageSend(config.Config.AlertRoomID, message)
}

BIN
sounds/airhorn.dca Normal file

Binary file not shown.

View File

@@ -11,8 +11,8 @@ import (
var Config configStruct var Config configStruct
type configStruct struct { type configStruct struct {
Token string `json:"Token"` Token string `json:"Token"`
AlertRoomID string `json:"AlertRoomID"` Activator string `json:"Activator"`
} }
func Configure() { func Configure() {

149
src/handlers/airhorn.go Normal file
View File

@@ -0,0 +1,149 @@
package handlers
import (
"../config"
"encoding/binary"
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"io"
"io/ioutil"
"os"
"strings"
"time"
)
var sounds = make(map[string][][]byte, 0)
const SOUNDS_DIR string = "./sounds/"
func AirhornHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, config.Config.Activator) {
// Find the channel that the message came from.
c, err := s.State.Channel(m.ChannelID)
if err != nil {
// Could not find channel.
return
}
// Find the guild for that channel.
g, err := s.State.Guild(c.GuildID)
if err != nil {
// Could not find guild.
return
}
// Look for the message sender in that guilds current voice states.
for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID {
err = playSound(s, g.ID, vs.ChannelID, strings.TrimPrefix(m.Content, config.Config.Activator))
if err != nil {
fmt.Println("Error playing sound:", err)
}
return
}
}
}
}
// loadSound attempts to load an encoded sound file from disk.
func LoadSounds() error {
files, _ := ioutil.ReadDir(SOUNDS_DIR)
for _, file := range files {
fmt.Println(file.Name())
err := loadFile(file.Name())
if err != nil {
fmt.Println(err.Error())
}
}
return nil
}
func loadFile(fileName string) error {
trimmedName := strings.TrimSuffix(fileName, ".dca")
sounds[trimmedName] = make([][]byte, 0)
file, err := os.Open(SOUNDS_DIR + fileName)
if err != nil {
fmt.Println("Error opening dca file :", err)
return err
}
var opuslen int16
for {
// Read opus frame length from dca file.
err = binary.Read(file, binary.LittleEndian, &opuslen)
// If this is the end of the file, just return.
if err == io.EOF || err == io.ErrUnexpectedEOF {
file.Close()
if err != nil {
return err
}
return nil
}
if err != nil {
fmt.Println("Error reading from dca file :", err)
return err
}
// Read encoded pcm from dca file.
InBuf := make([]byte, opuslen)
err = binary.Read(file, binary.LittleEndian, &InBuf)
// Should not be any end of file errors
if err != nil {
fmt.Println("Error reading from dca file :", err)
return err
}
sounds[trimmedName] = append(sounds[trimmedName], InBuf)
}
}
// playSound plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID string, sound string) (err error) {
if _, ok := sounds[sound]; !ok {
return errors.New("Sound not found")
}
// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, false)
if err != nil {
return err
}
// Sleep for a specified amount of time before playing the sound
time.Sleep(250 * time.Millisecond)
// Start speaking.
_ = vc.Speaking(true)
// Send the buffer data.
for _, buff := range sounds[sound] {
vc.OpusSend <- buff
}
// Stop speaking
_ = vc.Speaking(false)
// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)
// Disconnect from the provided voice channel.
_ = vc.Disconnect()
return nil
}

29
src/main.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import (
"./bot"
"./config"
"./handlers"
)
// Variables used for command line parameters
var (
BotID string
)
func main() {
//read config file
config.Configure()
//connect bot to account with token
bot.Connect(config.Config.Token)
//load sound files into memory
handlers.LoadSounds()
//add handlers
bot.AddHandler(handlers.AirhornHandler)
//start websock to listen for messages
bot.Start()
}