diff --git a/Config.lua b/Config.lua index 74e93e6..8988dc6 100644 --- a/Config.lua +++ b/Config.lua @@ -1,44 +1,29 @@ --- upvalue the globals -local _G = getfenv(0) -local LibStub = _G.LibStub -local GetAddOnMetadata = _G.GetAddOnMetadata or C_AddOns.GetAddOnMetadata -local coroutine = _G.coroutine - local addonName = ... -local IOW = LibStub("AceAddon-3.0"):GetAddon(addonName); -if not IOW then return end -IOW.Config = IOW.Config or {} -local Config = IOW.Config +---@class (partial) InviteOnWhisper +local IOW = LibStub("AceAddon-3.0"):GetAddon(addonName) +if not IOW then return end -Config.version = GetAddOnMetadata(addonName, "Version") or "" +local Config = {} +IOW.Config = Config -local function getCounter(start, increment) - start = start or 1 - increment = increment or 1 - return coroutine.wrap(function() - local count = start - while true do - count = count + increment - coroutine.yield(count) - end - end) -end +Config.version = C_AddOns.GetAddOnMetadata(addonName, "Version") or "" +Config.name = "Invite On Whisper"; function Config:GetOptions() - local orderCount = getCounter() + local increment = CreateCounter() local options = { type = 'group', - get = function(info) return Config:GetConfig(info[#info]); end, - set = function(info, value) return Config:SetConfig(info[#info], value); end, + get = function(info) return self:GetConfig(info[#info]) end, + set = function(info, value) return self:SetConfig(info[#info], value) end, args = { version = { - order = orderCount(), + order = increment(), type = "description", name = "Version: " .. self.version }, confirm = { - order = orderCount(), + order = increment(), name = "Confirmation", desc = "Asks for confirmation for group invites", descStyle = 'inline', @@ -46,28 +31,28 @@ function Config:GetOptions() type = "toggle", }, triggerOutgoingGInv = { - order = orderCount(), + order = increment(), name = "Trigger on outgoing whispers for Guild invites", descStyle = 'inline', width = "full", type = "toggle", }, triggerOutgoingInv = { - order = orderCount(), + order = increment(), name = "Trigger on outgoing whispers for Group invites", descStyle = 'inline', width = "full", type = "toggle", }, keywordMatchMiddle = { - order = orderCount(), + order = increment(), name = "Toggle Smart Match", desc = "Smart Match will search your received whispers for an invite keyword. If any invite keyword is found in the whisper, it will trigger an invite. For example, the 'invite' keyword would then be triggered from \"Invite please?\"", width = "full", type = "toggle", }, addGuildInviteTrigger = { - order = orderCount(), + order = increment(), type = "input", name = "Add Guild invite trigger phrase", set = function(_, phrase) @@ -75,7 +60,7 @@ function Config:GetOptions() end, }, removeGuildInviteTrigger = { - order = orderCount(), + order = increment(), type = "select", style = "dropdown", name = "Remove Guild invite trigger phrase", @@ -88,13 +73,13 @@ function Config:GetOptions() end return tempTable end, - get = function(_, _) return false end, - set = function(_, phrase, ...) + get = function() return false end, + set = function(_, phrase) IOW.DB.ginv[phrase] = nil end, }, addGroupInviteTrigger = { - order = orderCount(), + order = increment(), type = "input", name = "Add Group invite trigger phrase", set = function(_, phrase) @@ -102,7 +87,7 @@ function Config:GetOptions() end, }, removeGroupInviteTrigger = { - order = orderCount(), + order = increment(), type = "select", style = "dropdown", name = "Remove Group invite trigger phrase", @@ -115,7 +100,7 @@ function Config:GetOptions() end return tempTable end, - get = function(_, _) return false end, + get = function() return false end, set = function(_, phrase, ...) IOW.DB.inv[phrase] = nil end, @@ -128,19 +113,19 @@ end function Config:Initialize() self:RegisterOptions() - LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Invite On Whisper", "Invite On Whisper") + LibStub("AceConfigDialog-3.0"):AddToBlizOptions(self.name, self.name) end function Config:RegisterOptions() - LibStub("AceConfig-3.0"):RegisterOptionsTable("Invite On Whisper", self:GetOptions()) + LibStub("AceConfig-3.0"):RegisterOptionsTable(self.name, self:GetOptions()) end function Config:OpenConfig() - Settings.OpenToCategory('Invite On Whisper') + Settings.OpenToCategory(self.name) end function Config:GetConfig(property) - return IOW.DB[property]; + return IOW.DB[property] end function Config:SetConfig(property, value) diff --git a/InviteOnWhisper.lua b/InviteOnWhisper.lua index c48054a..c5823e0 100644 --- a/InviteOnWhisper.lua +++ b/InviteOnWhisper.lua @@ -1,17 +1,7 @@ --- upvalue the globals -local _G = getfenv(0) -local LibStub = _G.LibStub -local pairs = _G.pairs -local GuildInvite = _G.C_GuildInfo.Invite or _G.GuildInvite -local InviteUnit = _G.C_PartyInfo.InviteUnit or _G.InviteUnit -local C_BattleNet = _G.C_BattleNet -local StaticPopupDialogs = _G.StaticPopupDialogs -local StaticPopup_Show = _G.StaticPopup_Show - local addonName = ... -local IOW = LibStub('AceAddon-3.0'):NewAddon(addonName, 'AceConsole-3.0', 'AceHook-3.0', 'AceEvent-3.0'); -if not IOW then return end +---@class InviteOnWhisper: AceAddon, AceConsole-3.0, AceHook-3.0, AceEvent-3.0 +local IOW = LibStub('AceAddon-3.0'):NewAddon(addonName, 'AceConsole-3.0', 'AceHook-3.0', 'AceEvent-3.0') function IOW:OnInitialize() IOWDB = IOWDB or {} @@ -39,7 +29,7 @@ function IOW:OnInitialize() button1 = "Yes", button2 = "No", OnAccept = function(_, characterName) - GuildInvite(characterName) + C_GuildInfo.Invite(characterName) end, OnCancel = function() end, timeout = 0, @@ -52,8 +42,12 @@ function IOW:OnInitialize() text = "Do you want to invite %s to your party/raid?", button1 = "Yes", button2 = "No", - OnAccept = function(_, characterName) - InviteUnit(characterName) + OnAccept = function(_, characterNameOrPresenceID) + if type(characterNameOrPresenceID) == "number" then + BNInviteFriend(characterNameOrPresenceID) + else + C_PartyInfo.InviteUnit(characterNameOrPresenceID) + end end, OnCancel = function() end, timeout = 0, @@ -93,21 +87,28 @@ end function IOW:GetCharacterNameFromPresenceID(presenceID) local accountInfo = C_BattleNet.GetAccountInfoByID(presenceID); - if(accountInfo.gameAccountInfo and accountInfo.gameAccountInfo.characterName and accountInfo.gameAccountInfo.realmName) then + if(accountInfo and accountInfo.gameAccountInfo and accountInfo.gameAccountInfo.characterName and accountInfo.gameAccountInfo.realmName) then return accountInfo.gameAccountInfo.characterName .. '-' .. accountInfo.gameAccountInfo.realmName; end return nil; end +---@param message string +---@param outgoing boolean +---@param presenceID number? function IOW:HandleBnetWhisper(message, presenceID, outgoing) local characterName = self:GetCharacterNameFromPresenceID(presenceID); if(characterName) then - self:ProcessMessage(message, characterName, outgoing); + self:ProcessMessage(message, characterName, outgoing, presenceID); end end -function IOW:ProcessMessage(message, characterName, outgoing) +---@param message string +---@param characterName string +---@param outgoing boolean +---@param presenceID number? +function IOW:ProcessMessage(message, characterName, outgoing, presenceID) message = message:lower():trim() if self.DB.ginv[message] and (not outgoing or self.DB.triggerOutgoingGInv) then local dialog = StaticPopup_Show("IOWguildinvPopup", characterName) @@ -119,11 +120,15 @@ function IOW:ProcessMessage(message, characterName, outgoing) if(self.DB.confirm) then local dialog = StaticPopup_Show("IOWgroupinvPopup", characterName) if (dialog) then - dialog.data = characterName + dialog.data = presenceID or characterName end else self:Print("Trying to invite " .. characterName .. " to your party/raid") - InviteUnit(characterName) + if presenceID then + BNInviteFriend(presenceID) + else + C_PartyInfo.InviteUnit(characterName) + end end return end @@ -149,7 +154,7 @@ function IOW:ProcessMessage(message, characterName, outgoing) local dialog = StaticPopup_Show("IOWgroupinvPopup", characterName) if (dialog) then found = true - dialog.data = characterName + dialog.data = presenceID or characterName end break end