Files
wow-Heimdall/Modules/Messenger.lua
2025-01-09 11:20:02 +01:00

129 lines
4.3 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
local ModuleName = "Messenger"
---@diagnostic disable-next-line: missing-fields
shared.Messenger = {}
function shared.Messenger.Init()
---@class Message
---@field message string
---@field channel string
---@field data string
local function FindOrJoinChannel(channelName, password)
local channelId = GetChannelName(channelName)
if channelId == 0 then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel not found, joining: %s", ModuleName, channelName))
end
if password then
JoinPermanentChannel(channelName, password)
else
JoinPermanentChannel(channelName)
end
end
channelId = GetChannelName(channelName)
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel found with ID: %s (%s)", ModuleName, channelId, channelName))
end
return channelId
end
---@diagnostic disable-next-line: missing-fields
if not shared.messenger then shared.messenger = {} end
if not shared.messenger.queue then shared.messenger.queue = {} end
if not shared.messenger.ticker then
local function DoMessage()
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Processing message queue", ModuleName))
end
if not Heimdall_Data.config.messenger.enabled then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Module disabled, skipping message processing", ModuleName))
end
return
end
---@type Message
local message = shared.messenger.queue[1]
if not message then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Message queue empty", ModuleName))
end
return
end
if not message.message or message.message == "" then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Invalid message: empty content", ModuleName))
end
return
end
if not message.channel or message.channel == "" then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Invalid message: no channel specified", ModuleName))
end
return
end
if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Processing channel message: '%s' to '%s'", ModuleName, message.message,
message.data))
end
local channelId = GetChannelName(message.data)
if channelId == 0 then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel not found, attempting to join: %s", ModuleName, message.data))
end
channelId = FindOrJoinChannel(message.data)
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel join result - ID: %s", ModuleName, channelId))
end
end
message.data = tostring(channelId)
end
table.remove(shared.messenger.queue, 1)
if not message.message or message.message == "" then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Skipping empty message", ModuleName))
end
return
end
if not message.channel or message.channel == "" then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Skipping message with no channel", ModuleName))
end
return
end
if not message.data or message.data == "" then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Skipping message with no data", ModuleName))
end
return
end
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Sending message: '%s' to %s:%s", ModuleName, message.message, message.channel,
message.data))
end
SendChatMessage(message.message, message.channel, nil, message.data)
end
local function Tick()
if Heimdall_Data.config.messenger.debug then
local queueSize = #shared.messenger.queue
print(string.format("[%s] Queue check - Messages pending: %d", ModuleName, queueSize))
end
DoMessage()
shared.messenger.ticker = C_Timer.NewTimer(Heimdall_Data.config.messenger.interval, Tick, 1)
end
Tick()
end
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Module initialized with interval: %s", ModuleName,
Heimdall_Data.config.messenger.interval))
end
print("[Heimdall] Messenger loaded")
end