Forced member caching and added asynchronous failed event reloading logic
This commit is contained in:
parent
07501e7fa0
commit
4cf14a6bed
|
@ -5,6 +5,8 @@ local cron = import("cron")
|
||||||
local fake_message = import("fake_message")
|
local fake_message = import("fake_message")
|
||||||
local md5 = import("md5")
|
local md5 = import("md5")
|
||||||
local discordia = import("discordia")
|
local discordia = import("discordia")
|
||||||
|
local timer = import("timer")
|
||||||
|
local sync_emitter = discordia.Emitter()
|
||||||
local event_emitter = events
|
local event_emitter = events
|
||||||
local events = {
|
local events = {
|
||||||
timer = {},
|
timer = {},
|
||||||
|
@ -24,13 +26,16 @@ local exec = function(v,command)
|
||||||
log("ERROR","Failed event: "..command)
|
log("ERROR","Failed event: "..command)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if not msg.member then
|
--forcefully caching the goddamn member
|
||||||
log("ERROR","Unable to retrieve event creator: "..tostring(v.user.id))
|
local member = msg.guild:getMember(v.user)
|
||||||
|
if not member then
|
||||||
|
log("ERROR","Unable to retrieve event creator: "..tostring(v.user))
|
||||||
log("ERROR","Failed event: "..command)
|
log("ERROR","Failed event: "..command)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
command_handler:handle(fake_message(msg,{
|
command_handler:handle(fake_message(msg,{
|
||||||
delete = function() end,
|
delete = function() end,
|
||||||
|
member = member,
|
||||||
content = command
|
content = command
|
||||||
}),1)
|
}),1)
|
||||||
end
|
end
|
||||||
|
@ -152,8 +157,7 @@ local remove_user_event = function(user_id,id)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- load timer events
|
sync_emitter:on("createEventEntry",function(k,v,timer,evname)
|
||||||
for k,v in pairs(config.events.timer) do
|
|
||||||
local channel = client:getChannel(v.channel)
|
local channel = client:getChannel(v.channel)
|
||||||
if channel then
|
if channel then
|
||||||
local message = channel:getMessage(v.id)
|
local message = channel:getMessage(v.id)
|
||||||
|
@ -162,19 +166,38 @@ for k,v in pairs(config.events.timer) do
|
||||||
--orphan events with mismatching hashes
|
--orphan events with mismatching hashes
|
||||||
if status and (hash ~= k) then
|
if status and (hash ~= k) then
|
||||||
log("WARNING", "Hash mismatch, orphaning event.")
|
log("WARNING", "Hash mismatch, orphaning event.")
|
||||||
events.timer[k] = nil
|
if timer then
|
||||||
config.events.timer[k] = nil
|
config.events.timer[k] = nil
|
||||||
create_event(message,v.comm)
|
config.events.timer[hash] = v
|
||||||
|
else
|
||||||
|
config.events.event[evname][k] = nil
|
||||||
|
config.events.event[evname][hash] = v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
log("ERROR","No message with id "..v.id)
|
log("ERROR","No message with id "..v.id)
|
||||||
log("ERROR","Event id: "..k..".\nEvent description: ")
|
log("ERROR","Event id: "..k..".\nEvent description: ")
|
||||||
print(v.comm)
|
print(v.comm)
|
||||||
|
sync_emitter:emit("eventEntryCreated",false,k)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
log("ERROR","No channel with id "..v.channel)
|
log("ERROR","No channel with id "..v.channel)
|
||||||
log("ERROR","Event id: "..k..".\nEvent description: ")
|
log("ERROR","Event id: "..k..".\nEvent description: ")
|
||||||
print(v.comm)
|
print(v.comm)
|
||||||
|
sync_emitter:emit("eventEntryCreated",false,k)
|
||||||
|
end
|
||||||
|
sync_emitter:emit("eventEntryCreated",true,k)
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- load timer events
|
||||||
|
for k,v in pairs(config.events.timer) do
|
||||||
|
sync_emitter:emit("createEventEntry",k,v,true)
|
||||||
|
local cor, ev, hash = sync_emitter:waitFor("eventEntryCreated")
|
||||||
|
if (not cor) or (not ev) then
|
||||||
|
log("INFO","Retrying in 2 seconds")
|
||||||
|
timer.setTimeout(2000,function()
|
||||||
|
sync_emitter:emit("createEventEntry",k,v,true)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -182,27 +205,13 @@ end
|
||||||
for _,evtype in pairs(config.events.event) do
|
for _,evtype in pairs(config.events.event) do
|
||||||
events.event[_] = {}
|
events.event[_] = {}
|
||||||
for k,v in pairs(evtype) do
|
for k,v in pairs(evtype) do
|
||||||
local channel = client:getChannel(v.channel)
|
sync_emitter:emit("createEventEntry",k,v,false,_)
|
||||||
if channel then
|
local cor,ev,hash = sync_emitter:waitFor("eventEntryCreated")
|
||||||
local message = channel:getMessage(v.id)
|
if (not cor) or (not ev) then
|
||||||
if message then
|
log("INFO","Retrying in 2 seconds")
|
||||||
local status,hash = create_event(message,v.comm,true)
|
timer.setTimeout(2000,function()
|
||||||
--orphan events with mismatching hashes
|
sync_emitter:emit("createEventEntry",k,v,false,_)
|
||||||
if status and (hash ~= k) then
|
end)
|
||||||
log("WARNING", "Hash mismatch, orphaning event.")
|
|
||||||
events.event[_][k] = nil
|
|
||||||
config.events.event[_][k] = nil
|
|
||||||
create_event(message,v.comm)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
log("ERROR","No message with id "..v.id)
|
|
||||||
log("ERROR","Event "..k..".\nEvent description: ")
|
|
||||||
config.events.event[_][k] = nil
|
|
||||||
end
|
|
||||||
else
|
|
||||||
log("ERROR","No channel with id "..v.channel)
|
|
||||||
log("ERROR","Event "..k..".\nEvent description: ")
|
|
||||||
config.events.event[_][k] = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue