Refactor messenger to use GetChannelName

This commit is contained in:
2025-01-06 01:58:51 +01:00
parent f6b043fa39
commit 2d3820952f

View File

@@ -10,9 +10,6 @@ function shared.Messenger.Init()
---@field channel string ---@field channel string
---@field data string ---@field data string
---@type table<string, number>
local channelIdMap = {}
local function GetChannelId(channelName) local function GetChannelId(channelName)
local channels = { GetChannelList() } local channels = { GetChannelList() }
for i = 1, #channels, 2 do for i = 1, #channels, 2 do
@@ -25,8 +22,8 @@ function shared.Messenger.Init()
end end
local function FindOrJoinChannel(channelName, password) local function FindOrJoinChannel(channelName, password)
local channelId = GetChannelId(channelName) local channelId = GetChannelName(channelName)
if not channelId then if channelId == 0 then
print("Channel", tostring(channelName), "not found, joining") print("Channel", tostring(channelName), "not found, joining")
if password then if password then
JoinPermanentChannel(channelName, password) JoinPermanentChannel(channelName, password)
@@ -34,20 +31,10 @@ function shared.Messenger.Init()
JoinPermanentChannel(channelName) JoinPermanentChannel(channelName)
end end
end end
channelId = GetChannelId(channelName) channelId = GetChannelName(channelName)
channelIdMap[channelName] = channelId
return channelId return channelId
end 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
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
if not shared.messenger then shared.messenger = {} end if not shared.messenger then shared.messenger = {} end
if not shared.messenger.queue then shared.messenger.queue = {} end if not shared.messenger.queue then shared.messenger.queue = {} end
@@ -60,17 +47,11 @@ function shared.Messenger.Init()
if not message.message or message.message == "" then return end if not message.message or message.message == "" then return end
if not message.channel or message.channel == "" 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 if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
print("Channel presented as string:", message.data) print("Channel presented as string:", message.data)
local channelId = channelIdMap[message.data] local channelId = GetChannelName(message.data)
if not channelId then if channelId == 0 then
print("Channel not found, scanning") print("Channel not found, joining")
ScanChannels()
channelId = channelIdMap[message.data]
end
if not channelId then
print("Channel not joined, joining")
channelId = FindOrJoinChannel(message.data) channelId = FindOrJoinChannel(message.data)
end end
print("Channel resolved to id", channelId) print("Channel resolved to id", channelId)