Files
wow-Heimdall/Modules/Messenger.lua
2025-01-01 15:00:08 +01:00

103 lines
2.9 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
---@diagnostic disable-next-line: missing-fields
shared.Messenger = {}
function shared.Messenger.Init()
---@class Message
---@field message string
---@field channel string
---@field data string|number
---@type table<string, number>
local channelIdMap = {}
local FindOrJoinChannel = function(channelName, password)
local function GetChannelId(channelName)
local channels = { GetChannelList() }
for i = 1, #channels, 2 do
local id = channels[i]
local name = channels[i + 1]
if name == channelName then
return id
end
end
end
local channelId = GetChannelId(channelName)
if not channelId then
print("Channel", tostring(channelName), "not found, joining")
if password then
JoinPermanentChannel(channelName, password)
else
JoinPermanentChannel(channelName)
end
end
channelId = GetChannelId(channelName)
channelIdMap[channelName] = channelId
return channelId
end
local ScanChannels = function()
local channels = { GetChannelList() }
for i = 1, #channels, 2 do
local id = channels[i]
local name = channels[i + 1]
channelIdMap[name] = id
end
end
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 not Heimdall_Data.config.messenger.enabled then return end
---@type Message
local message = shared.messenger.queue[1]
if not message then return end
if not message.message or message.message == "" then return end
if not message.channel or message.channel == "" then return end
-- Map channel names to ids
if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
print("Channel presented as string:", message.data)
local channelId = channelIdMap[message.data]
if not channelId then
print("Channel not found, scanning")
ScanChannels()
channelId = channelIdMap[message.data]
end
if not channelId then
print("Channel not joined, joining")
channelId = FindOrJoinChannel(message.data)
end
print("Channel resolved to id", channelId)
message.data = channelId
end
table.remove(shared.messenger.queue, 1)
if not message.message or message.message == "" then return end
if not message.channel or message.channel == "" then return end
if not message.data or message.data == "" then return end
SendChatMessage(message.message, message.channel, nil, message.data)
end
local function Tick()
DoMessage()
shared.messenger.ticker = C_Timer.NewTimer(Heimdall_Data.config.messenger.interval, Tick, 1)
end
Tick()
end
--C_Timer.NewTicker(2, function()
-- print("Q")
-- table.insert(data.messenger.queue, {
-- channel = "CHANNEL",
-- data = "Foobar",
-- message = "TEST"
-- })
--end)
print("Heimdall - Messenger loaded")
end