Replaced the synchrouns async with simple synchronous code

This commit is contained in:
Yessiest 2022-05-28 01:23:30 +04:00
parent 2e23f128f0
commit c41e4f6f26
1 changed files with 33 additions and 25 deletions

View File

@ -13,7 +13,13 @@ local events = {
event = {} event = {}
} }
sync_emitter:on("executeCommand",function(v,command,tried) -- timer threads are not API-friendly so we need to call certain functions
-- in context of a discordia emitter.
sync_emitter:on("execContext",function(comm)
comm()
end)
exec = function(v,command,tried)
local channel = client:getChannel(v.channel) local channel = client:getChannel(v.channel)
if not channel then if not channel then
log("ERROR","Unable to retrieve event channel: "..tostring(v.channel)) log("ERROR","Unable to retrieve event channel: "..tostring(v.channel))
@ -21,7 +27,9 @@ sync_emitter:on("executeCommand",function(v,command,tried)
if not tried then if not tried then
log("INFO","Retrying...") log("INFO","Retrying...")
timer.setTimeout(2000,function() timer.setTimeout(2000,function()
sync_emitter:emit("executeCommand",v,command,true) sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end) end)
return return
else else
@ -36,7 +44,9 @@ sync_emitter:on("executeCommand",function(v,command,tried)
if not tried then if not tried then
log("INFO","Retrying...") log("INFO","Retrying...")
timer.setTimeout(2000,function() timer.setTimeout(2000,function()
sync_emitter:emit("executeCommand",v,command,true) sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end) end)
return return
else else
@ -52,7 +62,9 @@ sync_emitter:on("executeCommand",function(v,command,tried)
if not tried then if not tried then
log("INFO","Retrying...") log("INFO","Retrying...")
timer.setTimeout(2000,function() timer.setTimeout(2000,function()
sync_emitter:emit("executeCommand",v,command,true) sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end) end)
return return
else else
@ -65,7 +77,7 @@ sync_emitter:on("executeCommand",function(v,command,tried)
member = member, member = member,
content = command content = command
}),1) }),1)
end) end
if not config.events then if not config.events then
config.events = { config.events = {
@ -184,7 +196,7 @@ local remove_user_event = function(user_id,id)
return false return false
end end
sync_emitter:on("createEventEntry",function(k,v,timer,evname) register_event = function(k,v,timer,evname)
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)
@ -205,28 +217,26 @@ sync_emitter:on("createEventEntry",function(k,v,timer,evname)
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) return false,k
return
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) return false,k
return end
return true,k
end end
sync_emitter:emit("eventEntryCreated",true,k)
end)
-- load timer events -- load timer events
for k,v in pairs(config.events.timer) do for k,v in pairs(config.events.timer) do
sync_emitter:emit("createEventEntry",k,v,true) local ev,hash = register_event(k,v,true)
local cor, ev, hash = sync_emitter:waitFor("eventEntryCreated",2000, if (not ev) then
function(ev,hash) return hash == k end)
if (not cor) or (not ev) then
log("INFO","Retrying event "..k.." in 2 seconds") log("INFO","Retrying event "..k.." in 2 seconds")
timer.setTimeout(2000,function() timer.setTimeout(2000,function()
sync_emitter:emit("createEventEntry",k,v,true) sync_emitter:emit("execContext",function()
register_event(k,v)
end)
end) end)
end end
end end
@ -235,13 +245,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
sync_emitter:emit("createEventEntry",k,v,false,_) local ev,hash = register_event(k,v,true)
local cor,ev,hash = sync_emitter:waitFor("eventEntryCreated",2000, if (not ev) then
function(ev,hash) return hash == k end)
if (not cor) or (not ev) then
log("INFO","Retrying event "..k.." in 2 seconds") log("INFO","Retrying event "..k.." in 2 seconds")
timer.setTimeout(2000,function() timer.setTimeout(2000,function()
sync_emitter:emit("createEventEntry",k,v,false,_) sync_emitter:emit("execContext",function()
register_event(k,v)
end)
end) end)
end end
end end
@ -375,9 +385,7 @@ fhandler:close()
local eventfunc = load(data,"event loader: "..plugin_path.."/events.lua",nil,setmetatable({ local eventfunc = load(data,"event loader: "..plugin_path.."/events.lua",nil,setmetatable({
id = id, id = id,
event_emitter = event_emitter, event_emitter = event_emitter,
exec = function(v,command) exec = exec,
sync_emitter:emit("executeCommand",v,command)
end,
events = events, events = events,
config = config, config = config,
discordia = discordia discordia = discordia