- Quotes portiert

- Pasteee portiert
- Roll portiert
- post_petition() verfügbar
This commit is contained in:
Andreas Bielawski 2016-06-14 13:14:09 +02:00
parent b3bf7f7c42
commit 0ab125d8b7
5 changed files with 245 additions and 56 deletions

View File

@ -1,56 +0,0 @@
local dice = {}
local utilities = require('otouto.utilities')
dice.command = 'roll <nDr>'
function dice:init(config)
dice.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('roll', true).table
dice.doc = [[```
]]..config.cmd_pat..[[roll <nDr>
Returns a set of dice rolls, where n is the number of rolls and r is the range. If only a range is given, returns only one roll.
```]]
end
function dice:action(msg)
local input = utilities.input(msg.text_lower)
if not input then
utilities.send_message(self, msg.chat.id, dice.doc, true, msg.message_id, true)
return
end
local count, range
if input:match('^[%d]+d[%d]+$') then
count, range = input:match('([%d]+)d([%d]+)')
elseif input:match('^d?[%d]+$') then
count = 1
range = input:match('^d?([%d]+)$')
else
utilities.send_message(self, msg.chat.id, dice.doc, true, msg.message_id, true)
return
end
count = tonumber(count)
range = tonumber(range)
if range < 2 then
utilities.send_reply(self, msg, 'The minimum range is 2.')
return
end
if range > 1000 or count > 1000 then
utilities.send_reply(self, msg, 'The maximum range and count are 1000.')
return
end
local output = '*' .. count .. 'd' .. range .. '*\n`'
for _ = 1, count do
output = output .. math.random(range) .. '\t'
end
output = output .. '`'
utilities.send_message(self, msg.chat.id, output, true, msg.message_id, true)
end
return dice

View File

@ -0,0 +1,45 @@
local pasteee = {}
local bot = require('otouto.bot')
local utilities = require('otouto.utilities')
function pasteee:init(config)
if not cred_data.pasteee_key then
print('Missing config value: pasteee_key.')
print('pasteee.lua will not be enabled, listquotes won\'t be available.')
return
end
pasteee.triggers = {
"^/pasteee (.*)$"
}
pasteee.doc = [[*
]]..config.cmd_pat..[[pasteee* _<Text>_: Postet Text auf Paste.ee]]
end
pasteee.command = 'pasteee <Text>'
local key = cred_data.pasteee_key
function upload(text, noraw)
local url = "https://paste.ee/api"
local pet = post_petition(url, 'key='..key..'&paste='..text..'&format=json')
if pet.status ~= 'success' then return 'Ein Fehler ist aufgetreten: '..pet.error, true end
if noraw then
return pet.paste.link
else
return pet.paste.raw
end
end
function pasteee:action(msg, config, matches)
local text = matches[1]
local link, iserror = upload(text)
if iserror then
utilities.send_reply(self, msg, link)
return
end
utilities.send_reply(self, msg, '[Text auf Paste.ee ansehen]('..link..')', true)
end
return pasteee

111
otouto/plugins/quotes.lua Normal file
View File

@ -0,0 +1,111 @@
local quotes = {}
local bot = require('otouto.bot')
local utilities = require('otouto.utilities')
local redis = (loadfile "./otouto/redis.lua")()
require("./otouto/plugins/pasteee")
function quotes:init(config)
quotes.triggers = {
"^/(delquote) (.+)$",
"^/(addquote) (.+)$",
"^/(quote)$",
"^/(listquotes)$"
}
quotes.doc = [[*
]]..config.cmd_pat..[[addquote* _<Zitat>_: Fügt Zitat hinzu.
*]]..config.cmd_pat..[[delquote* _<Zitat>_: Löscht das Zitat (nur Superuser)
*]]..config.cmd_pat..[[quote*: Gibt zufälliges Zitat aus
*]]..config.cmd_pat..[[listquotes*: Listet alle Zitate auf
]]
end
quotes.command = 'quote'
function quotes:save_quote(msg)
if msg.text:sub(11):isempty() then
return "Benutzung: /addquote [Zitat]"
end
local quote = msg.text:sub(11)
local hash = get_redis_hash(msg, 'quotes')
print('Saving quote to redis set '..hash)
redis:sadd(hash, quote)
return '*Gespeichert!*'
end
function quotes:delete_quote(msg)
if msg.text:sub(11):isempty() then
return "Benutzung: /delquote [Zitat]"
end
local quote = msg.text:sub(11)
local hash = get_redis_hash(msg, 'quotes')
print('Deleting quote from redis set '..hash)
if redis:sismember(hash, quote) == true then
redis:srem(hash, quote)
return '*Zitat erfolgreich gelöscht!*'
else
return 'Dieses Zitat existiert nicht.'
end
end
function quotes:get_quote(msg)
local hash = get_redis_hash(msg, 'quotes')
if hash then
print('Getting quote from redis set '..hash)
local quotes_table = redis:smembers(hash)
if not quotes_table[1] then
return 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit /addquote [Zitat]'
else
return quotes_table[math.random(1,#quotes_table)]
end
end
end
function quotes:list_quotes(msg)
local hash = get_redis_hash(msg, 'quotes')
if hash then
print('Getting quotes from redis set '..hash)
local quotes_table = redis:smembers(hash)
local text = ""
for num,quote in pairs(quotes_table) do
text = text..num..") "..quote..'\n'
end
if not text or text == "" then
return 'Es wurden noch keine Zitate gespeichert.\nSpeichere doch welche mit !addquote [Zitat]'
else
return upload(text)
end
end
end
function quotes:action(msg, config, matches)
if matches[1] == "quote" then
utilities.send_message(self, msg.chat.id, quotes:get_quote(msg), true)
return
elseif matches[1] == "addquote" and matches[2] then
utilities.send_reply(self, msg, quotes:save_quote(msg), true)
return
elseif matches[1] == "delquote" and matches[2] then
if msg.from.id ~= config.admin then
utilities.send_reply(self, msg, config.errors.sudo)
return
end
utilities.send_reply(self, msg, quotes:delete_quote(msg), true)
return
elseif matches[1] == "listquotes" then
local link, iserror = quotes:list_quotes(msg)
if iserror then
utilities.send_reply(self, msg, link)
return
end
utilities.send_reply(self, msg, '[Lise aller Zitate auf Paste.ee ansehen]('..link..')', true)
return
end
utilities.send_reply(self, msg, quotes.doc, true)
end
return quotes

32
otouto/plugins/roll.lua Normal file
View File

@ -0,0 +1,32 @@
local roll = {}
local utilities = require('otouto.utilities')
roll.command = 'roll'
roll.doc = 'roll'
function roll:init(config)
roll.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('roll', true).table
roll.doc = [[*
]]..config.cmd_pat..[[roll*: Werfe einen Würfel*]]
end
local canroll = {
"1",
"2",
"3",
"4",
"5",
"6"
}
function roll:roll_dice()
local randomroll = math.random(6)
return canroll[randomroll]
end
function roll:action(msg)
utilities.send_reply(self, msg, 'Du hast eine *'..roll:roll_dice()..'* gewürfelt.', true)
end
return roll

View File

@ -8,6 +8,8 @@ local ltn12 = require('ltn12')
local HTTPS = require('ssl.https')
local URL = require('socket.url')
local JSON = require('dkjson')
local http = require('socket.http')
local https = require('ssl.https')
local serpent = require("serpent")
local bindings = require('otouto.bindings')
local redis = (loadfile "./otouto/redis.lua")()
@ -211,6 +213,17 @@ local lc_list = {
['!'] = 'ǃ'
}
-- Retruns true if the string is empty
function string:isempty()
return self == nil or self == ''
end
-- Retruns true if the string is blank
function string:isblank()
self = self:trim()
return self:isempty()
end
function get_name(msg)
local name = msg.from.first_name
if name == nil then
@ -582,6 +595,50 @@ function match_pattern(pattern, text)
-- nil
end
function post_petition(url, arguments, headers)
local url, h = string.gsub(url, "http://", "")
local url, hs = string.gsub(url, "https://", "")
local post_prot = "http"
if hs == 1 then
post_prot = "https"
end
local response_body = {}
local request_constructor = {
url = post_prot..'://'..url,
method = "POST",
sink = ltn12.sink.table(response_body),
headers = headers or {},
redirect = false
}
local source = arguments
if type(arguments) == "table" then
local source = helpers.url_encode_arguments(arguments)
end
if not headers then
request_constructor.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF8"
request_constructor.headers["X-Accept"] = "application/json"
request_constructor.headers["Accept"] = "application/json"
end
request_constructor.headers["Content-Length"] = tostring(#source)
request_constructor.source = ltn12.source.string(source)
if post_prot == "http" then
ok, response_code, response_headers, response_status_line = http.request(request_constructor)
else
ok, response_code, response_headers, response_status_line = https.request(request_constructor)
end
if not ok then
return nil
end
response_body = JSON.decode(table.concat(response_body))
return response_body, response_headers
end
function get_redis_hash(msg, var)
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
return 'chat:'..msg.chat.id..':'..var