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)
@ -53,46 +56,49 @@ MinecraftSubCommandHandler:AddSubCommand(MinecraftStatusSubCommand.name, functio
return Interaction:reply('There is no data for this Discord server', true) return Interaction:reply('There is no data for this Discord server', true)
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
else else
table.insert(Players, 'No players online') table.insert(Players, 'No players online')
end end
if AnonymousPlayers>0 then if AnonymousPlayers > 0 then
for AnonymousPlayerIndex = 1, AnonymousPlayers do for AnonymousPlayerIndex = 1, AnonymousPlayers do
table.insert(Players, 'Anonymous Player') table.insert(Players, 'Anonymous Player')
end end
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 }
}, },
color = COLOURS.GREEN color = COLOURS.GREEN
} }
else else
EmbedData = { EmbedData = {
title = 'Server Status for '..ServerIPStr, title = 'Server Status for ' .. ServerIPStr,
description = 'Server is offline', description = 'Server is offline',
color = COLOURS.RED color = COLOURS.RED
} }
end end
return Interaction:reply({embed = EmbedData}) return Interaction:reply({ embed = EmbedData })
end) end)
MinecraftSubCommandHandler:AddSubCommand(MinecraftSetIpSubCommand.name, function(Interaction, Command, Args) MinecraftSubCommandHandler:AddSubCommand(MinecraftSetIpSubCommand.name, function(Interaction, Command, Args)
@ -107,18 +113,18 @@ MinecraftSubCommandHandler:AddSubCommand(MinecraftSetIpSubCommand.name, function
if not ServerIP then if not ServerIP then
return Interaction:reply('Invalid server IP') return Interaction:reply('Invalid server IP')
end end
local ServerPort = ServerIPStr:match(ServerIP..':(%d+)') or 25565 local ServerPort = ServerIPStr:match(ServerIP .. ':(%d+)') or 25565
local GuildMinecraftData = {IP = ServerIP, PORT = ServerPort} local GuildMinecraftData = { IP = ServerIP, PORT = ServerPort }
local GlobalMinecraftData = json.decode(io.open('minecraft_data.json','r'):read('*a')) local GlobalMinecraftData = json.decode(io.open('minecraft_data.json', 'r'):read('*a'))
GlobalMinecraftData[GuildId] = GuildMinecraftData GlobalMinecraftData[GuildId] = GuildMinecraftData
io.open('minecraft_data.json','w+'):write(json.encode(GlobalMinecraftData)):close() io.open('minecraft_data.json', 'w+'):write(json.encode(GlobalMinecraftData)):close()
return Interaction:reply('Successfully added `'..ServerIP..':'..ServerPort..'` for ServerId='..GuildId) return Interaction:reply('Successfully added `' .. ServerIP .. ':' .. ServerPort .. '` for ServerId=' .. GuildId)
end) end)
return { return {
Command = MinecraftMainCommand, Command = MinecraftMainCommand,
Callback = MinecraftSubCommandHandler:GetMainCommandCallback() Callback = MinecraftSubCommandHandler:GetMainCommandCallback()
} }