This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/otouto/plugins/channel.lua

71 lines
2.4 KiB
Lua
Raw Normal View History

local channel = {}
2016-06-07 06:31:34 +02:00
local bindings = require('otouto.bindings')
local utilities = require('otouto.utilities')
function channel:init(config)
2016-08-14 04:46:18 +02:00
channel.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('ch', true).table
channel.command = 'ch <channel> \\n <message>'
channel.doc = config.cmd_pat .. [[ch <channel>
<message>
Sends a message to a channel. Channel may be specified via ID or username. Messages are markdown-enabled. Users may only send messages to channels for which they are the owner or an administrator.
The following markdown syntax is supported:
*bold text*
_italic text_
[text](URL)
`inline fixed-width code`
```pre-formatted fixed-width code block```]]
end
2016-06-07 09:02:05 +02:00
function channel:action(msg, config)
2016-08-14 04:46:18 +02:00
local input = utilities.input(msg.text)
if not input then
utilities.send_reply(msg, channel.doc, 'html')
return
end
local chat_id = utilities.get_word(input, 1)
local chat, t = bindings.getChat{chat_id = chat_id}
if not chat then
utilities.send_reply(msg, 'Sorry, I was unable to retrieve information for that channel.\n`' .. t.description .. '`', true)
return
elseif chat.result.type ~= 'channel' then
utilities.send_reply(msg, 'Sorry, that group does not appear to be a channel.')
return
end
local admin_list, t = bindings.getChatAdministrators{ chat_id = chat_id }
if not admin_list then
utilities.send_reply(msg, 'Sorry, I was unable to retrieve a list of administrators for that channel.\n`' .. t.description .. '`', true)
return
end
local is_admin = false
for _, admin in ipairs(admin_list.result) do
if admin.user.id == msg.from.id then
is_admin = true
2016-08-14 04:46:18 +02:00
end
end
if not is_admin then
utilities.send_reply(msg, 'Sorry, you do not appear to be an administrator for that channel.')
return
end
local text = input:match('\n(.+)')
if not text then
utilities.send_reply(msg, 'Please enter a message to be sent on a new line. Markdown is supported.')
return
end
local success, result = utilities.send_message(chat_id, text, true, nil, true)
if success then
utilities.send_reply(msg, 'Your message has been sent!')
2016-08-14 04:46:18 +02:00
else
utilities.send_reply(msg, 'Sorry, I was unable to send your message.\n`' .. result.description .. '`', true)
2016-08-14 04:46:18 +02:00
end
end
return channel