Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mcping command #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ RUN go install -v
###
# Now generate our smaller image
###
FROM alpine
FROM python:3-alpine

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"]
7 changes: 6 additions & 1 deletion handlers/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions handlers/mcping/mcping.go
Original file line number Diff line number Diff line change
@@ -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", "mcping.py", server)
out, err := pythonCmd.Output()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use CombinedOutput since this doesn't capture stderr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script doesnt seem to use stderr, should I still use that?


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))
}
10 changes: 10 additions & 0 deletions handlers/mcping/mcping.py
Original file line number Diff line number Diff line change
@@ -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)))