Jump to content

Need help for Discord bot

Go to solution Solved by vlads_,
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.

Hi! I have a Terraria (and Minecraft, but that's not relevant since I don't have an especific command to start that server) server setup on a VM on Google Cloud running Ubuntu. I have a little script that initiates the server by entering a command. The thing is that I had an idea. What if my friends could start and stop the server by putting a command on a Discord chat? I've seen bots do things when entering a command like !play (whatever music) and things like that, but I don't really know if they are capable of performing an externela action like this. I don't know either if the Google Cloud SDK has any support for something like this. If anyone would like to help, anything is welcome! I would like to learn from this and any ideas (even if they're wrong or are not exactly what I'm asking) are veeery welcome.

Thank you all in advance!

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't worked with Google Cloud before, but the bot can stop the server by just running 'shutdown' as root, presumably.

 

As for staring it back up, that doesn't sound doable, since if the server is shutdown, there is no bot to listen for commands, is there?

The Eight Fallacies of Distributed Computing

Essentially everyone, when they first build a distributed application, makes the following eight assumptions. All prove to be false in the long run and all cause big trouble and painful learning experiences.

  1. The network is reliable
  2. Latency is zero
  3. Bandwidth is infinite
  4. The network is secure
  5. Topology doesn’t change
  6. There is one administrator
  7. Transport cost is zero
  8. The network is homogeneous

        — Peter Deutsch

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vlads_ said:

I haven't worked with Google Cloud before, but the bot can stop the server by just running 'shutdown' as root, presumably.

 

As for staring it back up, that doesn't sound doable, since if the server is shutdown, there is no bot to listen for commands, is there?

i believe the term "server" means the Terraria server and not the host VM.

You can make a discord bot in a number of languages and seeing as i don't know what ones you know, This tutorial from real python is a very good python one.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Edited by vlads_

The Eight Fallacies of Distributed Computing

Essentially everyone, when they first build a distributed application, makes the following eight assumptions. All prove to be false in the long run and all cause big trouble and painful learning experiences.

  1. The network is reliable
  2. Latency is zero
  3. Bandwidth is infinite
  4. The network is secure
  5. Topology doesn’t change
  6. There is one administrator
  7. Transport cost is zero
  8. The network is homogeneous

        — Peter Deutsch

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×