148 lines
4.4 KiB
Lua
148 lines
4.4 KiB
Lua
local addonname, shared = ...
|
|
---@cast addonname string
|
|
|
|
local function GetOrDefault(table, keys, default)
|
|
local value = default
|
|
if not table then return value end
|
|
if not keys then return value end
|
|
|
|
local traverse = table
|
|
for i = 1, #keys do
|
|
local key = keys[i]
|
|
if traverse[key] ~= nil then
|
|
traverse = traverse[key]
|
|
else
|
|
break
|
|
end
|
|
|
|
if i == #keys then
|
|
value = traverse
|
|
end
|
|
end
|
|
return value
|
|
end
|
|
|
|
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
|
|
|
|
---@class YapperConfig
|
|
---@field message string
|
|
---@field interval number
|
|
---@field channel string
|
|
---@field channelData any
|
|
---@field enabled boolean
|
|
if not YapperConfig then YapperConfig = {} end
|
|
|
|
---@cast shared { config: YapperConfig}
|
|
|
|
shared.config = {
|
|
message = GetOrDefault(YapperConfig, { "message" }, ""),
|
|
interval = GetOrDefault(YapperConfig, { "interval" }, 1),
|
|
channel = GetOrDefault(YapperConfig, { "channel" }, "SAY"),
|
|
channelData = GetOrDefault(YapperConfig, { "channelData" }, nil),
|
|
enabled = GetOrDefault(YapperConfig, { "enabled" }, false),
|
|
}
|
|
local timer = nil
|
|
|
|
local function Yap()
|
|
if not shared.config.enabled then return end
|
|
if not shared.config.message then
|
|
print(string.format("Yapper found no message to yap (%s)", tostring(shared.config.message)))
|
|
shared.config.enabled = false
|
|
return
|
|
end
|
|
if not shared.config.channel then
|
|
print(string.format("Yapper found no channel to yap in (%s)", tostring(shared.config.channel)))
|
|
shared.config.enabled = false
|
|
return
|
|
end
|
|
|
|
local data = shared.config.channelData
|
|
if shared.config.channel == "CHANNEL" and not tonumber(shared.config.channelData) then
|
|
data = GetChannelId(shared.config.channelData)
|
|
if data == nil then
|
|
print(string.format("Yapper found no channel id for %s", shared.config.channelData))
|
|
shared.config.enabled = false
|
|
return
|
|
end
|
|
end
|
|
SendChatMessage(shared.config.message, shared.config.channel, nil, data)
|
|
|
|
if not shared.config.interval or shared.config.interval <= 0 then
|
|
print("Yapper has no interval set, quitting (%s)", tostring(shared.config.interval))
|
|
shared.config.enabled = false
|
|
return
|
|
end
|
|
timer = C_Timer.NewTimer(shared.config.interval, Yap)
|
|
end
|
|
|
|
local function init()
|
|
if not shared.config.enabled then return end
|
|
if timer and not timer:IsCancelled() then timer:Cancel() end
|
|
Yap()
|
|
end
|
|
|
|
local loadedFrame = CreateFrame("Frame")
|
|
loadedFrame:RegisterEvent("ADDON_LOADED")
|
|
loadedFrame:SetScript("OnEvent", function(self, event, addonName)
|
|
if addonName == addonname then
|
|
init()
|
|
end
|
|
end)
|
|
|
|
local logoutFrame = CreateFrame("Frame")
|
|
logoutFrame:RegisterEvent("PLAYER_LOGOUT")
|
|
logoutFrame:SetScript("OnEvent", function(self, event)
|
|
shared.config.enabled = false
|
|
end)
|
|
|
|
SlashCmdList["YAPPER_MAIN_CMD"] = function(input)
|
|
if input == "" or input == nil then
|
|
shared.config.enabled = not shared.config.enabled
|
|
print(string.format("Yapper is now %s", shared.config.enabled and "enabled" or "disabled"))
|
|
init()
|
|
return
|
|
end
|
|
|
|
if tonumber(input) then
|
|
shared.config.interval = tonumber(input)
|
|
print(string.format("Yapper interval set to %d", shared.config.interval))
|
|
init()
|
|
return
|
|
end
|
|
|
|
shared.config.message = input
|
|
print(string.format("Yapper message set to %s", shared.config.message))
|
|
init()
|
|
end
|
|
SLASH_YAPPER_MAIN_CMD1 = "/yap"
|
|
|
|
SlashCmdList["YAPPER_CHANNEL_CMD"] = function(input)
|
|
if input == "" or input == nil then
|
|
shared.config.channel = nil
|
|
shared.config.channelData = nil
|
|
print("Yapper channel set to nil")
|
|
return
|
|
end
|
|
|
|
local data = { strsplit(" ", input) }
|
|
if #data == 1 then
|
|
shared.config.channel = data[1]
|
|
shared.config.channelData = nil
|
|
print(string.format("Yapper channel set to %s", shared.config.channel))
|
|
return
|
|
end
|
|
shared.config.channel = data[1]
|
|
shared.config.channelData = data[2]
|
|
print(string.format("Yapper channel set to %s with data %s", shared.config.channel, shared.config.channelData))
|
|
end
|
|
SLASH_YAPPER_CHANNEL_CMD1 = "/yapc"
|