1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-03-03 14:53:01 +02:00

Merge pull request #1162 from grilledham/discord_report_player_time

Add Player online time to discord report/jail embeds.
This commit is contained in:
Jayefuu 2021-02-01 15:14:41 +00:00 committed by GitHub
commit 902fe0d81c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 19 deletions

View File

@ -153,34 +153,38 @@ local function send_report_to_discord(reporting_player, reported_player, message
local server_id = Server.get_server_id()
local server_name = Server.get_server_name()
local text = {}
local text = {'**'}
if reporting_player and reporting_player.valid then
text[#text + 1] = reporting_player.name
text[#text + 1] = Utils.sanitise_string_for_discord(reporting_player.name)
else
text[#text + 1] = '<script>'
end
text[#text + 1] = ' reported '
text[#text + 1] = reported_player.name
text[#text + 1] = Utils.sanitise_string_for_discord(reported_player.name)
text[#text + 1] = '**\\n'
if server_id ~= '' then
text[#text + 1] = ' s'
text[#text + 1] = server_id
text[#text + 1] = '-'
text[#text + 1] = server_name
text[#text + 1] = 'Server: s'
text[#text + 1] = Utils.sanitise_string_for_discord(server_id)
text[#text + 1] = ' - '
text[#text + 1] = Utils.sanitise_string_for_discord(server_name)
text[#text + 1] = '\\n'
end
text[#text + 1] = ' Game time '
text[#text + 1] = ' Game time: '
text[#text + 1] = Utils.format_time(game.tick)
text[#text + 1] = ':\\n\\n'
text[#text + 1] = message
text[#text + 1] = '\\nPlayer online time: '
text[#text + 1] = Utils.format_time(reported_player.online_time)
text[#text + 1] = '\\nMessage: '
text[#text + 1] = Utils.sanitise_string_for_discord(message)
text = table.concat(text)
Server.to_discord_named_embed(helpdesk_channel, text)
Server.to_discord_named_embed_raw(helpdesk_channel, text)
Server.to_discord_named_raw(helpdesk_channel, moderator_role_mention)
Server.to_discord_named_embed(moderation_log_channel, text)
Server.to_discord_named_embed_raw(moderation_log_channel, text)
end
function Module.report(reporting_player, reported_player, message)
@ -221,20 +225,27 @@ local function send_jail_to_discord(target_player, player)
local server_id = Server.get_server_id()
local server_name = Server.get_server_name()
local text = {target_player.name, ' has been jailed by ', player.name}
local text = {'**'}
text[#text+ 1] = Utils.sanitise_string_for_discord(player.name)
text[#text+ 1] = ' has jailed '
text[#text+ 1] = Utils.sanitise_string_for_discord(target_player.name)
text[#text+ 1] = '**\\n'
if server_id ~= '' then
text[#text + 1] = ' s'
text[#text + 1] = server_id
text[#text + 1] = '-'
text[#text + 1] = server_name
text[#text + 1] = 'Server: s'
text[#text + 1] = Utils.sanitise_string_for_discord(server_id)
text[#text + 1] = ' - '
text[#text + 1] = Utils.sanitise_string_for_discord(server_name)
text[#text + 1] = '\\n'
end
text[#text + 1] = ' Game time '
text[#text + 1] = 'Game time: '
text[#text + 1] = Utils.format_time(game.tick)
text[#text + 1] = '\\nPlayer online time: '
text[#text + 1] = Utils.format_time(target_player.online_time)
local message = table.concat(text)
Server.to_discord_named_embed(moderation_log_channel, message)
Server.to_discord_named_embed_raw(moderation_log_channel, message)
end
--- Places a target in jail

View File

@ -256,6 +256,29 @@ function Module.validate_player(player_ident)
return player, player.name, player.index
end
local non_breaking_space = '' -- This is \u200B an invisible space charcater.
local sensitive_characters_map = {
['\\'] = '\\\\',
['*'] = '\\*',
['_'] = '\\_',
['~'] = '\\~',
['`'] = '\\`',
['|'] = '\\|',
['>'] = '\\>',
['@'] = '@' .. non_breaking_space
}
--- Escapes markdown characters mentions. Intended to make it safe to send user input to discord.
-- @param message <string>
-- @return <string>
function Module.sanitise_string_for_discord(message)
for character, replace in pairs(sensitive_characters_map) do
message = string.gsub(message, character, replace)
end
return message
end
-- add utility functions that exist in base factorio/util
require 'util'

25
utils/core_tests.lua Normal file
View File

@ -0,0 +1,25 @@
local Declare = require 'utils.test.declare'
local Assert = require 'utils.test.assert'
local Core = require 'utils.core'
local non_breaking_space = '' -- This is \u200B an invisible space charcater.
Declare.module({'utils', 'Core'}, function()
Declare.module('sanitise_string_for_discord', function()
Declare.test('escapes markdown', function()
local actual = Core.sanitise_string_for_discord('**a**_b_~c~`d`|e|>f')
Assert.equal('\\*\\*a\\*\\*\\_b\\_\\~c\\~\\`d\\`\\|e\\|\\>f', actual)
end)
-- This test is making sure backslash '\' is escaped first, else there would be a different number of backslashes.
Declare.test('escapes backslash', function()
local actual = Core.sanitise_string_for_discord('\\*abc\\*')
Assert.equal('\\\\\\*abc\\\\\\*', actual)
end)
Declare.test('escapes mention', function()
local actual = Core.sanitise_string_for_discord('@grilledham')
Assert.equal('@' .. non_breaking_space .. 'grilledham', actual)
end)
end)
end)