From 52e25268aaf8d9573795a8a6c0db60469bae7950 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Fri, 27 May 2022 23:46:08 +0400 Subject: [PATCH] Added retries for failed cron events and updated calculate command --- libraries/luaqalc | 2 +- plugins/cron/init.lua | 47 ++++++++++++++++++++++++++++++++++-------- plugins/tools/help.lua | 7 ++++++- plugins/tools/init.lua | 25 +++++++++++++++++++++- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/libraries/luaqalc b/libraries/luaqalc index 23fcea1..e68aa52 160000 --- a/libraries/luaqalc +++ b/libraries/luaqalc @@ -1 +1 @@ -Subproject commit 23fcea17c2eb380b6a6b1ba848d5bc7ebdc38b05 +Subproject commit e68aa525e06d8025a2976d2f93d03ad7a4f81807 diff --git a/plugins/cron/init.lua b/plugins/cron/init.lua index f3642de..8da8a35 100644 --- a/plugins/cron/init.lua +++ b/plugins/cron/init.lua @@ -13,32 +13,59 @@ local events = { event = {} } -local exec = function(v,command) +sync_emitter:on("executeCommand",function(v,command,tried) 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) - return + log("ERROR","Failed event: "..command.."\nChannel Object: "..tostring(channel)) + if not tried then + log("INFO","Retrying...") + timer.setTimeout(2000,function() + sync_emitter:emit("executeCommand",v,command,true) + end) + 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) - return + 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("executeCommand",v,command,true) + end) + 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) - return + if not tried then + log("INFO","Retrying...") + timer.setTimeout(2000,function() + sync_emitter:emit("executeCommand",v,command,true) + 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, member = member, content = command }),1) -end +end) if not config.events then config.events = { @@ -328,7 +355,7 @@ timer:on("min",function() for k,v in pairs(events.timer) do local status,command = v.comm(os.date("*t")) if status then - exec(v,command) + sync_emitter:emit("executeCommand",v,command) if v.type == "onetime" then events.timer[k] = nil config.events.timer[k] = nil @@ -344,7 +371,9 @@ fhandler:close() local eventfunc = load(data,"event loader: "..plugin_path.."/events.lua",nil,setmetatable({ id = id, event_emitter = event_emitter, - exec = exec, + exec = function(v,command) + sync_emitter:emit("executeCommand",v,command) + end, events = events, config = config, discordia = discordia diff --git a/plugins/tools/help.lua b/plugins/tools/help.lua index 414535b..d15988e 100644 --- a/plugins/tools/help.lua +++ b/plugins/tools/help.lua @@ -7,7 +7,12 @@ return { fields = { {name = "Usage",value = [[calculate ""]]}, {name = "Perms: ",value = "All"}, - {name = "Options",value = "`-e` - exact mode"} + {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) +]]} } }}, ["pfp"] = "Show the profile picture of a user, or if none is specified, of yourself", diff --git a/plugins/tools/init.lua b/plugins/tools/init.lua index bd854df..3f891fc 100644 --- a/plugins/tools/init.lua +++ b/plugins/tools/init.lua @@ -104,7 +104,30 @@ local calculate = command("calculate",{ "string" }, exec = function(msg,args,opts) - msg:reply(qalculator.qalc(table.concat(args," "),opts["e"])) + 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) end, }) plugin:add_command(calculate)