Compare commits

..

No commits in common. "c41e4f6f264a96a817ce3c5d5c389bca72047d2c" and "4cf14a6bed0fd7d898cfaa34453a7e9eebbdfd3f" have entirely different histories.

5 changed files with 26 additions and 95 deletions

View File

@ -7,7 +7,7 @@ local command = class("Command")
local acl = import("classes.command-acl")
local discordia = import("discordia")
function command:__init(name,callback)
assert(name:match("^%S+$"),"Name cannot contain any space characters")
assert(name:match("^[-_%w]+$"),"Name can only contain alphanumeric characters, underscores or dashes")
self.rules = acl()
self.name = name
self.timer = discordia.Date():toMilliseconds()

@ -1 +1 @@
Subproject commit 8a1906df7a402349ff7b083e59cf17223363f59b
Subproject commit 23fcea17c2eb380b6a6b1ba848d5bc7ebdc38b05

View File

@ -13,64 +13,25 @@ local events = {
event = {}
}
-- 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 exec = function(v,command)
local channel = client:getChannel(v.channel)
if not channel then
log("ERROR","Unable to retrieve event channel: "..tostring(v.channel))
log("ERROR","Failed event: "..command.."\nChannel Object: "..tostring(channel))
if not tried then
log("INFO","Retrying...")
timer.setTimeout(2000,function()
sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end)
log("ERROR","Failed event: "..command)
return
else
log("ERROR","Tried 2 times to run the event and failed.")
return
end
end
local msg = channel:getMessage(v.id)
if not msg then
log("ERROR","Unable to retrieve event message: "..tostring(v.id))
log("ERROR","Failed event: "..command.."\nMessage object: "..tostring(msg).."\nChannel: "..tostring(channel.id))
if not tried then
log("INFO","Retrying...")
timer.setTimeout(2000,function()
sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end)
log("ERROR","Failed event: "..command)
return
else
log("ERROR","Tried 2 times to run the event and failed.")
return
end
end
--forcefully caching the goddamn member
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)
if not tried then
log("INFO","Retrying...")
timer.setTimeout(2000,function()
sync_emitter:emit("execContext",function()
exec(v,command,true)
end)
end)
return
else
log("ERROR","Tried 2 times to run the event and failed.")
return
end
end
command_handler:handle(fake_message(msg,{
delete = function() end,
@ -196,7 +157,7 @@ local remove_user_event = function(user_id,id)
return false
end
register_event = function(k,v,timer,evname)
sync_emitter:on("createEventEntry",function(k,v,timer,evname)
local channel = client:getChannel(v.channel)
if channel then
local message = channel:getMessage(v.id)
@ -217,26 +178,25 @@ register_event = function(k,v,timer,evname)
log("ERROR","No message with id "..v.id)
log("ERROR","Event id: "..k..".\nEvent description: ")
print(v.comm)
return false,k
sync_emitter:emit("eventEntryCreated",false,k)
end
else
log("ERROR","No channel with id "..v.channel)
log("ERROR","Event id: "..k..".\nEvent description: ")
print(v.comm)
return false,k
end
return true,k
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
local ev,hash = register_event(k,v,true)
if (not ev) then
log("INFO","Retrying event "..k.." in 2 seconds")
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("execContext",function()
register_event(k,v)
end)
sync_emitter:emit("createEventEntry",k,v,true)
end)
end
end
@ -245,13 +205,12 @@ end
for _,evtype in pairs(config.events.event) do
events.event[_] = {}
for k,v in pairs(evtype) do
local ev,hash = register_event(k,v,true)
if (not ev) then
log("INFO","Retrying event "..k.." in 2 seconds")
sync_emitter:emit("createEventEntry",k,v,false,_)
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("execContext",function()
register_event(k,v)
end)
sync_emitter:emit("createEventEntry",k,v,false,_)
end)
end
end
@ -369,7 +328,7 @@ timer:on("min",function()
for k,v in pairs(events.timer) do
local status,command = v.comm(os.date("*t"))
if status then
sync_emitter:emit("executeCommand",v,command)
exec(v,command)
if v.type == "onetime" then
events.timer[k] = nil
config.events.timer[k] = nil

View File

@ -7,12 +7,7 @@ return {
fields = {
{name = "Usage",value = [[calculate "<expression>"]]},
{name = "Perms: ",value = "All"},
{name = "Options",value = [[
`-e` - exact mode (try to be as exact as possible)
`-i` - interval mode (imprecise numbers will be calculated using variance formula)
`-f` - factorize mode (factorize result, default behaviour is to expand result)
`-o` - output only (don't show embed and messages)
]]}
{name = "Options",value = "`-e` - exact mode"}
}
}},
["pfp"] = "Show the profile picture of a user, or if none is specified, of yourself",

View File

@ -104,30 +104,7 @@ local calculate = command("calculate",{
"string"
},
exec = function(msg,args,opts)
local e,i,f = opts["e"],opts["i"],opts["f"]
local result = {embed = {
title = "Result",
fields = {
{name = "Value: ",value = nil},
},
footer = {
text = "Powered by libqalculate"
},
color = discordia.Color.fromHex("7A365F").value
}}
local value,err = qalculator.qalc(table.concat(args," "),e,i,f)
result.embed.fields[1].value = "```"..value.."```"
if opts["o"] then
msg:reply(value)
return
end
if #err > 0 then
result.embed.fields[2] = {
name = "Messages: ",
value = "```"..table.concat(err,"\n").."```"
}
end
msg:reply(result)
msg:reply(qalculator.qalc(table.concat(args," "),opts["e"]))
end,
})
plugin:add_command(calculate)