-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelHandler.lua
More file actions
171 lines (147 loc) · 4.5 KB
/
ChannelHandler.lua
File metadata and controls
171 lines (147 loc) · 4.5 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
RaidCalendar = RaidCalendar or {}
---@class RaidCalendar
local m = RaidCalendar
if m.ChannelHandler then return end
---@class ChannelHandler
---@field request_events fun( bots: table )
---@field request_bots fun()
---@field find_discord_id fun( name: string, bot: string )
---@field authorize_user fun( user_id: string, bot: string )
---@field ping fun( bots: table )
---@field on_msg fun( text: string, player_name: string, channel_name: string)
---@field join_channel fun()
---@field get_channel_id fun(): number
local M = {}
function M.new()
local channel_id = 0
local channel_name = "LFT"
---@param command MessageCommand
---@param bot string?
---@param data table?
local function broadcast( command, bot, data )
m.debug( string.format( "Channel send %s", command ) )
bot = bot and ":" .. bot or ""
local _data = ""
if data then
for _, v in pairs( data ) do
_data = _data .. ":" .. v
end
end
SendChatMessage( string.upper( m.prefix ) .. ":" .. command .. bot .. _data, 'CHANNEL', nil, channel_id )
end
local function request_events( bots )
local data = {}
for bot in pairs( bots ) do
table.insert( data, bot )
end
broadcast( m.MessageCommand.RequestEvents, nil, data )
end
local function request_bots()
broadcast( m.MessageCommand.Who, nil, nil )
end
local function find_discord_id( name, bot )
broadcast( m.MessageCommand.RequestDiscordId, bot, { name = name } )
end
local function authorize_user( user_id, bot )
broadcast( m.MessageCommand.RequestDiscordAuth, bot, {
userId = user_id
} )
end
local function ping( bots )
local data = {}
for bot in pairs( bots ) do
table.insert( data, bot )
end
broadcast( m.MessageCommand.Ping, nil, data )
end
local function join_channel()
local is_in_channel = false
local channels = { GetChannelList() }
for _, channel in next, channels do
if string.upper( channel ) == channel_name then
is_in_channel = true
break
end
end
if not is_in_channel then
JoinChannelByName( channel_name )
end
channel_id = GetChannelName( channel_name )
end
local function on_msg( text, player_name, channel )
if player_name ~= m.player then
local source = string.match( channel, "(%d+)%." )
if source then
local _, ch_name = GetChannelName( source )
if ch_name and string.upper( ch_name ) == channel_name then
local msg = m.explode( text )
local bot_name = player_name
if getn( msg ) > 1 and msg[ 1 ] == "RAIDCAL" then
--m.info( "got raidcal " )
local command = msg[ 2 ]
if command == "GUILD" and getn( msg ) == 3 then
-- m.info( "got guild " )
m.welcome_popup.add_bot( bot_name, msg[ 3 ] )
elseif command == "PONG" then
if m.db.bots[ bot_name ] then
m.calendar_popup.set_online( bot_name )
end
elseif command == "DID" and getn( msg ) == 5 and msg[ 3 ] == m.player then
m.welcome_popup.discord_response( msg[ 4 ] ~= "", msg[ 4 ], msg[ 5 ] )
elseif command == "DAUTH" and getn( msg ) == 5 and msg[ 3 ] == m.player then
local user_id = msg[ 4 ]
local success = msg[ 5 ] == "1" and true or false
if success then
m.debug( "Saving Discord ID: " .. user_id )
local bot = m.welcome_popup.get_current_bot()
m.db.bots[ bot_name ] = {
discord_id = user_id,
discord_bot = bot.discord_bot,
guild = bot.guild_name
}
end
m.welcome_popup.auth_response( user_id, success )
elseif command == "EVENT" and m.db.bots[ bot_name ] and getn( msg ) >= 13 then
local event_id = msg[ 3 ]
if m.db.events[ event_id ] then
m.db.events[ event_id ].bot = bot_name
else
m.db.events[ event_id ] = {
title = msg[ 4 ],
color = msg[ 5 ],
templateId = msg[ 6 ],
signUpCount = msg[ 7 ],
leaderId = msg[ 8 ],
lastUpdated = msg[ 9 ],
leaderName = msg[ 10 ],
closeTime = msg[ 11 ],
startTime = msg[ 12 ],
endTime = msg[ 13 ],
bot = bot_name
}
end
m.db.user_settings.last_updated = time()
m.calendar_popup.update()
end
end
end
end
end
end
local function get_channel_id()
return channel_id
end
---@type ChannelHandler
return {
request_events = request_events,
request_bots = request_bots,
find_discord_id = find_discord_id,
authorize_user = authorize_user,
ping = ping,
join_channel = join_channel,
on_msg = on_msg,
get_channel_id = get_channel_id
}
end
m.ChannelHandler = M
return M