Added purge command

This commit is contained in:
Yessiest 2022-05-10 03:35:39 +04:00
parent a492d03ecc
commit 01f52daab9
3 changed files with 78 additions and 6 deletions

View File

@ -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. 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 -- Adjustments for lua5.1
if _VERSION=="Lua 5.1" then if _VERSION=="Lua 5.1" then
table.unpack = unpack table.unpack = unpack
@ -124,7 +129,7 @@ local predtypes = {
end}, end},
{"^/([^/]*)/$","regex",function(regex) {"^/([^/]*)/$","regex",function(regex)
return function(input) return function(input)
return (tostring(input):match(regex) ~= nil) return (safe_regex(tostring(input),regex) ~= nil)
end end
end}, end},
{"^\"([^\"]*)\"$","string",function(str) {"^\"([^\"]*)\"$","string",function(str)

View File

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

View File

@ -5,6 +5,11 @@ local plugin = pluginc("help")
local db = sql.open(server.config_path.."sec.sqlite") 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 if not db:rowexec("SELECT name FROM sqlite_master WHERE type='table' AND name='infractions'") then
db:exec [[ db:exec [[
CREATE TABLE infractions(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, desc TEXT, action TEXT, timestamp INTEGER); 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.", description = "nuff said.",
fields = { fields = {
{name = "Usage:",value = "warn <user> <reason>"}, {name = "Usage:",value = "warn <user> <reason>"},
{name = "Perms:",value = "kick_members"}, {name = "Perms:",value = "kickMembers"},
} }
}}, }},
perms = { perms = {
"kick_members" "kickMembers"
}, },
args = { args = {
"member", "member",
@ -99,12 +104,12 @@ local infractions = command("infractions", {
description = "Infractions include kicks, bans, mutes and warnings.", description = "Infractions include kicks, bans, mutes and warnings.",
fields = { fields = {
{name = "Usage: ", value = "infractions <user> [<startfrom>]"}, {name = "Usage: ", value = "infractions <user> [<startfrom>]"},
{name = "Perms: ", value = "kick_members"}, {name = "Perms: ", value = "kickMembers"},
{name = "Options: ", value = "--type=(warn default,ban,kick)"} {name = "Options: ", value = "--type=(warn default,ban,kick)"}
} }
}}, }},
perms = { perms = {
"kick_members" "kickMembers"
}, },
args = { args = {
"member", "member",
@ -144,4 +149,66 @@ local infractions = command("infractions", {
}) })
plugin:add_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 return plugin