Load alien/job whitelists from database, if enabled (#17952)

* Load alien/job whitelists from database, if enabled

* SQL Template for Table

* Fixes

* Commands

* .

* .

* fix

* cannot be a list

* .

* .

* embed

* .

* .

* query test

* query test

* query test

* .

* fix
This commit is contained in:
Selis
2025-09-28 10:28:55 +02:00
committed by GitHub
parent ca27d60647
commit 315fbb415f
12 changed files with 421 additions and 354 deletions
+1
View File
@@ -31,6 +31,7 @@ GLOBAL_PROTECT(href_token)
var/datum/filter_editor/filteriffic
var/datum/particle_editor/particle_test
var/datum/whitelist_editor/whitelist_editor
/// A lazylist of tagged datums, for quick reference with the View Tags verb
var/list/tagged_datums
+166 -1
View File
@@ -30,7 +30,7 @@
admin_only = FALSE
/datum/tgs_chat_command/parsetest/Run(datum/tgs_chat_user/sender, params)
return "```You passed:[params]```"
return new /datum/tgs_message_content("```You passed:[params]```")
/datum/tgs_chat_command/staffwho
name = "staffwho"
@@ -378,3 +378,168 @@ GLOBAL_LIST_EMPTY(pending_discord_registrations)
if("dust")
real_target.dust()
return "Smite [smite_name] sent!"
#define VALID_ACTIONS list("add", "remove", "list")
#define VALID_KINDS list("job", "species")
#define VALID_USAGE "whitelist \[[list2text(VALID_ACTIONS, ", ")]\] \[[list2text(VALID_KINDS, ", ")]\] <ckey> (role)"
/datum/tgs_chat_command/whitelist
name = "whitelist"
help_text = "allows the management of player whitelists. Usage: whitelist \[add, remove, list\] \[job, species\] <ckey> (role)"
admin_only = TRUE
/datum/tgs_chat_command/whitelist/Run(datum/tgs_chat_user/sender, params)
var/list/message_as_list = splittext(params, " ")
var/datum/tgs_message_content/message = new("Invalid return message.")
// Check if the command is valid
if(!CONFIG_GET(flag/sql_enabled))
message.text = "```A database is required to be set up for this feature.```"
return message
if(!LAZYLEN(message_as_list))
message.text = "```Invalid command usage: [VALID_USAGE]```"
return message
var/action = message_as_list[1]
if(!(action in VALID_ACTIONS))
message.text = "```First param must be a valid action.```"
return message
message_as_list.Cut(1, 2)
if(!LAZYLEN(message_as_list))
message.text = "```Invalid command usage: [VALID_USAGE]```"
return message
var/kind = message_as_list[1]
if(!(kind in VALID_KINDS))
message.text = "```Second param must be a valid whitelist kind.```"
return message
message_as_list.Cut(1, 2)
if(!LAZYLEN(message_as_list))
message.text = "```Invalid command usage: [VALID_USAGE]```"
return message
var/ckey = message_as_list[1]
if(!istext(ckey))
message.text = "```Third param must be a valid ckey.```"
return message
// Role is not required for listing the roles of a ckey
var/role
if(action != "list")
message_as_list.Cut(1, 2)
if(!LAZYLEN(message_as_list))
message.text = "```Invalid command usage: [VALID_USAGE]```"
return message
role = message_as_list[1]
if(!istext(role))
message.text = "```Fourth param must be a valid whitelist role.```"
return message
// Resolve the action
switch(action)
if("add")
var/datum/db_query/command_add = SSdbcore.NewQuery(
"INSERT INTO [format_table_name("whitelist")] (ckey, kind, entry) VALUES (:ckey, :kind, :entry)",
list("ckey" = ckey, "kind" = kind, "entry" = role)
)
if(!command_add.Execute())
log_sql("Error while trying to add [ckey] to the [role] [kind] whitelist.")
message.text = "Error while trying to add [ckey] to the [role] [kind] whitelist. Please review SQL logs."
return message
qdel(command_add)
var/list/our_whitelists
if(kind == "job")
our_whitelists = GLOB.job_whitelist[ckey]
if(!our_whitelists)
our_whitelists = list()
GLOB.job_whitelist[ckey] = our_whitelists
if(kind == "species")
our_whitelists = GLOB.alien_whitelist[ckey]
if(!our_whitelists)
our_whitelists = list()
GLOB.alien_whitelist[ckey] = our_whitelists
if(our_whitelists)
our_whitelists |= role
if("remove")
var/datum/db_query/command_remove = SSdbcore.NewQuery(
"DELETE FROM [format_table_name("whitelist")] WHERE ckey = :ckey AND kind = :kind AND entry = :entry",
list("ckey" = ckey, "kind" = kind, "entry" = role)
)
if(!command_remove.Execute())
log_sql("Error while trying to remove [ckey] from the [role] [kind] whitelist.")
message.text = "Error while trying to remove [ckey] from the [role] [kind] whitelist. Please review SQL logs."
return message
qdel(command_remove)
var/list/our_whitelists
if(kind == "job")
our_whitelists = GLOB.job_whitelist[ckey]
if(!our_whitelists)
our_whitelists = list()
GLOB.job_whitelist[ckey] = our_whitelists
if(kind == "species")
our_whitelists = GLOB.alien_whitelist[ckey]
if(!our_whitelists)
our_whitelists = list()
GLOB.alien_whitelist[ckey] = our_whitelists
if(our_whitelists && (role in our_whitelists))
our_whitelists -= role
// Listing all whitelists for a specific ckey
if("list")
var/datum/tgs_chat_embed/structure/embed = new()
var/found = FALSE
message.text = ""
message.embed = embed
embed.title = "Whitelists for [ckey]"
var/datum/db_query/query_list = SSdbcore.NewQuery(
"SELECT kind, entry FROM [format_table_name("whitelist")] WHERE ckey = :ckey",
list("ckey" = ckey)
)
if(!query_list.Execute())
log_sql("Error while trying to query whitelists for [ckey].")
embed.description = "Error while trying to query whitelists for [ckey]. Please review SQL logs."
embed.colour = "#FF0000"
return message
else
while(query_list.NextRow())
var/kind_query_result = query_list.item[1]
var/entry_query_result = query_list.item[2]
embed.description += "- [kind_query_result] - [entry_query_result]\n"
found = TRUE
qdel(query_list)
if(!found)
embed.description += "No whitelist entries found for [ckey]"
return message
// Notify the player of the change
var/key_to_find = "[ckey(ckey)]"
if(length(key_to_find))
var/client/user
for(var/client/C in GLOB.clients)
if(C.ckey == key_to_find)
user = C
break
to_chat(user, span_info("You have been [action][action == "remove" ? "d" : "ed"] to the [kind] whitelist of [role] by an administrator."))
// Notify the admins on discord that it was successful
message.text = "\[Whitelist Edit\] [ckey] has been [action][action == "remove" ? "d" : "ed"] to [kind] whitelist: [role]"
log_admin(message.text)
message_admins(message.text)
return message
#undef VALID_USAGE
#undef VALID_KINDS
#undef VALID_ACTIONS