Page MenuHomePhabricator (Chris)

allowlist.lua
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

allowlist.lua

--[[
mattata v2.0 - Allowlist Plugin
]]
local plugin = {}
plugin.name = 'allowlist'
plugin.category = 'admin'
plugin.description = 'Manage the group allowlist'
plugin.commands = { 'allowlist' }
plugin.help = '/allowlist add <user> - Adds a user to the allowlist. /allowlist remove <user> - Removes a user. /allowlist - Lists allowlisted users.'
plugin.group_only = true
plugin.admin_only = true
function plugin.on_message(api, message, ctx)
local tools = require('telegram-bot-lua.tools')
if not message.args then
-- List allowlisted users
local result = ctx.db.execute(
"SELECT user_id FROM chat_members WHERE chat_id = $1 AND role = 'allowlisted'",
{ message.chat.id }
)
if not result or #result == 0 then
return api.send_message(message.chat.id, 'No users are allowlisted.\nUsage: /allowlist add <user>')
end
local output = '<b>Allowlisted users:</b>\n\n'
for _, row in ipairs(result) do
local info = api.get_chat(row.user_id)
local name = info and info.result and tools.escape_html(info.result.first_name) or tostring(row.user_id)
output = output .. string.format('- <a href="tg://user?id=%s">%s</a> [%s]\n', row.user_id, name, row.user_id)
end
return api.send_message(message.chat.id, output, 'html')
end
local action, target = message.args:lower():match('^(%S+)%s+(.+)$')
if not action then
return api.send_message(message.chat.id, 'Usage: /allowlist <add|remove> <user>')
end
-- Resolve target user
local user_id
if message.reply and message.reply.from then
user_id = message.reply.from.id
else
user_id = target:match('^@?(%S+)')
if tonumber(user_id) == nil then
user_id = ctx.redis.get('username:' .. user_id:lower())
end
end
user_id = tonumber(user_id)
if not user_id then
return api.send_message(message.chat.id, 'I couldn\'t find that user. Try replying to their message or providing a valid username/ID.')
end
if action == 'add' then
ctx.db.upsert('chat_members', {
chat_id = message.chat.id,
user_id = user_id,
role = 'allowlisted'
}, { 'chat_id', 'user_id' }, { 'role' })
local target_info = api.get_chat(user_id)
local target_name = target_info and target_info.result and tools.escape_html(target_info.result.first_name) or tostring(user_id)
api.send_message(message.chat.id, string.format(
'<a href="tg://user?id=%d">%s</a> has been added to the allowlist.',
user_id, target_name
), 'html')
elseif action == 'remove' or action == 'del' or action == 'delete' then
ctx.db.execute(
"UPDATE chat_members SET role = 'member' WHERE chat_id = $1 AND user_id = $2 AND role = 'allowlisted'",
{ message.chat.id, user_id }
)
local target_info = api.get_chat(user_id)
local target_name = target_info and target_info.result and tools.escape_html(target_info.result.first_name) or tostring(user_id)
api.send_message(message.chat.id, string.format(
'<a href="tg://user?id=%d">%s</a> has been removed from the allowlist.',
user_id, target_name
), 'html')
else
api.send_message(message.chat.id, 'Usage: /allowlist <add|remove> <user>')
end
end
return plugin

File Metadata

Mime Type
text/html
Expires
Sat, Nov 14, 6:46 PM (1 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
126433
Default Alt Text
allowlist.lua (3 KB)

Event Timeline