Files
wow-Heimdall/Modules/Inviter.lua
2025-01-06 13:14:13 +01:00

144 lines
4.8 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
---@diagnostic disable-next-line: missing-fields
shared.Inviter = {}
function shared.Inviter.Init()
---@type Timer
local updateTimer = nil
local function FixGroup()
if not IsInRaid() then ConvertToRaid() end
if Heimdall_Data.config.inviter.allAssist then SetEveryoneIsAssistant() end
--shared.dumpTable(Heimdall_Data.config.inviter)
if Heimdall_Data.config.inviter.agentsAssist then
--shared.dumpTable(Heimdall_Data.config.agents)
for name, _ in pairs(Heimdall_Data.config.agents) do
if UnitInParty(name)
and not UnitIsGroupLeader(name)
and not UnitIsRaidOfficer(name) then
print("Promoting " .. name .. " to assistant")
PromoteToAssistant(name, true)
end
end
end
end
local framePool = {}
---@param name string
---Kicking people or othwerise people leaving will fuck up the buttons
---We should make them reactive to GROUP_ROSTER_UPDATE
---But that is currently not trivial and would require some work
---Since I'm the only one currently using this - I don't care where the buttons are
local function OverlayKickButtonElvUI(name)
for group = 1, 8 do
for player = 1, 5 do
local button = _G[string.format("ElvUF_RaidGroup%dUnitButton%d", group, player)]
local unitName = button and button.unit and UnitName(button.unit)
if unitName == name then
local overlayButton = framePool[button.unit] or
CreateFrame("Button",
string.format("HeimdallKickButton%s", button.unit, button, "SecureActionButtonTemplate"))
framePool[button.unit] = overlayButton
overlayButton:SetSize(button.UNIT_WIDTH/2, button.UNIT_HEIGHT/2)
overlayButton:SetPoint("CENTER", button, "CENTER", 0, 0)
overlayButton:SetNormalTexture("Interface\\Buttons\\UI-GroupLoot-KickIcon")
overlayButton:SetHighlightTexture("Interface\\Buttons\\UI-GroupLoot-KickIcon")
overlayButton:SetPushedTexture("Interface\\Buttons\\UI-GroupLoot-KickIcon")
overlayButton:SetDisabledTexture("Interface\\Buttons\\UI-GroupLoot-KickIcon")
overlayButton:SetAlpha(0.5)
overlayButton:Show()
overlayButton:SetScript("OnClick", function()
UninviteUnit(name)
overlayButton:Hide()
end)
-- button:SetAttribute("type", "macro")
-- button:SetAttribute("macrotext", "/kick " .. unit)
return
end
end
end
end
---@type table<string, number>
local groupMembers = {}
local function CleanGroups()
if not Heimdall_Data.config.inviter.kickOffline then return end
print("Cleaning groups")
local now = GetTime()
for i = 1, 40 do
local unit = "raid" .. i
if UnitExists(unit) then
local name = UnitName(unit)
if name then
-- When we load (into game) we want to consider everyone "online"
-- In other words everyone we haven't seen before is "online" the first time we see them
-- This is to avoid kicking people who might not be offline which we don't know because we just logged in
if not groupMembers[name] then
groupMembers[name] = now
else
local online = UnitIsConnected(unit)
if online then
groupMembers[name] = now
end
end
end
end
end
for name, time in pairs(groupMembers) do
if time < now - Heimdall_Data.config.inviter.afkThreshold then
print(string.format("Kicking %s for being offline", name))
-- Blyat this is protected...
-- UninviteUnit(name)
OverlayKickButtonElvUI(name)
end
if not UnitInParty(name) then
print(string.format("%s no longer in party", name))
groupMembers[name] = nil
end
end
end
local function Tick()
CleanGroups()
C_Timer.NewTimer(Heimdall_Data.config.inviter.cleanupInterval, Tick, 1)
end
Tick()
local inviterGroupFrame = CreateFrame("Frame")
inviterGroupFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
inviterGroupFrame:SetScript("OnEvent", function(self, event, ...)
if not Heimdall_Data.config.inviter.enabled then return end
if not UnitIsGroupLeader("player") then return end
if updateTimer then updateTimer:Cancel() end
updateTimer = C_Timer.NewTimer(Heimdall_Data.config.inviter.throttle, FixGroup)
end)
local inviterChannelFrame = CreateFrame("Frame")
inviterChannelFrame:RegisterEvent("CHAT_MSG_CHANNEL")
inviterChannelFrame:SetScript("OnEvent", function(self, event, msg, sender, ...)
if not Heimdall_Data.config.inviter.enabled then return end
local channelId = select(6, ...)
local channelname = ""
---@type any[]
local channels = { GetChannelList() }
for i = 1, #channels, 2 do
---@type number
local id = channels[i]
---@type string
local name = channels[i + 1]
if id == channelId then
channelname = name
end
end
if channelname ~= Heimdall_Data.config.inviter.listeningChannel then return end
if msg == Heimdall_Data.config.inviter.keyword then InviteUnit(sender) end
end)
print("Heimdall - Inviter loaded")
end