1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-14 11:45:40 -05:00
mumble-discord-bridge/main.go

181 lines
4.3 KiB
Go
Raw Normal View History

2020-10-29 02:21:07 -04:00
package main
import (
"crypto/tls"
2020-10-29 02:21:07 -04:00
"flag"
"fmt"
"log"
"net"
2020-10-29 02:21:07 -04:00
"os"
"os/signal"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"layeh.com/gumble/gumble"
_ "layeh.com/gumble/opus"
)
func lookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func lookupEnvOrInt(key string, defaultVal int) int {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}
func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}
func getConfig(fs *flag.FlagSet) []string {
cfg := make([]string, 0, 10)
fs.VisitAll(func(f *flag.Flag) {
cfg = append(cfg, fmt.Sprintf("%s:%q", f.Name, f.Value.String()))
})
return cfg
}
func main() {
godotenv.Load()
mumbleAddr := flag.String("mumble-address", lookupEnvOrString("MUMBLE_ADDRESS", ""), "MUMBLE_ADDRESS, mumble server address, example example.com")
mumblePort := flag.Int("mumble-port", lookupEnvOrInt("MUMBLE_PORT", 64738), "MUMBLE_PORT mumble port")
mumbleUsername := flag.String("mumble-username", lookupEnvOrString("MUMBLE_USERNAME", "discord-bridge"), "MUMBLE_USERNAME, mumble username")
mumblePassword := flag.String("mumble-password", lookupEnvOrString("MUMBLE_PASSWORD", ""), "MUMBLE_PASSWORD, mumble password, optional")
mumbleInsecure := flag.Bool("mumble-insecure", lookupEnvOrBool("MUMBLE_INSECURE", false), "mumble insecure, env alt MUMBLE_INSECURE")
2020-10-29 02:21:07 -04:00
discordToken := flag.String("discord-token", lookupEnvOrString("DISCORD_TOKEN", ""), "DISCORD_TOKEN, discord bot token")
discordGID := flag.String("discord-gid", lookupEnvOrString("DISCORD_GID", ""), "DISCORD_GID, discord gid")
discordCID := flag.String("discord-cid", lookupEnvOrString("DISCORD_CID", ""), "DISCORD_CID, discord cid")
flag.Parse()
log.Printf("app.config %v\n", getConfig(flag.CommandLine))
if *mumbleAddr == "" {
log.Fatalln("missing mumble address")
}
if *mumbleUsername == "" {
log.Fatalln("missing mumble username")
}
if *discordToken == "" {
log.Fatalln("missing discord bot token")
}
if *discordGID == "" {
log.Fatalln("missing discord gid")
}
if *discordCID == "" {
log.Fatalln("missing discord cid")
}
// DISCORD Setup
2020-10-29 02:21:07 -04:00
discord, err := discordgo.New("Bot " + *discordToken)
if err != nil {
log.Println(err)
return
}
// Open Websocket
discord.LogLevel = 2
2020-10-29 02:21:07 -04:00
err = discord.Open()
if err != nil {
log.Println(err)
return
}
defer discord.Close()
2020-10-29 02:21:07 -04:00
log.Println("Discord Bot Connected")
dgv, err := discord.ChannelVoiceJoin(*discordGID, *discordCID, false, false)
if err != nil {
log.Println(err)
return
}
defer dgv.Speaking(false)
defer dgv.Close()
discord.ShouldReconnectOnError = true
2020-10-29 02:21:07 -04:00
// MUMBLE Setup
config := gumble.NewConfig()
config.Username = *mumbleUsername
config.Password = *mumblePassword
config.AudioInterval = time.Millisecond * 10
m := MumbleDuplex{}
var tlsConfig tls.Config
if *mumbleInsecure {
tlsConfig.InsecureSkipVerify = true
}
mumble, err := gumble.DialWithDialer(new(net.Dialer),*mumbleAddr+":"+strconv.Itoa(*mumblePort),config, &tlsConfig)
if err != nil {
log.Println(err)
return
}
defer mumble.Disconnect()
// Shared Channels
// Shared channels pass PCM information in 10ms chunks [480]int16
var toMumble = mumble.AudioOutgoing()
var toDiscord = make(chan []int16, 100)
var die = make(chan bool)
log.Println("Mumble Connected")
// Start Passing Between
// Mumble
go m.fromMumbleMixer(toDiscord)
config.AudioListeners.Attach(m)
//Discord
go discordReceivePCM(dgv, die)
go fromDiscordMixer(toMumble)
go discordSendPCM(dgv, toDiscord, die)
2020-10-29 02:21:07 -04:00
// Wait for Exit Signal
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
for {
<-ticker.C
if mumble.State() != 2 {
log.Println("Lost mumble connection " + strconv.Itoa(int(mumble.State())))
die <- true
}
}
}()
2020-10-29 02:21:07 -04:00
select {
case sig := <-c:
log.Printf("\nGot %s signal. Terminating Mumble-Bridge\n", sig)
case <-die:
log.Println("\nGot internal die request. Terminating Mumble-Bridge")
2020-10-29 02:21:07 -04:00
}
}