-
Notifications
You must be signed in to change notification settings - Fork 0
/
InviteOnWhisper.lua
171 lines (155 loc) · 5.91 KB
/
InviteOnWhisper.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
local addonName = ...
---@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 {}
self.DB = IOWDB
self:InitDefaults()
self.Config:Initialize()
self:RegisterEvent("CHAT_MSG_BN_WHISPER", function(_, message, _, _, _, _, _, _, _, _, _, _, _, bnetIDAccount, _)
self:HandleBnetWhisper(message, bnetIDAccount, false)
end)
self:RegisterEvent("CHAT_MSG_BN_WHISPER_INFORM", function(_, message, _, _, _, _, _, _, _, _, _, _, _, bnetIDAccount, _)
self:HandleBnetWhisper(message, bnetIDAccount, true)
end)
self:RegisterEvent("CHAT_MSG_WHISPER", function(_, message, characterName, _)
self:HandleWhisper(message, characterName, false)
end)
self:RegisterEvent("CHAT_MSG_WHISPER_INFORM", function(_, message, characterName, _)
self:HandleWhisper(message, characterName, true)
end)
self:RegisterChatCommand('iow', self.Config.OpenConfig)
StaticPopupDialogs["IOWguildinvPopup"] = {
text = "Do you want to invite %s to your guild?",
button1 = "Yes",
button2 = "No",
OnAccept = function(_, characterName)
C_GuildInfo.Invite(characterName)
end,
OnCancel = function() end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["IOWgroupinvPopup"] = {
text = "Do you want to invite %s to your party/raid?",
button1 = "Yes",
button2 = "No",
OnAccept = function(_, characterNameOrPresenceID)
if type(characterNameOrPresenceID) == "number" then
BNInviteFriend(characterNameOrPresenceID)
else
C_PartyInfo.InviteUnit(characterNameOrPresenceID)
end
end,
OnCancel = function() end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
end
function IOW:InitDefaults()
local defaults = {
ginv = {
ginv = true,
guildinv = true,
ginvite = true
},
inv = {
inv = true,
invite = true
},
confirm = true,
keywordMatchMiddle = true,
triggerOutgoingGInv = true,
triggerOutgoingInv = false,
}
for property, value in pairs(defaults) do
if self.DB[property] == nil then
self.DB[property] = value
end
end
end
function IOW:HandleWhisper(message, characterName, outgoing)
self:ProcessMessage(message, characterName, outgoing)
end
function IOW:GetCharacterNameFromPresenceID(presenceID)
local accountInfo = C_BattleNet.GetAccountInfoByID(presenceID);
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, presenceID);
end
end
---@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)
if (dialog) then
dialog.data = characterName
end
return
elseif self.DB.inv[message] and (not outgoing or self.DB.triggerOutgoingInv) then
if(self.DB.confirm) then
local dialog = StaticPopup_Show("IOWgroupinvPopup", characterName)
if (dialog) then
dialog.data = presenceID or characterName
end
else
self:Print("Trying to invite " .. characterName .. " to your party/raid")
if presenceID then
BNInviteFriend(presenceID)
else
C_PartyInfo.InviteUnit(characterName)
end
end
return
end
if self.DB.keywordMatchMiddle then
local found = false
message = ' ' .. message .. ' '
-- wrapping spaces around message, so that it starts and ends with a non alphabetical letter
for phrase in pairs(self.DB.ginv) do
if (not outgoing) and message:find('[^A-z]' .. phrase:lower():trim() .. '[^A-z]') then
local dialog = StaticPopup_Show("IOWguildinvPopup", characterName)
if (dialog) then
found = true
dialog.data = characterName
end
break
end
end
if not found then
for phrase in pairs(self.DB.inv) do
if (not outgoing) and message:find('[^A-z]' .. phrase:lower():trim() .. '[^A-z]') then
local dialog = StaticPopup_Show("IOWgroupinvPopup", characterName)
if (dialog) then
found = true
dialog.data = presenceID or characterName
end
break
end
end
end
if found then
self:Print("An invite keyword was found in the whisper you received. Type \"/iow\" and disable 'Smart Match' if you don't want long whispers to trigger an invite.")
if(not self.DB.confirm) then
self:Print("The confirmation dialog cannot be disabled when Smart Match got triggered")
end
end
end
end