Final beta release of cron

This commit is contained in:
Yessiest 2022-05-08 20:39:46 +04:00
parent 08450e8a4b
commit 860134bccd
1 changed files with 133 additions and 72 deletions

View File

@ -33,6 +33,86 @@ if not config.events then
} }
end end
local create_event = function(msg,cronjob,create_entry)
local arg = cronjob
local func,functype = cron.parse_line(arg)
if not func then
msg:reply(functype)
return false
end
local hash = md5.sumhexa(arg):sub(1,16)
if functype == "directive" then
local event_name = arg:match("^@(%w+)")
if not events.event[event_name] then events.event[event_name] = {} end
events.event[event_name][hash] = {
comm = func,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
if create_entry then return true end
if not config.events.event[event_name] then config.events.event[event_name] = {} end
config.events.event[event_name][hash] = {
comm = arg,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
else
events.timer[hash] = {
comm = func,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
if create_entry then return true end
config.events.timer[hash] = {
comm = arg,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
end
return true
end
-- load timer events
for k,v in pairs(config.events.timer) do
local channel = client:getChannel(v.channel)
if channel then
local message = channel:getMessage(v.id)
if message then
create_event(message,v.comm,true)
else
log("ERROR","No message with id "..v.id)
end
else
log("ERROR","No channel with id "..v.channel)
end
end
-- load named events
for _,evtype in pairs(config.events.event) do
events.event[_] = {}
for k,v in pairs(evtype) do
local channel = client:getChannel(v.channel)
if channel then
local message = channel:getMessage(v.id)
if message then
create_event(message,v.comm,true)
else
log("ERROR","No message with id "..v.id)
end
else
log("ERROR","No channel with id "..v.channel)
end
end
end
local event = command("event",{ local event = command("event",{
help = {embed={ help = {embed={
title = "Add a cron event", title = "Add a cron event",
@ -46,48 +126,7 @@ local event = command("event",{
"administrator" "administrator"
}, },
exec = function(msg,args,opts) exec = function(msg,args,opts)
local arg = table.concat(args," ") return create_event(msg,table.concat(args," "))
local func,functype = cron.parse_line(arg)
if not func then
msg:reply(functype)
return false
end
local hash = md5.sumhexa(arg):sub(1,16)
if functype == "directive" then
local event_name = arg:match("^@(%w+)")
if not events.event[event_name] then events.event[event_name] = {} end
events.event[event_name][hash] = {
func,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
if not config.events.event[event_name] then config.events.event[event_name] = {} end
config.events.event[event_name][hash] = {
arg,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
else
events.timer[hash] = {
func,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
config.events.timer[hash] = {
arg,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
end
return true
end end
}) })
plugin:add_command(event) plugin:add_command(event)
@ -108,32 +147,12 @@ local delay = command("delay",{
local format = args[1] local format = args[1]
table.remove(args,1) table.remove(args,1)
local arg = os.date("%d.%m.%y %H:%M ",cron.convert_delay(format))..table.concat(args," ") local arg = os.date("%d.%m.%y %H:%M ",cron.convert_delay(format))..table.concat(args," ")
local func,functype = cron.parse_line(arg) return create_event(msg,arg)
if not func then
msg:reply(functype)
return false
end
local hash = md5.sumhexa(arg):sub(1,16)
events.timer[hash] = {
func,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
config.events.timer[hash] = {
arg,
channel = tostring(msg.channel.id),
id = tostring(msg.id),
user = tostring(msg.author.id),
type = functype
}
return true
end end
}) })
plugin:add_command(delay) plugin:add_command(delay)
local delay = command("events",{ local events_comm = command("events",{
help = {embed={ help = {embed={
title = "View your running events", title = "View your running events",
description = "nuff said.", description = "nuff said.",
@ -162,8 +181,8 @@ local delay = command("events",{
end end
end end
local stop = false local stop = false
for k,v in pairs(config.events.event) do for _,evtype in pairs(config.events.event) do
for _,events in pairs(v) do for k,v in pairs(evtype) do
if v.user == tostring(msg.author.id) then if v.user == tostring(msg.author.id) then
table.insert(uevents,v) table.insert(uevents,v)
table.insert(uhashes,k) table.insert(uhashes,k)
@ -188,17 +207,55 @@ local delay = command("events",{
if not uhashes[I] then if not uhashes[I] then
break break
end end
message.embed.description = message.embed.description.."["..uhashes[I].."] `"..uevents[I][1].."`\n" message.embed.description = message.embed.description.."["..uhashes[I].."] `"..uevents[I].comm.."`\n"
end end
msg:reply(message) msg:reply(message)
end end
}) })
plugin:add_command(delay) plugin:add_command(events_comm)
local remove_event= command("remove-event",{
help = {embed={
title = "Remove an event",
description = "nuff said.",
fields = {
{name = "Usage:",value = "remove-event <id>"},
{name = "Perms:",value = "administrator"},
}
}},
perms = {
"administrator"
},
args = {
"string"
},
exec = function(msg,args,opts)
for k,v in pairs(config.events.timer) do
if k == args[1] then
config.events.timer[k] = nil
events.timer[k] = nil
return true
end
end
for evname,evtype in pairs(config.events.event) do
for k,v in pairs(evtype) do
if k == args[1] then
config.events.event[evname][k] = nil
events.event[evname][k] = nil
return true
end
end
end
msg:reply("Not found")
return false
end
})
plugin:add_command(remove_event)
local timer = discordia.Clock() local timer = discordia.Clock()
timer:on("min",function() timer:on("min",function()
for k,v in pairs(events.timer) do for k,v in pairs(events.timer) do
local status,command = v[1](os.date("*t")) local status,command = v.comm(os.date("*t"))
if status then if status then
exec(v,command) exec(v,command)
if v.type == "onetime" then if v.type == "onetime" then
@ -210,16 +267,20 @@ timer:on("min",function()
end) end)
client:on("messageCreate",function(msg) client:on("messageCreate",function(msg)
if (not msg.guild) or (tostring(msg.guild.id) ~= tostring(id)) then
return
end
local content = msg.content local content = msg.content
local user = msg.author.name local user = msg.author.id
local channelid = msg.channel.id
for k,v in pairs(events.event.message or {}) do for k,v in pairs(events.event.message or {}) do
local status,command = v[1]({content,user}) local status,command = v.comm({content,user,channelid})
if status then if status then
exec(v,command) exec(v,command)
end end
end end
for k,v in pairs(events.event.messageOnce or {}) do for k,v in pairs(events.event.messageOnce or {}) do
local status,command = v[1]({content,user}) local status,command = v.comm({content,user,channelid})
events.event.messageOnce[k] = nil events.event.messageOnce[k] = nil
config.events.event.messageOnce[k] = nil config.events.event.messageOnce[k] = nil
end end