diff --git a/plugins/cron/events.lua b/plugins/cron/events.lua new file mode 100644 index 0000000..5f4b71c --- /dev/null +++ b/plugins/cron/events.lua @@ -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) diff --git a/plugins/cron/init.lua b/plugins/cron/init.lua index 285e266..3c5daeb 100644 --- a/plugins/cron/init.lua +++ b/plugins/cron/init.lua @@ -12,12 +12,16 @@ local events = { local exec = function(v,command) local channel = client:getChannel(v.channel) 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 end local msg = channel:getMessage(v.id) 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 end command_handler:handle(fake_message(msg,{ @@ -341,28 +345,18 @@ timer:on("min",function() 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 - for k,v in pairs(events.event.message or {}) do - local status,command = v.comm({content,user,channelid}) - if status then - exec(v,command) - end - 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) +--load events file +local fhandler = io.open("./plugins/cron/events.lua","r") +local data = fhandler:read("*a") +fhandler:close() +local eventfunc = load(data,"event loader: ./plugins/cron/events.lua",nil,setmetatable({ + id = id, + client = client, + exec = exec, + events = events, + config = config +},{__index = _G})) +eventfunc() timer:start(true) return plugin