Compare commits

..

2 Commits

Author SHA1 Message Date
Yessiest 3dd57d3bdc Added event orphaning for old hash types 2022-05-20 18:23:26 +04:00
Yessiest e431755aad Added new events 2022-05-20 18:22:54 +04:00
2 changed files with 161 additions and 27 deletions

126
plugins/cron/events.lua Normal file
View File

@ -0,0 +1,126 @@
local replace = function(text,args)
local text = text
for k,v in pairs(args) do
text = text:gsub(k,v)
end
return text
end
local trigger = function(evtbl,args,vars)
for k,v in pairs(events.event[evtbl] or {}) do
local status,command = v.comm(args)
if status then
exec(v,replace(command,vars))
end
end
end
local triggerOnce = function(evname,args,vars)
for k,v in pairs(events.event[evname] or {}) do
local status,command = v.comm(args)
if status then
exec(v,replace(command,vars))
events.event[evname][k] = nil
config.events.event[evname][k] = nil
end
end
end
client:on("messageCreate",function(msg)
if (not msg.guild) or (tostring(msg.guild.id) ~= tostring(id)) then
return
end
local content = msg.content
local user = msg.author.id
local channelid = msg.channel.id
local args = {
["%$USER"] = user,
["%$USERNAME"] = msg.author.name,
["%$CHANNEL"] = channelid,
["%$CONTENT"] = msg.content
}
-- @message: content, userId, channelId, $USER, $USERNAME, $CHANNEL, $CONTENT
trigger("message",{content,user,channelid},args)
-- @messageOnce: content, userId, channelId, $USER, $USERNAME, $CHANNEL, $CONTENT
triggerOnce("messageOnce",{content,user,channelid},args)
end)
client:on("userBan",function(user,guild)
if tostring(guild.id) ~= tostring(id) then
return
end
args = {
["%$USER"] = user.id,
["%$USERNAME"] = user.name
}
-- @ban: userId, $USER, $USERNAME
trigger("ban",{user.id},args)
-- @banOnce: userId, $USER, $USERNAME
triggerOnce("banOnce",{user.id},args)
end)
client:on("userUnban",function(user,guild)
if tostring(guild.id) ~= tostring(id) then
return
end
args = {
["%$USER"] = user.id,
["%$USERNAME"] = user.name
}
-- @unban: userId, $USER, $USERNAME
trigger("unban",{user.id},args)
-- @unbanOnce: userId, $USER, $USERNAME
triggerOnce("unbanOnce",{user.id},args)
end)
client:on("memberJoin", function(member)
if tostring(member.guild.id) ~= tostring(id) then
return
end
args = {
["%$USER"] = member.id,
["%$USERNAME"] = member.name,
["%$AGE"] = member.user.createdAt,
["%$DISCRIM"] = member.user.discriminator,
["%$TAG"] = member.user.tag
}
-- @join: userid, username, age, $USER, $USERNAME, $AGE, $DISCRIM, $TAG
trigger("join",{
member.id,
member.name,
member.user.createdAt
},args)
-- @joinOnce: userid, username, age, $USER, $USERNAME, $AGE, $DISCRIM, $TAG
triggerOnce("joinOnce",{
member.id,
member.name,
member.user.createdAt
},args)
end)
client:on("memberLeave", function(member)
if tostring(member.guild.id) ~= tostring(id) then
return
end
args = {
["%$USER"] = member.id,
["%$USERNAME"] = member.name,
["%$AGE"] = member.user.createdAt,
["%$DISCRIM"] = member.user.discriminator,
["%$TAG"] = member.user.tag,
["%$GUILDTIME"] = member.joinedAt,
["%$ROLE"] = member.highestRole.name,
}
--@leave: userid, username, role, $USER, $USERNAME, $AGE, $DISCRIM, $TAG, $GUILDTIME, $ROLE
trigger("leave",{
member.id,
member.name,
member.highestRole.name,
},args)
--@leave: userid, username, role, $USER, $USERNAME, $AGE, $DISCRIM, $TAG, $GUILDTIME, $ROLE
triggerOnce("leaveOnce",{
member.id,
member.name,
member.highestRole.name,
},args)
end)

View File

@ -12,12 +12,16 @@ local events = {
local exec = function(v,command) local exec = function(v,command)
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 timer channel: "..tostring(v.channel)) log("ERROR","Unable to retrieve event channel: "..tostring(v.channel))
return return
end end
local msg = channel:getMessage(v.id) local msg = channel:getMessage(v.id)
if not msg then if not msg then
log("ERROR","Unable to retrieve timer message: "..tostring(v.id)) log("ERROR","Unable to retrieve event message: "..tostring(v.id))
return
end
if not msg.member then
log("ERROR","Unable to retrieve event creator: "..tostring(v.user.id))
return return
end end
command_handler:handle(fake_message(msg,{ command_handler:handle(fake_message(msg,{
@ -77,7 +81,7 @@ local create_event = function(msg,cronjob,create_entry)
type = functype type = functype
} }
end end
return true return true,hash
end end
local get_user_events = function(author_id,page) local get_user_events = function(author_id,page)
@ -149,7 +153,14 @@ for k,v in pairs(config.events.timer) do
if channel then if channel then
local message = channel:getMessage(v.id) local message = channel:getMessage(v.id)
if message then if message then
create_event(message,v.comm,true) local status,hash = create_event(message,v.comm,true)
--orphan events with mismatching hashes
if status and (hash ~= k) then
log("WARNING", "Hash mismatch, orpahning event.")
events.timer[k] = nil
config.events.timer[k] = nil
create_event(message,v.comm)
end
else else
log("ERROR","No message with id "..v.id) log("ERROR","No message with id "..v.id)
end end
@ -166,7 +177,14 @@ for _,evtype in pairs(config.events.event) do
if channel then if channel then
local message = channel:getMessage(v.id) local message = channel:getMessage(v.id)
if message then if message then
create_event(message,v.comm,true) local status,hash = create_event(message,v.comm,true)
--orphan events with mismatching hashes
if status and (hash ~= k) then
log("WARNING", "Hash mismatch, orpahning event.")
events.event[_][k] = nil
config.events.event[_][k] = nil
create_event(message,v.comm)
end
else else
log("ERROR","No message with id "..v.id) log("ERROR","No message with id "..v.id)
end end
@ -341,28 +359,18 @@ timer:on("min",function()
end end
end) end)
client:on("messageCreate",function(msg) --load events file
if (not msg.guild) or (tostring(msg.guild.id) ~= tostring(id)) then local fhandler = io.open("./plugins/cron/events.lua","r")
return local data = fhandler:read("*a")
end fhandler:close()
local content = msg.content local eventfunc = load(data,"event loader: ./plugins/cron/events.lua",nil,setmetatable({
local user = msg.author.id id = id,
local channelid = msg.channel.id client = client,
for k,v in pairs(events.event.message or {}) do exec = exec,
local status,command = v.comm({content,user,channelid}) events = events,
if status then config = config
exec(v,command) },{__index = _G}))
end eventfunc()
end
for k,v in pairs(events.event.messageOnce or {}) do
local status,command = v.comm({content,user,channelid})
if status then
exec(v,command)
events.event.messageOnce[k] = nil
config.events.event.messageOnce[k] = nil
end
end
end)
timer:start(true) timer:start(true)
return plugin return plugin