Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F621168
filter.lua
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
filter.lua
View Options
--[[
mattata v2.0 - Filter Plugin
]]
local
plugin
=
{}
plugin
.
name
=
'filter'
plugin
.
category
=
'admin'
plugin
.
description
=
'Add content filters to the group'
plugin
.
commands
=
{
'filter'
,
'addfilter'
}
plugin
.
help
=
'/filter <pattern> [action] - Adds a filter. Actions: delete (default), warn, ban, kick.'
plugin
.
group_only
=
true
plugin
.
admin_only
=
true
local
VALID_ACTIONS
=
{
delete
=
true
,
warn
=
true
,
ban
=
true
,
kick
=
true
,
mute
=
true
}
function
plugin
.
on_message
(
api
,
message
,
ctx
)
local
tools
=
require
(
'telegram-bot-lua.tools'
)
if
not
message
.
args
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Usage: /filter <pattern> [action]
\n
Actions: delete (default), warn, ban, kick, mute'
,
'html'
)
end
local
pattern
,
action
if
message
.
args
:
match
(
'^(.+)%s+(delete|warn|ban|kick|mute)$'
)
then
pattern
,
action
=
message
.
args
:
match
(
'^(.+)%s+(delete|warn|ban|kick|mute)$'
)
else
pattern
=
message
.
args
action
=
'delete'
end
pattern
=
pattern
:
match
(
'^%s*(.-)%s*$'
)
-- trim
if
pattern
==
''
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Please provide a pattern to filter.'
)
end
-- Validate regex pattern
local
ok
=
pcall
(
string.match
,
''
,
pattern
)
if
not
ok
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Invalid pattern. Please provide a valid Lua pattern.'
)
end
-- Check for duplicate
local
existing
=
ctx
.
db
.
execute
(
'SELECT id FROM filters WHERE chat_id = $1 AND pattern = $2'
,
{
message
.
chat
.
id
,
pattern
}
)
if
existing
and
#
existing
>
0
then
-- Update the action if filter already exists
ctx
.
db
.
execute
(
'UPDATE filters SET action = $1 WHERE chat_id = $2 AND pattern = $3'
,
{
action
,
message
.
chat
.
id
,
pattern
}
)
require
(
'src.core.session'
).
invalidate_cached_list
(
message
.
chat
.
id
,
'filters'
)
return
api
.
send_message
(
message
.
chat
.
id
,
string.format
(
'Filter <code>%s</code> updated with action: <b>%s</b>.'
,
tools
.
escape_html
(
pattern
),
action
),
'html'
)
end
ctx
.
db
.
insert
(
'filters'
,
{
chat_id
=
message
.
chat
.
id
,
pattern
=
pattern
,
action
=
action
,
created_by
=
message
.
from
.
id
})
-- Invalidate filter cache
require
(
'src.core.session'
).
invalidate_cached_list
(
message
.
chat
.
id
,
'filters'
)
api
.
send_message
(
message
.
chat
.
id
,
string.format
(
'Filter added: <code>%s</code> (action: <b>%s</b>)'
,
tools
.
escape_html
(
pattern
),
action
),
'html'
)
end
return
plugin
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 14, 6:50 PM (16 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
136079
Default Alt Text
filter.lua (2 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline