Browse Source

Added purge command

main
Yessiest 2 years ago
parent
commit
01f52daab9
  1. 7
      libraries/cron.lua
  2. 2
      plugins/cron/init.lua
  3. 75
      plugins/security/init.lua

7
libraries/cron.lua

@ -9,6 +9,11 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local safe_regex = function(str,pattern)
local status,ret = pcall(string.match,str,pattern)
if status then return ret end
end
-- Adjustments for lua5.1
if _VERSION=="Lua 5.1" then
table.unpack = unpack
@ -124,7 +129,7 @@ local predtypes = {
end},
{"^/([^/]*)/$","regex",function(regex)
return function(input)
return (tostring(input):match(regex) ~= nil)
return (safe_regex(tostring(input),regex) ~= nil)
end
end},
{"^\"([^\"]*)\"$","string",function(str)

2
plugins/cron/init.lua

@ -116,7 +116,7 @@ end
local event = command("event",{
help = {embed={
title = "Add a cron event",
description = "Description coming soon",
description = "https://github.com/512mb-org/512mb.org-bot/wiki/Events-and-cronjobs",
fields = {
{name = "Usage:",value = "event ..."},
{name = "Perms:",value = "administrator"},

75
plugins/security/init.lua

@ -5,6 +5,11 @@ local plugin = pluginc("help")
local db = sql.open(server.config_path.."sec.sqlite")
local safe_regex = function(str,pattern)
local status,ret = pcall(string.match,str,pattern)
if status then return ret end
end
if not db:rowexec("SELECT name FROM sqlite_master WHERE type='table' AND name='infractions'") then
db:exec [[
CREATE TABLE infractions(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, desc TEXT, action TEXT, timestamp INTEGER);
@ -65,11 +70,11 @@ local warn = command("warn",{
description = "nuff said.",
fields = {
{name = "Usage:",value = "warn <user> <reason>"},
{name = "Perms:",value = "kick_members"},
{name = "Perms:",value = "kickMembers"},
}
}},
perms = {
"kick_members"
"kickMembers"
},
args = {
"member",
@ -99,12 +104,12 @@ local infractions = command("infractions", {
description = "Infractions include kicks, bans, mutes and warnings.",
fields = {
{name = "Usage: ", value = "infractions <user> [<startfrom>]"},
{name = "Perms: ", value = "kick_members"},
{name = "Perms: ", value = "kickMembers"},
{name = "Options: ", value = "--type=(warn default,ban,kick)"}
}
}},
perms = {
"kick_members"
"kickMembers"
},
args = {
"member",
@ -144,4 +149,66 @@ local infractions = command("infractions", {
})
plugin:add_command(infractions)
local purge = command("purge",{
help = { embed = {
title = "Purge a number of messages",
description = "nuff said.",
fields = {
{name = "Usage: ", value = "purge <number>"},
{name = "Perms: ", value = "manageMessages"},
{name = "Options: ", value = "`--regex (regex)` - match content against regex; \n`--user (user)` - match user against id/name; \n`-w` - match webhook messages"}
}
}},
perms = {
"manageMessages"
},
args = {
"number"
},
exec = function(msg,args,opts)
local messages = {}
local messageCount = args[1]
local deletedMessageCount = 0
local last_id = nil
local matchfunc = function(v)
last_id = v.id
local matches = true
if opts["regex"] and (not (
(type(v.content) == "string") and
(safe_regex(v.content,opts["regex"])))) then
matches = false
end
if opts["user"] and (not (
(v.author.id and (tostring(v.author.id) == opts["user"])) or
(v.author.name == opts["user"]))) then
matches = false
end
if opts["w"] and (not v.webhookId) then
matches = false
end
if matches then
table.insert(messages,v.id)
deletedMessageCount = deletedMessageCount + 1
end
end
local messages_fetched = msg.channel:getMessages(args[1]%100)
if messages_fetched then
messages_fetched:forEach(matchfunc)
end
msg.channel:bulkDelete(messages)
messageCount = messageCount-(args[1]%100)
while messageCount > 0 do
messages = {}
messages_fetched = msg.channel:getMessagesAfter(last_id,100)
if messages_fetched then
messages_fetched:forEach(matchfunc)
end
msg.channel:bulkDelete(messages)
messageCount = messageCount - 100
end
msg:reply("Deleted "..tostring(deletedMessageCount).." messages.")
end
})
plugin:add_command(purge)
return plugin
Loading…
Cancel
Save