Need help for Discord bot
2 hours ago, vorticalbox said:i believe the term "server" means the Terraria server and not the host VM.
Oh that makes more sense. Here's a small discord bot in Go to do that. Note that I don't know how terraria servers work so the exec.Commands are just a placeholder
package main import ( "flag" "log" "os/exec" dg "github.com/bwmarrin/discordgo" ) func main() { t := flag.String("t", "", "token") flag.Parse() s, _ := dg.New("Bot " + *t) err := s.Open() if err != nil { log.Fatal(err) } s.AddHandler(func(s *dg.Session, m *dg.MessageCreate) { if m.Content == "-start" { starter := exec.Command("terraria-server", "start", "aditional", "args") if starter.Run() != nil { s.ChannelMessageSend(m.ChannelID, "Could not start server!") } else { s.ChannelMessageSend(m.ChannelID, "Started server!") } } if m.Content == "-stop" { stopper := exec.Command("terraria-server", "stop", "aditional", "args") if stopper.Run() != nil { s.ChannelMessageSend(m.ChannelID, "Could not stop server!") } else { s.ChannelMessageSend(m.ChannelID, "Stopped server!") } } }) // do not exit c := make(chan struct{}) <-c }
To use this copy the code into a file "main.go" in a directory "mybot". Run "go mod init mybot" and "go build" and you should get a "mybot" executable. Run the executable as "./mybot -t <token>" replacing token with your bot token. Before building, you should replace "-start" and "-stop" with the commands you want people to say in Discord and what is in the exec.Commands with the commands you use to start the server. So if you run the server as
terraria-server start -foo "a b c"
you would write it as
exec.Command("terraria-server", "start", "-foo", "a b c")
Same with stopping it.
Edit: I'm assuming terraria-server daemonizes itself. If not the code would look a bit different.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now