Skip to content

Commit d2a1190

Browse files
committed
feat(guild-helper): update guild rank setting
1 parent c4f3931 commit d2a1190

3 files changed

Lines changed: 108 additions & 16 deletions

File tree

Modules/GuildHelper/Defaults.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ local defaults = {
3838
enabled = true,
3939
hasPublicNote = true,
4040
hasOfficerNote = true,
41-
guildRanks = {}, ---@type string[]
41+
selectedRanks = {}, ---@type string[]
4242
},
4343
rules = {
4444
levelRange = {
@@ -57,7 +57,7 @@ local defaults = {
5757
},
5858
guildRank = {
5959
enabled = true,
60-
ranks = { L["Newcome"] },
60+
selectedRanks = {}, ---@type string[]
6161
},
6262
},
6363
},

Modules/GuildHelper/Kick.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ local function IsProtected(publicNote, officerNote, rankName)
7474
return true
7575
end
7676

77-
if rankName and tContains(protection.guildRanks, rankName) then
78-
return true
77+
if rankName then
78+
local guildName = GetGuildInfo("player")
79+
if guildName then
80+
local fullKey = guildName .. "_" .. rankName
81+
if tContains(protection.selectedRanks, fullKey) then
82+
return true
83+
end
84+
end
7985
end
8086

8187
return false
@@ -91,8 +97,14 @@ end
9197
local function MatchesKickRule(rankName, level, offlineHours, publicNote, officerNote)
9298
local rules = M.profile.kick.rules
9399

94-
if rules.guildRank.enabled and rankName and tContains(rules.guildRank.ranks, rankName) then
95-
return true
100+
if rules.guildRank.enabled and rankName then
101+
local guildName = GetGuildInfo("player")
102+
if guildName then
103+
local fullKey = guildName .. "_" .. rankName
104+
if tContains(rules.guildRank.selectedRanks, fullKey) then
105+
return true
106+
end
107+
end
96108
end
97109

98110
if

Modules/GuildHelper/Setting.lua

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,74 @@ local E, F, L = ns[1], ns[2], ns[3]
33

44
local M = E:Module("Guild Helper") ---@class GuildHelper
55

6+
---Get current guild name
7+
---@return string?
8+
local function getCurrentGuildName()
9+
return (GetGuildInfo("player"))
10+
end
11+
12+
---Build guild rank key in format {GUILD_NAME}_{RANK_NAME}
13+
---@param guildName string
14+
---@param rankName string
15+
---@return string
16+
local function buildGuildRankKey(guildName, rankName)
17+
return guildName .. "_" .. rankName
18+
end
19+
20+
---Set the input as a multiselect for guild ranks
21+
---@param args table The args table to modify
22+
---@param getConfigTable fun(): string[] Function that returns the config table storing "{GUILD_NAME}_{RANK_NAME}" strings
23+
local function setGuildRankMultiselect(args, getConfigTable)
24+
args.type = "multiselect"
25+
args.disabled = function()
26+
return not getCurrentGuildName()
27+
end
28+
args.values = function()
29+
local t = {}
30+
if not getCurrentGuildName() then
31+
return t
32+
end
33+
local numRanks = GuildControlGetNumRanks()
34+
if numRanks and numRanks > 0 then
35+
for i = 1, numRanks do
36+
local rankName = GuildControlGetRankName(i)
37+
if rankName and rankName ~= "" then
38+
t[rankName] = rankName
39+
end
40+
end
41+
end
42+
return t
43+
end
44+
args.get = function(_, key)
45+
local guildName = getCurrentGuildName()
46+
if not guildName then
47+
return false
48+
end
49+
local fullKey = buildGuildRankKey(guildName, key)
50+
return tContains(getConfigTable(), fullKey)
51+
end
52+
args.set = function(_, key, value)
53+
local guildName = getCurrentGuildName()
54+
if not guildName then
55+
return
56+
end
57+
local tbl = getConfigTable()
58+
local fullKey = buildGuildRankKey(guildName, key)
59+
if value then
60+
if not tContains(tbl, fullKey) then
61+
table.insert(tbl, fullKey)
62+
end
63+
else
64+
for i, v in ipairs(tbl) do
65+
if v == fullKey then
66+
table.remove(tbl, i)
67+
break
68+
end
69+
end
70+
end
71+
end
72+
end
73+
674
local settings = {
775
ui = {
876
order = 11,
@@ -212,9 +280,15 @@ local settings = {
212280
},
213281
guildRanks = {
214282
order = 4,
215-
type = "input",
216-
name = L["Protected Guild Ranks"],
217-
desc = L["Guild rank names to protect, separated by commas."],
283+
name = function()
284+
local guildName = getCurrentGuildName()
285+
return string.format(
286+
"%s (%s)",
287+
L["Protected Guild Ranks"],
288+
guildName or F.Color.String(L["You are not in a guild"], "rose-600")
289+
)
290+
end,
291+
desc = L["Select guild ranks to protect from being kicked."],
218292
width = "full",
219293
hidden = function()
220294
return not M.profile.kick.protection.enabled
@@ -425,9 +499,15 @@ local settings = {
425499
},
426500
ranks = {
427501
order = 2,
428-
type = "input",
429-
name = L["Guild Ranks"],
430-
desc = L["Guild rank names to target, separated by commas."],
502+
name = function()
503+
local guildName = getCurrentGuildName()
504+
return string.format(
505+
"%s (%s)",
506+
L["Guild Ranks"],
507+
guildName or F.Color.String(L["You are not in a guild"], "rose-600")
508+
)
509+
end,
510+
desc = L["Select guild ranks to apply kick rules."],
431511
width = "full",
432512
hidden = function()
433513
return not M.profile.kick.rules.guildRank.enabled
@@ -469,16 +549,16 @@ E:SetStringListSetting(settings.invite.args.channels, function()
469549
return M.profile.invite.channels
470550
end)
471551

472-
E:SetStringListSetting(settings.kick.args.protection.args.guildRanks, function()
473-
return M.profile.kick.protection.guildRanks
552+
setGuildRankMultiselect(settings.kick.args.protection.args.guildRanks, function()
553+
return M.profile.kick.protection.selectedRanks
474554
end)
475555

476556
E:SetStringListSetting(settings.kick.args.rules.args.noteMark.args.patterns, function()
477557
return M.profile.kick.rules.noteMark.patterns
478558
end)
479559

480-
E:SetStringListSetting(settings.kick.args.rules.args.guildRank.args.ranks, function()
481-
return M.profile.kick.rules.guildRank.ranks
560+
setGuildRankMultiselect(settings.kick.args.rules.args.guildRank.args.ranks, function()
561+
return M.profile.kick.rules.guildRank.selectedRanks
482562
end)
483563

484564
M:SetSettings(settings)

0 commit comments

Comments
 (0)