Updated libraries and updated calculate command

This commit is contained in:
Yessiest 2022-04-09 23:22:33 +04:00
parent 0c463f9340
commit f5f7384066
4 changed files with 7 additions and 43 deletions

View File

@ -1,4 +1,5 @@
package.path = "./libraries/?.lua;./libraries/?/init.lua;"..package.path package.path = "./libraries/?.lua;./libraries/?/init.lua;"..package.path
package.cpath = "./libraries/?.so;"..package.cpath
--load discordia --load discordia
discordia = require("discordia") discordia = require("discordia")

@ -1 +0,0 @@
Subproject commit 5e1105631195255e8305fcaba6d37d167e9bd897

@ -1 +1 @@
Subproject commit 5e1105631195255e8305fcaba6d37d167e9bd897 Subproject commit 979f5959d22a2b3e8883dc4af6ffa10472686e43

View File

@ -2,6 +2,7 @@ local plugin_class = import("classes.plugin")
local command = import("classes.command") local command = import("classes.command")
local plugin = plugin_class() local plugin = plugin_class()
local markov = require("markov") local markov = require("markov")
local qalculator = require("libqalculator")
local markov_instance = markov.new() local markov_instance = markov.new()
math.randomseed(os.time()+os.clock()) math.randomseed(os.time()+os.clock())
@ -97,53 +98,16 @@ local cards = command("cards",{
}) })
plugin:add_command(cards) plugin:add_command(cards)
local calculate = command("calculate",{ local calculate = command("calculate",{
help = "Calculate maths using lua's interpeter. Math functions from C included, use ``sin(x)`` or ``cos(x)`` for example. Additionally, BitOp module is included with the name ``bit`` (example: ``bit.bnot(1,1)``)", help = "Calculates maths using libqalculate. https://qalculate.github.io/ for more info",
usage = [[ usage = [[
calculate <expression> calculate "<expression>"
``--bit``; ``-b`` - if the output is a number, convert it to binary ``-e`` - exact mode
``--hex``; ``-h`` - if the output is a number, convert it to hexadecimal
]], ]],
args = { args = {
"string" "string"
}, },
exec = function(msg,args,opts) exec = function(msg,args,opts)
local calculation_coroutine = coroutine.wrap(function() msg:reply(qalculator.qalc(args[1],opts["e"]))
local sandbox = {}
sandbox = safe_clone(math,{randomseed = true})
sandbox["bit"] = safe_clone(bit,{})
local expression = (table.concat(args," ") or "")
local exception_keywords = { --this causes too much trouble
"while",
"function",
"for",
"if",
"then",
"do",
"end",
"repeat",
"until"
}
for k,v in pairs(exception_keywords) do
if expression:find("%W"..v.."%W") then
msg:reply("Invalid syntax")
return
end
end
local state,answer = pcall(load("return "..expression,"calc","t",setmetatable(sandbox,{})))
if state then
if type(answer) == "number" then
if opts["bit"] or opts["b"] then
answer = "0b"..to_bit_string(answer)
elseif opts["hex"] or opts["h"] then
answer = "0x"..bit.tohex(answer)
end
end
msg:reply(tostring(answer))
else
msg:reply(answer)
end
end)
calculation_coroutine()
end, end,
}) })
plugin:add_command(calculate) plugin:add_command(calculate)