-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageHandler.lua
More file actions
703 lines (641 loc) · 19.2 KB
/
MessageHandler.lua
File metadata and controls
703 lines (641 loc) · 19.2 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
RaidCalendar = RaidCalendar or {}
---@class RaidCalendar
local m = RaidCalendar
if m.MessageHandler then return end
---@type MessageCommand
local MessageCommand = {
-- Outgoing
RequestBotStatus = "RBSTATUS",
RequestDiscordId = "RDID",
RequestDiscordAuth = "RDAUTH",
RequestChannelCheck = "CHCHECK",
RequestEvent = "REVENT",
RequestEvents = "REVENTS",
EditEvent = "EDITEVENT",
Signup = "SIGNUP",
SignupEdit = "SIGNUP_EDIT",
RequestSR = "RSR",
AddSR = "SRADD",
DeleteSR = "SRDELETE",
LockSR = "SRLOCK",
VersionCheck = "VERC",
Ping = "PING",
Who = "WHO",
-- Incoming
BotStatus = "BSTATUS",
DiscordId = "DID",
DiscordAuth = "DAUTH",
ChannelCheckResult = "CHCHECK_RESULT",
Event = "EVENT",
Events = "EVENTS",
EditEventResult = "EDITEVENT_RESULT",
SignupResult = "SIGNUP_RESULT",
SR = "SR",
AddSRResult = "SRADD_RESULT",
DeleteSRResult = "SRDELETE_RESULT",
LockSRResult = "SRLOCK_RESULT",
Version = "VER",
Pong = "PONG"
}
m.MessageCommand = MessageCommand
---@alias MessageCommand
---| "RDAUTH"
---| "DAUTH"
---| "CHCHECK"
---| "CHCHECK_RESULT"
---| "RBSTATUS"
---| "BSTATUS"
---| "DID"
---| "RDID"
---| "SR"
---| "RSR"
---| "SRADD"
---| "SRADD_RESULT"
---| "SRDELETE"
---| "SRDELETE_RESULT"
---| "SRLOCK"
---| "SRLOCK_RESULT"
---| "REVENT"
---| "REVENTS"
---| "EVENT"
---| "EVENTS"
---| "EDITEVENT"
---| "EDITEVENT_RESULT"
---| "SIGNUP"
---| "SIGNUP_EDIT"
---| "SIGNUP_RESULT"
---| "VERC"
---| "VER"
---| "WHO"
---| "PING"
---| "PONG"
---@class AceComm
---@field RegisterComm fun( self: any, prefix: string, method: function? )
---@field SendCommMessage fun( self: any, prefix: string, text: string, distribution: string, target: string?, prio: "BULK"|"NORMAL"|"ALERT"?, callbackFn: function?, callbackArg: any? )
---@class MessageHandler
---@field find_discord_id fun( name: string )
---@field check_channel_access fun( channel_id: string, renew: boolean? )
---@field authorize_user fun( user_id: string )
---@field add_sr fun( raid_id: number, sr_id: string, sr1: number, sr2: number, comment: string? )
---@field delete_sr fun( sr_id: string, id: number )
---@field request_sr fun( sr_id: string )
---@field lock_sr fun( sr_id: string, lock: boolean )
---@field request_event fun( event_id: string )
---@field request_events fun()
---@field signup fun( event_id: string, user_id: string )
---@field signup_edit fun( event_id: string, signup_id: string, role: string? )
---@field set_event_public fun( event_id: string, public: boolean )
---@field bot_status fun()
---@field version_check fun( show_all: boolean?)
local M = {}
function M.new()
local lib_stub = LibStub
---@type AceComm
local ace_comm = lib_stub( "AceComm-3.0" )
local chunk_total = 0
local chunk_data
local key_map = {
a = "announcements",
n = "name",
ct = "closingTime",
s = "startTime",
cl = "classes",
l = "lastUpdated",
e = "entryTime",
d = "description",
le = "leaderName",
ch = "channelType",
ld = "leaderId",
si = "signUps",
cn = "channelName",
ef = "effectiveName",
r = "roleName",
c = "color",
et = "endTime",
i = "id",
se = "serverId",
t = "templateId",
da = "date",
ro = "roles",
st = "status",
cs = "className",
sp = "specName",
p = "position",
ti = "time",
u = "userId",
tl = "title",
ty = "type",
li = "limit",
ci = "channelId",
sc = "specs",
di = "displayTitle",
su = "signUpCount",
co = "closeTime",
re = "reference",
ad = "allowDuplicateReservation",
ac = "allowComments",
rl = "reservationLimit",
cm = "comment",
ca = "character",
b = "raidItemId",
f = "itemId",
sr = "srPlus",
z = "specialization",
ah = "advancedHrItems",
h = "isHardReserved",
cz = "characterSpecializations",
cb = "characterNames",
rv = "reservations"
}
local value_map = {
[ "#1" ] = "Tanks",
[ "#2" ] = "Arms",
[ "#3" ] = "Fury",
[ "#4" ] = "Protection",
[ "#5" ] = "Protection1",
[ "#6" ] = "Holy",
[ "#7" ] = "Holy1",
[ "#8" ] = "Retribution",
[ "#9" ] = "Guardian",
[ "#10" ] = "Combat",
[ "#11" ] = "Demonology",
[ "#12" ] = "Destruction",
[ "#13" ] = "Enhancement",
[ "#14" ] = "Dps",
[ "#15" ] = "Feral",
[ "#16" ] = "Assassination",
[ "#17" ] = "Subtlety",
[ "#18" ] = "Survival",
[ "#19" ] = "Beastmastery",
[ "#20" ] = "Arcane",
[ "#21" ] = "Fire",
[ "#22" ] = "Frost",
[ "#23" ] = "Affliction",
[ "#24" ] = "Marksmanship",
[ "#25" ] = "Balance",
[ "#26" ] = "Shadow",
[ "#27" ] = "Smite",
[ "#28" ] = "Elemental",
[ "#29" ] = "Ranged",
[ "#30" ] = "Discipline",
[ "#31" ] = "Restoration",
[ "#32" ] = "Restoration1",
[ "#33" ] = "Healer",
[ "#34" ] = "Late",
[ "#35" ] = "Bench",
[ "#36" ] = "Tentative",
[ "#37" ] = "Absence",
[ "#38" ] = "Healers",
[ "#39" ] = "Melee",
[ "#40" ] = "Tank",
[ "#41" ] = "primary",
[ "#42" ] = "Druid",
[ "#43" ] = "DruidBalance",
[ "#44" ] = "DruidFeral",
[ "#45" ] = "DruidRestoration",
[ "#46" ] = "DruidBear",
[ "#47" ] = "Hunter",
[ "#48" ] = "HunterBeastMastery",
[ "#49" ] = "HunterMarksmanship",
[ "#50" ] = "HunterSurvival",
[ "#51" ] = "Mage",
[ "#52" ] = "MageArcane",
[ "#53" ] = "MageFire",
[ "#54" ] = "MageFrost",
[ "#55" ] = "Paladin",
[ "#56" ] = "PaladinHoly",
[ "#57" ] = "PaladinProtection",
[ "#58" ] = "PaladinRetribution",
[ "#59" ] = "Priest",
[ "#60" ] = "PriestDiscipline",
[ "#61" ] = "PriestHoly",
[ "#62" ] = "PriestShadow",
[ "#63" ] = "Rogue",
[ "#64" ] = "RogueSwords",
[ "#65" ] = "RogueDaggers",
[ "#66" ] = "RogueMaces",
[ "#67" ] = "Shaman",
[ "#68" ] = "ShamanElemental",
[ "#69" ] = "ShamanEnchancement",
[ "#70" ] = "ShamanRestoration",
[ "#71" ] = "ShamanTank",
[ "#72" ] = "Warlock",
[ "#73" ] = "WarlockAffliction",
[ "#74" ] = "Demonology",
[ "#75" ] = "Destruction",
[ "#76" ] = "Warrior",
[ "#77" ] = "WarriorArms",
[ "#78" ] = "WarriorFury",
[ "#79" ] = "WarriorProtection"
}
setmetatable( key_map, { __index = function( _, key ) return key end } );
setmetatable( value_map, { __index = function( _, key ) return key end } );
---@param tbl table
---@param keymap table
---@param valuemap table
---@return table
local function decode( tbl, keymap, valuemap )
local ret = {}
if not table then return ret end
for key, value in pairs( tbl ) do
if type( value ) == "table" then
value = decode( value, keymap, valuemap )
elseif type( value ) == "string" then
value = valuemap[ value ] or value
end
ret[ keymap[ key ] ] = value
end
return ret
end
---@param command MessageCommand
---@param data table?
local function broadcast( command, data )
m.debug( string.format( "Broadcasting %s", command ) )
--local channel = m.db.user_settings.bot_name and "GUILD" or "RAID"
local channel = "GUILD"
local _data = data and m.flatten( data ) or ""
ace_comm:SendCommMessage( m.prefix, command .. "::" .. _data, channel, nil, "NORMAL" )
end
local function find_discord_id( name )
broadcast( MessageCommand.RequestDiscordId, { name = name } )
end
local function check_channel_access( channel_id, renew )
broadcast( MessageCommand.RequestChannelCheck, {
userId = m.db.user_settings.discord_id,
channelId = channel_id,
renew = renew or false
} )
end
local function authorize_user( user_id )
broadcast( MessageCommand.RequestDiscordAuth, {
userId = user_id
} )
end
local function add_sr( raid_id, sr_id, sr1, sr2, comment )
local data = {
raidId = raid_id,
reference = sr_id,
comment = comment,
characterName = m.player,
characterClass = m.player_class,
specialization = m.player_class .. m.db.user_settings.sr_specName,
raidItemIds = {}
}
if sr1 then table.insert( data.raidItemIds, sr1 ) end
if sr2 then table.insert( data.raidItemIds, sr2 ) end
broadcast( MessageCommand.AddSR, data )
end
local function delete_sr( sr_id, id )
broadcast( MessageCommand.DeleteSR, {
reference = sr_id,
id = id
} )
end
local function request_sr( sr_id )
broadcast( MessageCommand.RequestSR, {
id = sr_id
} )
end
local function lock_sr( sr_id, locked )
broadcast( MessageCommand.LockSR, {
id = sr_id,
locked = locked
} )
end
local function request_events()
broadcast( MessageCommand.RequestEvents )
end
local function request_event( event_id )
broadcast( MessageCommand.RequestEvent, {
id = event_id
} )
end
local function signup( event_id, user_id )
local name = m.db.user_settings.use_character_name and m.player
local class_name = m.db.user_settings[ m.db.events[ event_id ].templateId .. "_className" ]
local spec_name = m.db.user_settings[ m.db.events[ event_id ].templateId .. "_specName" ]
local channel_id = m.db.events[ event_id ].channelId
broadcast( MessageCommand.Signup, {
eventId = event_id,
userId = user_id,
className = class_name,
specName = spec_name,
channelId = channel_id,
name = name
} );
end
local function signup_edit( event_id, signup_id, role )
local name = m.db.user_settings.use_character_name and m.player
local class_name = role and role or m.db.user_settings[ m.db.events[ event_id ].templateId .. "_className" ]
local spec_name = m.db.user_settings[ m.db.events[ event_id ].templateId .. "_specName" ]
local channel_id = m.db.events[ event_id ].channelId
broadcast( MessageCommand.SignupEdit, {
eventId = event_id,
signupId = signup_id,
className = class_name,
specName = spec_name,
channelId = channel_id,
name = name
} )
end
local function set_event_public( event_id, public )
broadcast( MessageCommand.EditEvent, {
eventId = event_id,
isPublic = public
} )
end
local function bot_status()
-- Send directly to avoid ChatThrottle's startup delay
SendAddonMessage( m.prefix, MessageCommand.RequestBotStatus .. "::", "GUILD" )
end
local function version_check( show_all )
m.version_show_all = show_all or false
broadcast( MessageCommand.VersionCheck )
end
---@param command string
---@param data table
---@param sender string
local function on_command( command, data, sender )
if command == MessageCommand.DiscordId then
--
-- Discord ID response
--
if data.player == m.player then
m.welcome_popup.discord_response( data.success, data.userId, sender )
end
elseif command == MessageCommand.ChannelCheckResult then
--
-- Channel access result
--
if data.player == m.player then
m.db.user_settings.channel_access[ data.channelId ] = data.success
m.event_popup.update()
end
elseif command == MessageCommand.DiscordAuth then
--
-- Discord authentication response
--
if data.player == m.player then
if data.success then
m.db.user_settings.discord_id = data.userId
end
m.welcome_popup.auth_response( data.userId, data.success )
end
elseif command == MessageCommand.SR then
--
-- SR
--
data = decode( data, key_map, value_map )
if data.success and data.success == false then
m.error( data.status )
return
end
for event_id, event in pairs( m.db.events ) do
if event.srId == data.reference then
m.db.events[ event_id ].sr = data
m.db.events[ event_id ].sr.lastUpdated = time()
m.sr_popup.update( event_id )
m.calendar_popup.update()
end
end
elseif command == MessageCommand.AddSRResult then
--
-- SR Added
--
data = decode( data, key_map, value_map )
if data.success then
m.debug( "SR Added" )
if data.addedSRs and type( data.addedSRs ) == "table" then
for event_id, event in pairs( m.db.events ) do
if event.srId == data.srId then
-- local _, event_id = m.find( data.srId, m.db.events, "srId" )
--if not event_id then
-- m.debug( "SR added but no event found for it!" )
--return
--end
--if data.addedSRs and type( data.addedSRs ) == "table" then
if m.db.events[ event_id ].sr and m.db.events[ event_id ].sr.reservations then
for _, res in pairs( data.addedSRs ) do
if not m.find( res.id, m.db.events[ event_id ].sr.reservations, "id" ) then
table.insert( m.db.events[ event_id ].sr.reservations, {
id = res.id,
raidItemId = res.raidItemId,
srPlus = res.srPlus,
comment = res.comment,
character = res.character
} )
m.sr_popup.update( event_id )
m.calendar_popup.update()
end
end
end
end
end
end
elseif data.player == m.player then
m.error( "Adding SR failed: " .. (data.status or "Unknown error") )
m.sr_popup.update()
end
elseif command == MessageCommand.DeleteSRResult then
--
-- SR Deleted
--
data = decode( data, key_map, value_map )
if data.success == true then
for event_id, event in pairs( m.db.events ) do
if event.sr then
local _, k = m.find( data.id, event.sr.reservations, "id" )
if k then
m.debug( "Delete entry: " .. tostring( k ) .. " in " .. event_id )
table.remove( event.sr.reservations, k )
m.sr_popup.update()
m.calendar_popup.update()
end
end
end
elseif data.player == m.player then
m.error( "Delete SR failed: " .. (data.status or "Unknown error") )
end
elseif command == MessageCommand.LockSRResult then
--
-- SRLOCK_RESULT
--
data = decode( data, key_map, value_map )
if data.success == true then
local _, eventId = m.find( data.srId, m.db.events, "srId" )
if m.db.events[ eventId ] and m.db.events[ eventId ].sr then
m.db.events[ eventId ].sr.locked = data.locked
end
m.sr_popup.update()
elseif data.player == m.player then
m.error( "Lock SR failed: " .. (data.status or "Unknown error") )
end
elseif command == MessageCommand.Event then
--
-- EVENT
--
data = decode( data, key_map, value_map )
m.debug( "Got event id: " .. data.id )
if m.db.events[ data.id ] and m.db.events[ data.id ].sr then
local sr = m.db.events[ data.id ].sr
m.db.events[ data.id ] = data
m.db.events[ data.id ].sr = sr
else
m.db.events[ data.id ] = data
end
local sr_ref = string.match( m.db.events[ data.id ].description, "https://raidres.top/res/(%w+)%s?" )
m.db.events[ data.id ].srId = sr_ref
m.db.events[ data.id ].title = string.gsub( m.db.events[ data.id ].title, "<:.*>", "" )
m.event_popup.update( data.id )
m.calendar_popup.update()
elseif command == MessageCommand.Events then
--
-- EVENTS
--
data = decode( data, key_map, value_map )
if data.error then
m.error( data.error )
return
end
if data.events then
m.debug( "Receiving events requested by " .. (data.player or "UNKNOWN") )
for _, event in data.events do
if m.db.events[ event.id ] then
m.db.events[ event.id ].isPublic = event.isPublic
-- Only send event update request from player who requested it if needed
if event.lastUpdated > m.db.events[ event.id ].lastUpdated and data.player == m.player then
m.debug( "Update event: " .. event.title )
request_event( event.id )
end
else
m.debug( "New event: " .. event.title )
event.title = string.gsub( event.title, "<:.*>", "" )
m.db.events[ event.id ] = event
end
end
end
-- Remove old and deleted raids
for id, event in m.db.events do
if not m.find( id, data.events, "id" ) then
m.debug( "Remove event: " .. event.title )
m.db.events[ id ] = nil
end
end
m.db.user_settings.last_updated = time()
m.calendar_popup.update()
elseif command == MessageCommand.EditEventResult then
--
-- EDITEVENT_RESULT
--
data = decode( data, key_map, value_map )
if data.success and m.db.events[ data.eventId ] then
m.db.events[ data.eventId ].isPublic = data.isPublic
m.event_popup.update( data.eventId )
m.calendar_popup.update()
elseif data.player == m.player then
m.error( "Edit event failed: " .. (data.status or "Unknown error") )
end
elseif command == MessageCommand.SignupResult then
--
-- SIGNUP_RESULT
--
data = decode( data, key_map, value_map )
if data.success and m.db.events[ data.eventId ] then
local _, index = m.find( data.signUp.id, m.db.events[ data.eventId ].signUps, "id" )
m.db.events[ data.eventId ].lastUpdated = tonumber( data.lastUpdated )
if index then
m.db.events[ data.eventId ].signUps[ index ] = data.signUp
else
table.insert( m.db.events[ data.eventId ].signUps, data.signUp )
end
m.calendar_popup.update()
m.event_popup.update( data.eventId )
elseif data.player == m.player then
m.error( "Signup failed: " .. data.status )
end
elseif command == MessageCommand.BotStatus then
--
-- Receive bot status
--
if data.player == m.player then
m.db.user_settings.bot_name = data.botName
m.db.user_settings.discord_bot = data.discordBot
m.db.user_settings.sr_admins = data.srAdmins
m.welcome_popup.bot_response( data.botName )
end
elseif command == MessageCommand.VersionCheck then
--
-- Receive version request
--
broadcast( MessageCommand.Version, { requester = sender, version = m.version, class = m.player_class } )
elseif command == MessageCommand.Version then
--
-- Receive version
--
if data.requester == m.player and m.version_show_all then
m.info( string.format( "%s [v%s]", m.colorize_player_by_class( sender, data.class ), data.version ), true )
return
end
if not m.db.user_settings.last_versioncheck or time() - m.db.user_settings.last_versioncheck > 3600 * 24 then
m.db.user_settings.last_versioncheck = time()
if m.is_new_version( m.version, data.version ) then
m.info( string.format( "New version (%s) is available!", data.version ) )
m.info( "https://github.com/sica42/RaidCalendar" )
end
end
end
end
local function on_comm_received( prefix, data_str, _, sender )
if prefix ~= m.prefix or sender == m.player then return end
local cmd_pat = "^([_%u%d]-)::"
local command = string.match( data_str, cmd_pat )
data_str = string.gsub( data_str, cmd_pat, "" )
if command then
if command == "CT" then
chunk_total = tonumber( data_str ) or 0
elseif string.find( command, "C%d+" ) then
local chunk_number = tonumber( string.match( command, "C(%d+)" ) )
chunk_data = chunk_number == 1 and data_str or chunk_data .. data_str
if chunk_number == chunk_total then
local cmd = string.match( chunk_data, cmd_pat )
chunk_data = string.gsub( chunk_data, cmd_pat, "" )
local lua_data = loadstring( "return " .. chunk_data )()
on_command( cmd, lua_data, sender )
end
else
data_str = string.gsub( data_str, cmd_pat, "" )
local success, lua_data = pcall( function()
return loadstring( "return " .. data_str )()
end )
if success then
on_command( command, lua_data, sender )
else
m.error( "Invalid data received. This is not suppose to happen!" )
m.debug( data_str )
end
end
else
m.debug( "No command, wtf?" )
end
end
ace_comm.RegisterComm( M, m.prefix, on_comm_received )
---@type MessageHandler
return {
find_discord_id = find_discord_id,
check_channel_access = check_channel_access,
authorize_user = authorize_user,
add_sr = add_sr,
delete_sr = delete_sr,
lock_sr = lock_sr,
request_sr = request_sr,
request_event = request_event,
request_events = request_events,
signup = signup,
signup_edit = signup_edit,
set_event_public = set_event_public,
bot_status = bot_status,
version_check = version_check
}
end
m.MessageHandler = M
return M