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/setandget.lua

73 lines
2.3 KiB
Lua
Raw Normal View History

local setandget = {}
2016-03-15 21:37:19 +01:00
local utilities = require('utilities')
function setandget:init(config)
self.database.setandget = self.database.setandget or {}
setandget.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('set', true):t('get', true).table
setandget.doc = [[```
]]..config.cmd_pat..[[set <name> <value>
Stores a value with the given name. Use "]]..config.cmd_pat..[[set <name> --" to delete the stored value.
]]..config.cmd_pat..[[get [name]
2016-03-15 21:37:19 +01:00
Returns the stored value or a list of stored values.
```]]
end
2016-03-15 21:37:19 +01:00
setandget.command = 'set <name> <value>'
2016-03-15 21:37:19 +01:00
function setandget:action(msg, config)
2016-03-15 21:37:19 +01:00
local input = utilities.input(msg.text)
self.database.setandget[msg.chat.id_str] = self.database.setandget[msg.chat.id_str] or {}
2016-03-15 21:37:19 +01:00
if msg.text_lower:match('^'..config.cmd_pat..'set') then
2016-03-15 21:37:19 +01:00
if not input then
utilities.send_message(self, msg.chat.id, setandget.doc, true, nil, true)
2016-03-15 21:37:19 +01:00
return
end
local name = utilities.get_word(input:lower(), 1)
local value = utilities.input(input)
2016-03-15 21:37:19 +01:00
if not name or not value then
utilities.send_message(self, msg.chat.id, setandget.doc, true, nil, true)
2016-03-15 21:37:19 +01:00
elseif value == '--' or value == '' then
self.database.setandget[msg.chat.id_str][name] = nil
utilities.send_message(self, msg.chat.id, 'That value has been deleted.')
2016-03-15 21:37:19 +01:00
else
self.database.setandget[msg.chat.id_str][name] = value
utilities.send_message(self, msg.chat.id, '"' .. name .. '" has been set to "' .. value .. '".', true)
2016-03-15 21:37:19 +01:00
end
elseif msg.text_lower:match('^'..config.cmd_pat..'get') then
2016-03-15 21:37:19 +01:00
if not input then
local output
if utilities.table_size(self.database.setandget[msg.chat.id_str]) == 0 then
2016-03-15 21:37:19 +01:00
output = 'No values have been stored here.'
else
output = '*List of stored values:*\n'
for k,v in pairs(self.database.setandget[msg.chat.id_str]) do
2016-03-15 21:37:19 +01:00
output = output .. '' .. k .. ': `' .. v .. '`\n'
end
end
utilities.send_message(self, msg.chat.id, output, true, nil, true)
2016-03-15 21:37:19 +01:00
return
end
local output
if self.database.setandget[msg.chat.id_str][input:lower()] then
output = '`' .. self.database.setandget[msg.chat.id_str][input:lower()] .. '`'
2016-03-15 21:37:19 +01:00
else
output = 'There is no value stored by that name.'
end
utilities.send_message(self, msg.chat.id, output, true, nil, true)
2016-03-15 21:37:19 +01:00
end
end
return setandget