1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-25 21:29:06 +02:00

Server util - new ways to send messages to discord

This commit is contained in:
Gerkiz 2022-01-14 16:16:21 +01:00
parent 2370cfa38b
commit c39279ffdd

View File

@ -62,6 +62,7 @@ local discord_jailed_embed_tag = '[DISCORD-JAILED-EMBED]'
local discord_unjailed_tag = '[DISCORD-UNJAILED]'
local discord_unjailed_embed_tag = '[DISCORD-UNJAILED-EMBED]'
local discord_admin_raw_tag = '[DISCORD-ADMIN-RAW]'
local discord_embed_parsed_tag = '[DISCORD-EMBED-PARSED]'
local discord_embed_tag = '[DISCORD-EMBED]'
local discord_embed_raw_tag = '[DISCORD-EMBED-RAW]'
local discord_admin_embed_tag = '[DISCORD-ADMIN-EMBED]'
@ -70,6 +71,7 @@ local discord_named_tag = '[DISCORD-NAMED]'
local discord_named_raw_tag = '[DISCORD-NAMED-RAW]'
local discord_named_bold_tag = '[DISCORD-NAMED-BOLD]'
local discord_named_embed_tag = '[DISCORD-NAMED-EMBED]'
local discord_named_embed_parsed_tag = '[DISCORD-NAMED-EMBED-PARSED]'
local discord_named_embed_raw_tag = '[DISCORD-NAMED-EMBED-RAW]'
local start_scenario_tag = '[START-SCENARIO]'
local stop_scenario_tag = '[STOP-SCENARIO]'
@ -103,6 +105,27 @@ local function assert_non_empty_string_and_no_spaces(str, argument_name)
end
end
local function getOnlineAdmins()
local online = game.connected_players
local i = 0
for _, p in pairs(online) do
if p.admin then
i = i + 1
end
end
return i
end
local function build_embed_data()
local d = {
time = Public.format_time(game.ticks_played),
onlinePlayers = #game.connected_players,
totalPlayers = #game.players,
onlineAdmins = getOnlineAdmins()
}
return d
end
--- The event id for the on_server_started event.
-- The event is raised whenever the server goes from the starting state to the running state.
-- It provides a good opportunity to request data from the web server.
@ -181,6 +204,28 @@ function Public.to_discord_named_embed(channel_name, message)
raw_print(concat({discord_named_embed_tag, channel_name, ' ', message}))
end
--- Sends an embed message that is parsed to the named discord channel. The message is sanitized of markdown server side.
-- @param message<string> the content of the embed.
function Public.to_discord_named_parsed_embed(channel_name, message)
assert_non_empty_string_and_no_spaces(channel_name, 'channel_name')
local table_to_json = game.table_to_json
if not type(message) == 'table' then
return
end
if not message.title then
return
end
if not message.description then
return
end
message.channelName = channel_name
raw_print(discord_named_embed_parsed_tag, table_to_json(message))
end
--- Sends an embed message to the named discord channel. The message is not sanitized of markdown.
-- @param message<string> the content of the embed.
function Public.to_discord_named_embed_raw(channel_name, message)
@ -252,6 +297,23 @@ function Public.to_admin_raw(message, locale)
end
end
--- Sends a embed message to the linked discord channel. The message is sanitized/parsed of markdown server side.
-- @param message<table> the content of the embed.
function Public.to_discord_embed_parsed(message)
local table_to_json = game.table_to_json
if not type(message) == 'table' then
return
end
if not message.title then
return
end
if not message.description then
return
end
raw_print(discord_embed_parsed_tag .. table_to_json(message))
end
--- Sends a embed message to the linked discord channel. The message is sanitized of markdown server side.
-- @param message<string> the content of the embed.
-- @param locale<boolean> if the message should be handled as localized.
@ -501,27 +563,6 @@ local function validate_arguments(data_set, key, callback_token)
end
end
local function getOnlineAdmins()
local online = game.connected_players
local i = 0
for _, p in pairs(online) do
if p.admin then
i = i + 1
end
end
return i
end
local function build_embed_data()
local d = {
time = Public.format_time(game.ticks_played),
onlinePlayers = #game.connected_players,
totalPlayers = #game.players,
onlineAdmins = getOnlineAdmins()
}
return d
end
local function send_try_get_data(data_set, key, callback_token)
data_set = double_escape(data_set)
key = double_escape(key)