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