From 1d45bd4d4093c4201b1e3fe838bf868eaa365673 Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Thu, 26 Dec 2019 00:23:58 +0100 Subject: [PATCH 1/3] Mcping command --- handlers/commands.go | 7 ++++++- handlers/mcping/mcping.go | 26 ++++++++++++++++++++++++++ handlers/mcping/mcping.py | 10 ++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 handlers/mcping/mcping.go create mode 100644 handlers/mcping/mcping.py diff --git a/handlers/commands.go b/handlers/commands.go index 0ddc980..76b9779 100644 --- a/handlers/commands.go +++ b/handlers/commands.go @@ -4,6 +4,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/lordralex/absol/handlers/alert" "github.com/lordralex/absol/handlers/factoids" + "github.com/lordralex/absol/handlers/mcping" "github.com/lordralex/absol/handlers/twitch" "github.com/lordralex/absol/logger" "github.com/spf13/viper" @@ -68,10 +69,14 @@ func OnMessageCommand(ds *discordgo.Session, mc *discordgo.MessageCreate) { { factoids.RunCommand(ds, mc, c, cmd, args) } + case "mcping": + { + mcping.RunCommand(ds, mc, c, cmd, args) + } default: { //at this point, let's just go with it's a factoid - newargs := make([]string, len(args) + 1) + newargs := make([]string, len(args)+1) newargs[0] = cmd for k, v := range args { newargs[k+1] = v diff --git a/handlers/mcping/mcping.go b/handlers/mcping/mcping.go new file mode 100644 index 0000000..1673d71 --- /dev/null +++ b/handlers/mcping/mcping.go @@ -0,0 +1,26 @@ +package mcping + +import ( + "github.com/bwmarrin/discordgo" + "github.com/lordralex/absol/logger" + "os/exec" +) + +func RunCommand(ds *discordgo.Session, mc *discordgo.MessageCreate, c *discordgo.Channel, cmd string, args []string) { + if len(args) == 0 { + return + } + + server := args[0] + + pythonCmd := exec.Command("python", "handlers/mcping/mcping.py", server) + out, err := pythonCmd.Output() + + if err != nil { + _, err = ds.ChannelMessageSend(c.ID, "Failed to run script") + logger.Err().Printf("Failed to run script\n%s", err) + return + } + + _, err = ds.ChannelMessageSend(c.ID, string(out)) +} diff --git a/handlers/mcping/mcping.py b/handlers/mcping/mcping.py new file mode 100644 index 0000000..66985ec --- /dev/null +++ b/handlers/mcping/mcping.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +from mcstatus import MinecraftServer +import sys + +try: + server = MinecraftServer.lookup(sys.argv[1]) + status = server.status() + print("The server is running {2}, has {0} players, replied in {1} ms".format(status.players.online, status.latency, status.version.name)) +except Exception as e: + print("Error: {0}".format(str(e))) \ No newline at end of file From 94fce529a2479b912471609c258530d36b0297e5 Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Thu, 26 Dec 2019 01:00:36 +0100 Subject: [PATCH 2/3] Modify dockerfile --- Dockerfile | 4 +++- handlers/mcping/mcping.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7dce5b8..45b76e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,8 +11,10 @@ RUN go install -v ### # Now generate our smaller image ### -FROM alpine +FROM jfloff/alpine-python +RUN pip install mcstatus COPY --from=builder /go/bin/absol /go/bin/absol +COPY --from=builder /build/handlers/mcping/mcping.py /go/bin/mcping.py CMD ["/go/bin/absol"] diff --git a/handlers/mcping/mcping.go b/handlers/mcping/mcping.go index 1673d71..2b4e294 100644 --- a/handlers/mcping/mcping.go +++ b/handlers/mcping/mcping.go @@ -13,7 +13,7 @@ func RunCommand(ds *discordgo.Session, mc *discordgo.MessageCreate, c *discordgo server := args[0] - pythonCmd := exec.Command("python", "handlers/mcping/mcping.py", server) + pythonCmd := exec.Command("python", "mcping.py", server) out, err := pythonCmd.Output() if err != nil { From f403185297b98bf70a2eb8f114790b65722d6f0f Mon Sep 17 00:00:00 2001 From: Uriel Salischiker Date: Thu, 26 Dec 2019 13:29:51 +0100 Subject: [PATCH 3/3] Use offical python alpine image --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 45b76e7..b4a7c72 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ RUN go install -v ### # Now generate our smaller image ### -FROM jfloff/alpine-python +FROM python:3-alpine RUN pip install mcstatus COPY --from=builder /go/bin/absol /go/bin/absol