96 lines
2.8 KiB
Lua
96 lines
2.8 KiB
Lua
local addonname, data = ...
|
|
---@cast data HeimdallData
|
|
---@cast addonname string
|
|
|
|
data.Messenger = {}
|
|
function data.Messenger.Init()
|
|
if not data.config.messenger.enabled then return end
|
|
|
|
---@class Message
|
|
---@field message string
|
|
---@field channel string
|
|
---@field data string
|
|
|
|
---@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 data.messenger then data.messenger = {} end
|
|
if not data.messenger.queue then data.messenger.queue = {} end
|
|
if not data.messenger.ticker then
|
|
data.messenger.ticker = C_Timer.NewTicker(0.2, function()
|
|
---@type Message
|
|
local message = data.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(data.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)
|
|
end
|
|
|
|
--C_Timer.NewTicker(2, function()
|
|
-- print("Q")
|
|
-- table.insert(data.messenger.queue, {
|
|
-- channel = "CHANNEL",
|
|
-- data = "Foobar",
|
|
-- message = "TEST"
|
|
-- })
|
|
--end)
|
|
end
|