2020-10-29 02:21:07 -04:00
package main
import (
"flag"
"log"
"os"
"os/signal"
2021-01-03 16:19:49 -05:00
"runtime"
2020-10-29 02:21:07 -04:00
"strconv"
2020-12-29 15:14:19 -05:00
"syscall"
2020-10-29 02:21:07 -04:00
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"layeh.com/gumble/gumble"
_ "layeh.com/gumble/opus"
)
2021-01-03 15:32:59 -05:00
var BridgeConf * BridgeConfig
var Bridge * BridgeState
2020-10-29 02:21:07 -04:00
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" )
2020-11-28 03:46:21 -05:00
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" )
2021-01-03 15:32:59 -05:00
discordCommand := flag . String ( "discord-command" , lookupEnvOrString ( "DISCORD_COMMAND" , "mumble-discord" ) , "Discord command string, env alt DISCORD_COMMAND, optional, defaults to mumble-discord" )
2021-01-03 15:45:12 -05:00
autoMode := flag . Bool ( "auto" , lookupEnvOrBool ( "AUTO_MODE" , false ) , "bridge starts in auto mode" )
2020-10-29 02:21:07 -04:00
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" )
}
2020-10-30 01:38:01 -04:00
// 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
2020-11-04 01:12:43 -05:00
discord . LogLevel = 2
2020-12-29 15:14:19 -05:00
discord . StateEnabled = true
2020-12-29 18:19:44 -05:00
discord . Identify . Intents = discordgo . MakeIntent ( discordgo . IntentsAllWithoutPrivileged )
2020-12-29 15:14:19 -05:00
// register handlers
discord . AddHandler ( ready )
discord . AddHandler ( messageCreate )
discord . AddHandler ( guildCreate )
2021-01-03 15:32:59 -05:00
discord . AddHandler ( voiceUpdate )
2020-10-29 02:21:07 -04:00
err = discord . Open ( )
if err != nil {
log . Println ( err )
return
}
2020-11-04 01:12:43 -05:00
defer discord . Close ( )
2020-10-29 02:21:07 -04:00
log . Println ( "Discord Bot Connected" )
2021-01-03 15:32:59 -05:00
log . Printf ( "Discord bot looking for command !%v" , * discordCommand )
2020-11-04 01:12:43 -05:00
config := gumble . NewConfig ( )
config . Username = * mumbleUsername
config . Password = * mumblePassword
config . AudioInterval = time . Millisecond * 10
2021-01-03 15:32:59 -05:00
BridgeConf = & BridgeConfig {
2020-12-29 15:14:19 -05:00
Config : config ,
MumbleAddr : * mumbleAddr + ":" + strconv . Itoa ( * mumblePort ) ,
MumbleInsecure : * mumbleInsecure ,
2021-01-03 15:45:12 -05:00
Auto : * autoMode ,
2021-01-03 15:32:59 -05:00
Command : * discordCommand ,
GID : * discordGID ,
CID : * discordCID ,
2020-11-28 03:46:21 -05:00
}
2021-01-03 15:32:59 -05:00
Bridge = & BridgeState {
ActiveConn : make ( chan bool ) ,
Connected : false ,
MumbleUserCount : 0 ,
DiscordUserCount : 0 ,
}
userCount := make ( chan int )
go pingMumble ( * mumbleAddr , strconv . Itoa ( * mumblePort ) , userCount )
go discordStatusUpdate ( discord , userCount )
2021-01-03 16:19:49 -05:00
go func ( ) {
for {
time . Sleep ( 3 * time . Second )
log . Println ( runtime . NumGoroutine ( ) )
//pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}
} ( )
2021-01-03 15:45:12 -05:00
if * autoMode {
Bridge . AutoChan = make ( chan bool )
go AutoBridge ( discord )
}
2020-12-29 15:14:19 -05:00
sc := make ( chan os . Signal , 1 )
signal . Notify ( sc , syscall . SIGINT , syscall . SIGTERM , os . Interrupt , os . Kill )
<- sc
log . Println ( "Bot shutting down" )
2020-10-29 02:21:07 -04:00
}