Minecraft user command: Format + fix

This commit is contained in:
2025-06-24 00:11:33 -04:00
parent 54d588b43a
commit 7b3ab8e115

View File

@ -1,6 +1,7 @@
local Discordia = require('discordia')
local json = require('json')
local http_request = require('../Modules/http.lua')
local HttpRequest = require('../Modules/HttpRequest.lua')
local Request = HttpRequest.Request
local SubCommandHandler = require('../Modules/SubCommandHandler.lua')
Discordia.extensions()
@ -12,8 +13,10 @@ local MinecraftSubCommandHandler = SubCommandHandler.new()
local MinecraftMainCommand = SlashCommandTools.slashCommand('minecraft', 'Minecraft server related commands')
local MinecraftStatusSubCommand = SlashCommandTools.subCommand('status', 'Get the Minecraft server status according to the preferred IP address set for this server')
local MinecraftSetIpSubCommand = SlashCommandTools.subCommand('setip', 'Set the preferred Minecraft server IP address for this server')
local MinecraftStatusSubCommand = SlashCommandTools.subCommand('status',
'Get the Minecraft server status according to the preferred IP address set for this server')
local MinecraftSetIpSubCommand = SlashCommandTools.subCommand('setip',
'Set the preferred Minecraft server IP address for this server')
local MinecraftSetIpOptions = SlashCommandTools.string('ip', 'The IP address of the server')
MinecraftSetIpOptions:setRequired(true)
@ -54,17 +57,20 @@ MinecraftSubCommandHandler:AddSubCommand(MinecraftStatusSubCommand.name, functio
end
local ServerIPStr = ServerMinecraftData.IP .. ':' .. ServerMinecraftData.PORT
local Response, Headers = http_request('GET', ('https://api.mcsrvstat.us/3/%s'):format(ServerIPStr))
local IsOnline = Response.online
local Headers, Body = Request("GET", ('https://api.mcsrvstat.us/3/%s'):format(ServerIPStr), nil,
{ ["User-Agent"] = "tommy-bot/1.0 Main-Release" })
if not Headers.code == 200 then
return error("Something went wrong")
end
local IsOnline = Body.online
local EmbedData
if IsOnline then
local MaxPlayers = Response.players.max
local OnlinePlayers = Response.players.online
local MaxPlayers = Body.players.max
local OnlinePlayers = Body.players.online
local AnonymousPlayers = OnlinePlayers
local Players = {}
if OnlinePlayers > 0 then
for PlayerIndex, PlayerData in next, Response.players.list do
for PlayerIndex, PlayerData in next, Body.players.list do
table.insert(Players, PlayerData.name)
AnonymousPlayers = AnonymousPlayers - 1
end
@ -78,7 +84,7 @@ MinecraftSubCommandHandler:AddSubCommand(MinecraftStatusSubCommand.name, functio
end
EmbedData = {
title = 'Server Status for ' .. ServerIPStr,
description = Response.motd.clean[1]..' ('..Response.version..')',
description = Body.motd.clean[1] .. ' (' .. Body.version .. ')',
fields = {
{ name = 'Players', value = OnlinePlayers .. '/' .. MaxPlayers, inline = true },
{ name = 'List of players', value = table.concat(Players, '\n'), inline = true }