Moved utilities.CMD_PAT to config.cmd_pat.

This commit is contained in:
Brayden Banks 2016-05-26 20:28:44 -07:00
parent ed6b536a46
commit bb8b8b9c64
52 changed files with 385 additions and 379 deletions

116
README.md
View File

@ -35,29 +35,29 @@ When you are ready to start the bot, run `./launch.sh`. To stop the bot, send "/
Note that certain plugins, such as translate.lua and greetings.lua, will require privacy mode to be disabled. Additionally, some plugins may require or make use of various API keys:
- bing.lua: [Bing Search API](http://datamarket.azure.com/dataset/bing/search) key (`bing_api_key`)
- gImages.lua & youtube.lua: Google [API](http://console.developers.google.com) and [CSE](https://cse.google.com/cse) keys (`google_api_key`, `google_cse_key`)
- weather.lua: [OpenWeatherMap](http://openweathermap.org) API key (`owm_api_key`)
- lastfm.lua: [last.fm](http://last.fm/api) API key (`lastfm_api_key`)
- bible.lua: [Biblia](http://api.biblia.com) API key (`biblia_api_key`)
- cats.lua: [The Cat API](http://thecatapi.com) API key (optional) (`thecatapi_key`)
- apod.lua: [NASA](http://api.nasa.gov) API key (`nasa_api_key`)
- translate.lua: [Yandex](http://tech.yandex.com/keys/get) API key (`yandex_key`)
- chatter.lua: [SimSimi](http://developer.simsimi.com/signUp) API key (`simsimi_key`)
- `bing.lua`: [Bing Search API](http://datamarket.azure.com/dataset/bing/search) key (`bing_api_key`)
- `gImages.lua` & `youtube.lua`: Google [API](http://console.developers.google.com) and [CSE](https://cse.google.com/cse) keys (`google_api_key`, `google_cse_key`)
- `weather.lua`: [OpenWeatherMap](http://openweathermap.org) API key (`owm_api_key`)
- `lastfm.lua`: [last.fm](http://last.fm/api) API key (`lastfm_api_key`)
- `bible.lua`: [Biblia](http://api.biblia.com) API key (`biblia_api_key`)
- `cats.lua`: [The Cat API](http://thecatapi.com) API key (optional) (`thecatapi_key`)
- `apod.lua`: [NASA](http://api.nasa.gov) API key (`nasa_api_key`)
- `translate.lua`: [Yandex](http://tech.yandex.com/keys/get) API key (`yandex_key`)
- `chatter.lua`: [SimSimi](http://developer.simsimi.com/signUp) API key (`simsimi_key`)
* * *
## Control plugins
Some plugins are designed to be used by the bot's owner. Here are some examples, how they're used, and what they do.
| Plugin | Command | Function |
|:--------------|:-----------|:---------------------------------------------------|
| control.lua | /reload | Reloads all plugins and configuration. |
| | /halt | Shuts down the bot after saving the database. |
| | /script | Runs a list a bot commands, separated by newlines. |
| blacklist.lua | /blacklist | Blocks people from using the bot. |
| shell.lua | /run | Executes shell commands on the host system. |
| luarun.lua | /lua | Executes Lua commands in the bot's environment. |
| Plugin | Command | Function |
|:----------------|:-----------|:---------------------------------------------------|
| `control.lua` | /reload | Reloads all plugins and configuration. |
| | /halt | Shuts down the bot after saving the database. |
| | /script | Runs a list a bot commands, separated by newlines. |
| `blacklist.lua` | /blacklist | Blocks people from using the bot. |
| `shell.lua` | /run | Executes shell commands on the host system. |
| `luarun.lua` | /lua | Executes Lua commands in the bot's environment. |
* * *
@ -68,7 +68,7 @@ To get started, run `./tg-install.sh`. Note that this script is written for Ubun
Once the installation is finished, enable the `administration` plugin in your config file. **The administration plugin must be loaded before the `about` and `blacklist` plugins.** You may have reason to change the default TCP port (4567); if that is the case, remember to change it in `tg-launch.sh` as well. Run `./tg-launch.sh` in a separate screen/tmux window. You'll have to enter your phone number and go through the login process the first time. The script is set to restart tg after two seconds, so you'll need to Ctrl+C after exiting.
While tg is running, you may start/reload otouto with administration.lua enabled, and have access to a wide variety of administrative commands and automata. The administration "database" is stored in `administration.json`. To start using otouto to administrate a group (note that you must be the owner (or an administrator)), send `/gadd` to that group. For a list of commands, use `/ahelp`. Below I'll describe various functions now available to you.
While tg is running, you may start/reload otouto with `administration.lua` enabled, and have access to a wide variety of administrative commands and automata. The administration "database" is stored in `administration.json`. To start using otouto to administrate a group (note that you must be the owner (or an administrator)), send `/gadd` to that group. For a list of commands, use `/ahelp`. Below I'll describe various functions now available to you.
| Command | Function | Privilege | Internal? |
|:------------|:------------------------------------------------|:----------|:----------|
@ -151,46 +151,46 @@ Additionally, antiflood can be configured to automatically ban a user after he h
## List of plugins
| Plugin | Command | Function | Aliases |
|:--------------------|:------------------------------|:--------------------------------------------------------|:--------|
| help.lua | /help [command] | Returns a list of commands or command-specific help. | /h |
| about.lua | /about | Returns the about text as configured in config.lua. |
| ping.lua | /ping | The simplest plugin ever! |
| echo.lua | /echo text | Repeats a string of text. |
| bing.lua | /bing query | Returns Bing web results. | /g |
| gImages.lua | /images query | Returns a Google image result. | /i |
| gMaps.lua | /location query | Returns location data from Google Maps. | /loc |
| youtube.lua | /youtube query | Returns the top video result from YouTube. | /yt |
| wikipedia.lua | /wikipedia query | Returns the summary of a Wikipedia article. | /w |
| lastfm.lua | /np [username] | Returns the song you are currently listening to. |
| lastfm.lua | /fmset [username] | Sets your username for /np. /fmset -- will delete it. |
| hackernews.lua | /hackernews | Returns the latest posts from Hacker News. | /hn |
| imdb.lua | /imdb query | Returns film information from IMDb. |
| hearthstone.lua | /hearthstone query | Returns data for Hearthstone cards matching the query. | /hs |
| calc.lua | /calc expression | Returns conversions and solutions to math expressions. |
| bible.lua | /bible reference | Returns a Bible verse. | /b |
| urbandictionary.lua | /urban query | Returns the top definition from Urban Dictionary. | /ud |
| time.lua | /time query | Returns the time, date, and a timezone for a location. |
| weather.lua | /weather query | Returns current weather conditions for a given location. |
| nick.lua | /nick nickname | Set your nickname. /nick - will delete it. |
| whoami.lua | /whoami | Returns user and chat info for you or the replied-to user. | /who |
| eightball.lua | /8ball | Returns an answer from a magic 8-ball. |
| dice.lua | /roll nDr | Returns RNG dice rolls. Uses D&D notation. |
| reddit.lua | /reddit [r/subreddit ¦ query] | Returns the top results from a subreddit, query, or r/all. | /r |
| xkcd.lua | /xkcd [query] | Returns an xkcd strip and its alt text. |
| slap.lua | /slap target | Gives someone a slap (or worse). |
| commit.lua | /commit | Returns a commit message from whatthecommit.com. |
| fortune.lua | /fortune | Returns a UNIX fortune. |
| pun.lua | /pun | Returns a pun. |
| pokedex.lua | /pokedex query | Returns a Pokedex entry. | /dex |
| currency.lua | /cash [amount] cur to cur | Converts one currency to another. |
| cats.lua | /cat | Returns a cat picture. |
| reactions.lua | /reactions | Returns a list of emoticons which can be posted by the bot. |
| apod.lua | /apod [date] | Returns the NASA Astronomy Picture of the Day. |
| dilbert.lua | /dilbert [date] | Returns a Dilbert strip. |
| patterns.lua | /s/from/to/ | Search-and-replace using Lua patterns. |
| me.lua | /me | Returns user-specific data stored by the bot. |
| remind.lua | /remind <duration> <message> | Reminds a user of something after a duration of minutes. |
| Plugin | Command | Function | Aliases |
|:----------------------|:------------------------------|:--------------------------------------------------------|:--------|
| `help.lua` | /help [command] | Returns a list of commands or command-specific help. | /h |
| `about.lua` | /about | Returns the about text as configured in config.lua. |
| `ping.lua` | /ping | The simplest plugin ever! |
| `echo.lua` | /echo text | Repeats a string of text. |
| `bing.lua` | /bing query | Returns Bing web results. | /g |
| `gImages.lua` | /images query | Returns a Google image result. | /i |
| `gMaps.lua` | /location query | Returns location data from Google Maps. | /loc |
| `youtube.lua` | /youtube query | Returns the top video result from YouTube. | /yt |
| `wikipedia.lua` | /wikipedia query | Returns the summary of a Wikipedia article. | /w |
| `lastfm.lua` | /np [username] | Returns the song you are currently listening to. |
| `lastfm.lua` | /fmset [username] | Sets your username for /np. /fmset -- will delete it. |
| `hackernews.lua` | /hackernews | Returns the latest posts from Hacker News. | /hn |
| `imdb.lua` | /imdb query | Returns film information from IMDb. |
| `hearthstone.lua` | /hearthstone query | Returns data for Hearthstone cards matching the query. | /hs |
| `calc.lua` | /calc expression | Returns conversions and solutions to math expressions. |
| `bible.lua` | /bible reference | Returns a Bible verse. | /b |
| `urbandictionary.lua` | /urban query | Returns the top definition from Urban Dictionary. | /ud |
| `time.lua` | /time query | Returns the time, date, and a timezone for a location. |
| `weather.lua` | /weather query | Returns current weather conditions for a given location. |
| `nick.lua` | /nick nickname | Set your nickname. /nick - will delete it. |
| `whoami.lua` | /whoami | Returns user and chat info for you or the replied-to user. | /who |
| `eightball.lua` | /8ball | Returns an answer from a magic 8-ball. |
| `dice.lua` | /roll nDr | Returns RNG dice rolls. Uses D&D notation. |
| `reddit.lua` | /reddit [r/subreddit ¦ query] | Returns the top results from a subreddit, query, or r/all. | /r |
| `xkcd.lua` | /xkcd [query] | Returns an xkcd strip and its alt text. |
| `slap.lua` | /slap target | Gives someone a slap (or worse). |
| `commit.lua` | /commit | Returns a commit message from whatthecommit.com. |
| `fortune.lua` | /fortune | Returns a UNIX fortune. |
| `pun.lua` | /pun | Returns a pun. |
| `pokedex.lua` | /pokedex query | Returns a Pokedex entry. | /dex |
| `currency.lua` | /cash [amount] cur to cur | Converts one currency to another. |
| `cats.lua` | /cat | Returns a cat picture. |
| `reactions.lua` | /reactions | Returns a list of emoticons which can be posted by the bot. |
| `apod.lua` | /apod [date] | Returns the NASA Astronomy Picture of the Day. |
| `dilbert.lua` | /dilbert [date] | Returns a Dilbert strip. |
| `patterns.lua` | /s/from/to/ | Search-and-replace using Lua patterns. |
| `me.lua` | /me | Returns user-specific data stored by the bot. |
| `remind.lua` | /remind <duration> <message> | Reminds a user of something after a duration of minutes. |
* * *

11
bot.lua
View File

@ -61,14 +61,14 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
msg = utilities.enrich_message(msg)
if msg.text:match('^'..utilities.CMD_PAT..'start .+') then
msg.text = utilities.CMD_PAT .. utilities.input(msg.text)
if msg.text:match('^'..config.cmd_pat..'start .+') then
msg.text = config.cmd_pat .. utilities.input(msg.text)
msg.text_lower = msg.text:lower()
end
for _,v in ipairs(self.plugins) do
for _,w in pairs(v.triggers) do
if string.match(msg.text:lower(), w) then
if string.match(msg.text_lower, w) then
local success, result = pcall(function()
return v.action(self, msg, config)
end)
@ -90,8 +90,7 @@ function bot:on_msg_receive(msg, config) -- The fn run whenever a message is rec
end
function bot:run()
local config = require('config') -- Load configuration file.
function bot:run(config)
bot.init(self, config) -- Actually start the script.
while self.is_started do -- Start a loop while the bot should be running.
@ -113,7 +112,7 @@ function bot:run()
utilities.save_data(self.info.username..'.db', self.database) -- Save the database.
for i,v in ipairs(self.plugins) do
if v.cron then -- Call each plugin's cron function, if it has one.
local result, err = pcall(function() v.cron(self) end)
local result, err = pcall(function() v.cron(self, config) end)
if not result then
utilities.handle_exception(self, err, 'CRON: ' .. i, config)
end

View File

@ -18,6 +18,8 @@ I am otouto, the plugin-wielding, multipurpose Telegram bot.
Send /help to get started.
]],
-- The symbol that starts a command. Usually noted as '/' in documentation.
cmd_pat = '/',
-- https://datamarket.azure.com/dataset/bing/search
bing_api_key = '',

View File

@ -1,5 +1,6 @@
local bot = require('bot')
local instance = {}
local config = require('config')
return bot.run(instance)
return bot.run(instance, config)

View File

@ -19,9 +19,9 @@ function about:action(msg, config)
local output = config.about_text .. '\nBased on otouto v'..bot.version..' by topkecleon.'
if (msg.new_chat_participant and msg.new_chat_participant.id == self.info.id)
or msg.text_lower:match('^'..utilities.CMD_PAT..'about')
or msg.text_lower:match('^'..utilities.CMD_PAT..'about@'..self.info.username:lower())
or msg.text_lower:match('^'..utilities.CMD_PAT..'start') then
or msg.text_lower:match('^'..config.cmd_pat..'about')
or msg.text_lower:match('^'..config.cmd_pat..'about@'..self.info.username:lower())
or msg.text_lower:match('^'..config.cmd_pat..'start') then
utilities.send_message(self, msg.chat.id, output, true)
return
end

View File

@ -66,17 +66,20 @@ function administration:init(config)
drua.PORT = config.cli_port or 4567
administration.init_flags(config.cmd_pat)
administration.init_command(self, config)
administration.doc = '`Returns a list of administrated groups.\nUse '..config.cmd_pat..'ahelp for more administrative commands.`'
end
administration.flags = {
function administration.init_flags(cmd_pat) return {
[1] = {
name = 'unlisted',
desc = 'Removes this group from the group listing.',
short = 'This group is unlisted.',
enabled = 'This group is no longer listed in '..utilities.CMD_PAT..'groups.',
disabled = 'This group is now listed in '..utilities.CMD_PAT..'groups.'
enabled = 'This group is no longer listed in '..cmd_pat..'groups.',
disabled = 'This group is now listed in '..cmd_pat..'groups.'
},
[2] = {
name = 'antisquig',
@ -105,7 +108,7 @@ administration.flags = {
name = 'antiflood',
desc = 'Prevents flooding by rate-limiting messages per user.',
short = 'This group automatically removes users who flood.',
enabled = 'Users will now be removed automatically for excessive messages. Use '..utilities.CMD_PAT..'antiflood to configure limits.',
enabled = 'Users will now be removed automatically for excessive messages. Use '..cmd_pat..'antiflood to configure limits.',
disabled = 'Users will no longer be removed automatically for excessive messages.',
kicked = 'You were automatically kicked from GROUPNAME for flooding.'
},
@ -116,7 +119,7 @@ administration.flags = {
enabled = 'This group will no longer remove users for being globally banned.',
disabled = 'This group will now remove users for being globally banned.'
}
}
} end
administration.antiflood = {
text = 10,
@ -197,7 +200,7 @@ function administration:mod_format(id)
return output
end
function administration:get_desc(chat_id)
function administration:get_desc(chat_id, config)
local group = self.database.administration.groups[tostring(chat_id)]
local t = {}
@ -237,12 +240,12 @@ function administration:get_desc(chat_id)
if modstring ~= '' then
table.insert(t, '*Moderators:*\n' .. utilities.trim(modstring))
end
table.insert(t, 'Run '..utilities.CMD_PAT..'ahelp@' .. self.info.username .. ' for a list of commands.')
table.insert(t, 'Run '..config..'ahelp@' .. self.info.username .. ' for a list of commands.')
return table.concat(t, '\n\n')
end
function administration:update_desc(chat)
function administration:update_desc(chat, config)
local group = self.database.administration.groups[tostring(chat)]
local desc = 'Welcome to ' .. group.name .. '!\n'
if group.motd then desc = desc .. group.motd .. '\n' end
@ -250,7 +253,7 @@ function administration:update_desc(chat)
local gov = self.database.users[tostring(group.governor)]
desc = desc .. '\nGovernor: ' .. utilities.build_name(gov.first_name, gov.last_name) .. ' [' .. gov.id .. ']\n'
end
local s = '\n'..utilities.CMD_PAT..'desc@' .. self.info.username .. ' for more information.'
local s = '\n'..config.cmd_pat..'desc@' .. self.info.username .. ' for more information.'
desc = desc:sub(1, 250-s:len()) .. s
drua.channel_set_about(chat, desc)
end
@ -400,7 +403,7 @@ function administration.init_command(self_, config)
else
group.name = msg.new_chat_title
if group.grouptype == 'supergroup' then
administration.update_desc(self, msg.chat.id)
administration.update_desc(self, msg.chat.id, config)
end
end
elseif msg.new_chat_photo then
@ -461,7 +464,7 @@ function administration.init_command(self_, config)
end
if msg.new_chat_participant and not new_user.do_kick then
local output = administration.get_desc(self, msg.chat.id)
local output = administration.get_desc(self, msg.chat.id, config)
utilities.send_message(self, msg.new_chat_participant.id, output, true, nil, true)
end
@ -481,7 +484,7 @@ function administration.init_command(self_, config)
},
{ -- /groups
triggers = utilities.triggers(self_.info.username):t('groups').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('groups').table,
command = 'groups',
privilege = 1,
@ -510,7 +513,7 @@ function administration.init_command(self_, config)
},
{ -- /ahelp
triggers = utilities.triggers(self_.info.username):t('ahelp', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('ahelp', true).table,
command = 'ahelp \\[command]',
privilege = 1,
@ -521,11 +524,11 @@ function administration.init_command(self_, config)
local rank = administration.get_rank(self, msg.from.id, msg.chat.id, config)
local input = utilities.get_word(msg.text_lower, 2)
if input then
input = input:gsub('^'..utilities.CMD_PAT..'', '')
input = input:gsub('^'..config.cmd_pat..'', '')
local doc
for _,action in ipairs(administration.commands) do
if action.keyword == input then
doc = ''..utilities.CMD_PAT..'' .. action.command:gsub('\\','') .. '\n' .. action.doc
doc = ''..config.cmd_pat..'' .. action.command:gsub('\\','') .. '\n' .. action.doc
break
end
end
@ -533,14 +536,14 @@ function administration.init_command(self_, config)
local output = '*Help for* _' .. input .. '_ :\n```\n' .. doc .. '\n```'
utilities.send_message(self, msg.chat.id, output, true, nil, true)
else
local output = 'Sorry, there is no help for that command.\n'..utilities.CMD_PAT..'ahelp@'..self.info.username
local output = 'Sorry, there is no help for that command.\n'..config.cmd_pat..'ahelp@'..self.info.username
utilities.send_reply(self, msg, output)
end
else
local output = '*Commands for ' .. administration.ranks[rank] .. ':*\n'
for i = 1, rank do
for _, val in ipairs(self.admin_temp.help[i]) do
output = output .. '' .. utilities.CMD_PAT .. val .. '\n'
output = output .. '' .. config.cmd_pat .. val .. '\n'
end
end
output = output .. 'Arguments: <required> \\[optional]'
@ -556,7 +559,7 @@ function administration.init_command(self_, config)
},
{ -- /ops
triggers = utilities.triggers(self_.info.username):t('ops'):t('oplist').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('ops'):t('oplist').table,
command = 'ops',
privilege = 1,
@ -586,7 +589,7 @@ function administration.init_command(self_, config)
},
{ -- /desc
triggers = utilities.triggers(self_.info.username):t('desc'):t('description').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('desc'):t('description').table,
command = 'description',
privilege = 1,
@ -594,7 +597,7 @@ function administration.init_command(self_, config)
doc = 'Returns a description of the group (in a private message), including its motd, rules, flags, governor, and moderators.',
action = function(self, msg, group, config)
local output = administration.get_desc(self, msg.chat.id)
local output = administration.get_desc(self, msg.chat.id, config)
if utilities.send_message(self, msg.from.id, output, true, nil, true) then
if msg.from.id ~= msg.chat.id then
utilities.send_reply(self, msg, 'I have sent you the requested information in a private message.')
@ -606,7 +609,7 @@ function administration.init_command(self_, config)
},
{ -- /rules
triggers = utilities.triggers(self_.info.username):t('rules?', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('rules?', true).table,
command = 'rules \\[i]',
privilege = 1,
@ -634,7 +637,7 @@ function administration.init_command(self_, config)
},
{ -- /motd
triggers = utilities.triggers(self_.info.username):t('motd').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('motd').table,
command = 'motd',
privilege = 1,
@ -651,7 +654,7 @@ function administration.init_command(self_, config)
},
{ -- /link
triggers = utilities.triggers(self_.info.username):t('link').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('link').table,
command = 'link',
privilege = 1,
@ -668,7 +671,7 @@ function administration.init_command(self_, config)
},
{ -- /kickme
triggers = utilities.triggers(self_.info.username):t('leave'):t('kickme').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('leave'):t('kickme').table,
command = 'kickme',
privilege = 1,
@ -689,7 +692,7 @@ function administration.init_command(self_, config)
},
{ -- /kick
triggers = utilities.triggers(self_.info.username):t('kick', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('kick', true).table,
command = 'kick <user>',
privilege = 2,
@ -713,7 +716,7 @@ function administration.init_command(self_, config)
},
{ -- /ban
triggers = utilities.triggers(self_.info.username):t('ban', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('ban', true).table,
command = 'ban <user>',
privilege = 2,
@ -737,7 +740,7 @@ function administration.init_command(self_, config)
},
{ -- /unban
triggers = utilities.triggers(self_.info.username):t('unban', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('unban', true).table,
command = 'unban <user>',
privilege = 2,
@ -763,7 +766,7 @@ function administration.init_command(self_, config)
},
{ -- /setrules
triggers = utilities.triggers(self_.info.username):t('setrules', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('setrules', true).table,
command = 'setrules <rules>',
privilege = 3,
@ -771,7 +774,7 @@ function administration.init_command(self_, config)
doc = 'Sets the group\'s rules. Rules will be automatically numbered. Separate rules with a new line. Markdown is supported. Pass "--" to delete the rules.',
action = function(self, msg, group, config)
local input = msg.text:match('^'..utilities.CMD_PAT..'setrules[@'..self.info.username..']*(.+)')
local input = msg.text:match('^'..config.cmd_pat..'setrules[@'..self.info.username..']*(.+)')
if input == ' --' or input == ' ' .. utilities.char.em_dash then
group.rules = {}
utilities.send_reply(self, msg, 'The rules have been cleared.')
@ -793,7 +796,7 @@ function administration.init_command(self_, config)
},
{ -- /changerule
triggers = utilities.triggers(self_.info.username):t('changerule', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('changerule', true).table,
command = 'changerule <i> <rule>',
privilege = 3,
@ -802,7 +805,7 @@ function administration.init_command(self_, config)
action = function(self, msg, group, config)
local input = utilities.input(msg.text)
local output = 'usage: `'..utilities.CMD_PAT..'changerule <i> <newrule>`'
local output = 'usage: `'..config.cmd_pat..'changerule <i> <newrule>`'
if input then
local rule_num = tonumber(input:match('^%d+'))
local new_rule = utilities.input(input)
@ -830,7 +833,7 @@ function administration.init_command(self_, config)
},
{ -- /setmotd
triggers = utilities.triggers(self_.info.username):t('setmotd', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('setmotd', true).table,
command = 'setmotd <motd>',
privilege = 2,
@ -853,7 +856,7 @@ function administration.init_command(self_, config)
utilities.send_message(self, msg.chat.id, output, true, nil, true)
end
if group.grouptype == 'supergroup' then
administration.update_desc(self, msg.chat.id)
administration.update_desc(self, msg.chat.id, config)
end
else
utilities.send_reply(self, msg, 'Please specify the new message of the day.')
@ -862,7 +865,7 @@ function administration.init_command(self_, config)
},
{ -- /setlink
triggers = utilities.triggers(self_.info.username):t('setlink', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('setlink', true).table,
command = 'setlink <link>',
privilege = 3,
@ -885,7 +888,7 @@ function administration.init_command(self_, config)
},
{ -- /alist
triggers = utilities.triggers(self_.info.username):t('alist').table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('alist').table,
command = 'alist',
privilege = 3,
@ -903,7 +906,7 @@ function administration.init_command(self_, config)
},
{ -- /flags
triggers = utilities.triggers(self_.info.username):t('flags?', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('flags?', true).table,
command = 'flag \\[i]',
privilege = 3,
@ -935,7 +938,7 @@ function administration.init_command(self_, config)
},
{ -- /antiflood
triggers = utilities.triggers(self_.info.username):t('antiflood', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('antiflood', true).table,
command = 'antiflood \\[<type> <i>]',
privilege = 3,
@ -944,7 +947,7 @@ function administration.init_command(self_, config)
action = function(self, msg, group, config)
if not group.flags[5] then
utilities.send_message(self, msg.chat.id, 'antiflood is not enabled. Use `'..utilities.CMD_PAT..'flag 5` to enable it.', true, nil, true)
utilities.send_message(self, msg.chat.id, 'antiflood is not enabled. Use `'..config.cmd_pat..'flag 5` to enable it.', true, nil, true)
else
if not group.antiflood then
group.antiflood = JSON.decode(JSON.encode(administration.antiflood))
@ -963,7 +966,7 @@ function administration.init_command(self_, config)
output = '*' .. key:gsub('^%l', string.upper) .. '* messages are now worth *' .. val .. '* points.'
end
else
output = 'usage: `'..utilities.CMD_PAT..'antiflood <type> <i>`\nexample: `'..utilities.CMD_PAT..'antiflood text 5`\nUse this command to configure the point values for each message type. When a user reaches 100 points, he is kicked. The points are reset each minute. The current values are:\n'
output = 'usage: `'..config.cmd_pat..'antiflood <type> <i>`\nexample: `'..config.cmd_pat..'antiflood text 5`\nUse this command to configure the point values for each message type. When a user reaches 100 points, he is kicked. The points are reset each minute. The current values are:\n'
for k,v in pairs(group.antiflood) do
output = output .. '*'..k..':* `'..v..'`\n'
end
@ -975,7 +978,7 @@ function administration.init_command(self_, config)
},
{ -- /mod
triggers = utilities.triggers(self_.info.username):t('mod', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('mod', true).table,
command = 'mod <user>',
privilege = 3,
@ -1002,7 +1005,7 @@ function administration.init_command(self_, config)
},
{ -- /demod
triggers = utilities.triggers(self_.info.username):t('demod', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('demod', true).table,
command = 'demod <user>',
privilege = 3,
@ -1028,7 +1031,7 @@ function administration.init_command(self_, config)
},
{ -- /gov
triggers = utilities.triggers(self_.info.username):t('gov', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('gov', true).table,
command = 'gov <user>',
privilege = 4,
@ -1050,14 +1053,14 @@ function administration.init_command(self_, config)
end
if group.grouptype == 'supergroup' then
drua.channel_set_admin(msg.chat.id, target.id, 2)
administration.update_desc(self, msg.chat.id)
administration.update_desc(self, msg.chat.id, config)
end
end
end
},
{ -- /degov
triggers = utilities.triggers(self_.info.username):t('degov', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('degov', true).table,
command = 'degov <user>',
privilege = 4,
@ -1077,14 +1080,14 @@ function administration.init_command(self_, config)
end
if group.grouptype == 'supergroup' then
drua.channel_set_admin(msg.chat.id, target.id, 0)
administration.update_desc(self, msg.chat.id)
administration.update_desc(self, msg.chat.id, config)
end
end
end
},
{ -- /hammer
triggers = utilities.triggers(self_.info.username):t('hammer', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('hammer', true).table,
command = 'hammer <user>',
privilege = 4,
@ -1120,7 +1123,7 @@ function administration.init_command(self_, config)
},
{ -- /unhammer
triggers = utilities.triggers(self_.info.username):t('unhammer', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('unhammer', true).table,
command = 'unhammer <user>',
privilege = 4,
@ -1144,7 +1147,7 @@ function administration.init_command(self_, config)
},
{ -- /admin
triggers = utilities.triggers(self_.info.username):t('admin', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('admin', true).table,
command = 'admin <user>',
privilege = 5,
@ -1171,7 +1174,7 @@ function administration.init_command(self_, config)
},
{ -- /deadmin
triggers = utilities.triggers(self_.info.username):t('deadmin', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('deadmin', true).table,
command = 'deadmin <user>',
privilege = 5,
@ -1199,7 +1202,7 @@ function administration.init_command(self_, config)
},
{ -- /gadd
triggers = utilities.triggers(self_.info.username):t('gadd', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('gadd', true).table,
command = 'gadd [i] ...',
privilege = 5,
@ -1238,7 +1241,7 @@ function administration.init_command(self_, config)
autokicks = {},
autoban = 3
}
administration.update_desc(self, msg.chat.id)
administration.update_desc(self, msg.chat.id, config)
table.insert(self.database.administration.activity, msg.chat.id_str)
utilities.send_reply(self, msg, 'I am now administrating this group.')
drua.channel_set_admin(msg.chat.id, self.info.id, 2)
@ -1247,7 +1250,7 @@ function administration.init_command(self_, config)
},
{ -- /grem
triggers = utilities.triggers(self_.info.username):t('grem', true):t('gremove', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('grem', true):t('gremove', true).table,
command = 'gremove \\[chat]',
privilege = 5,
@ -1278,7 +1281,7 @@ function administration.init_command(self_, config)
},
{ -- /glist
triggers = utilities.triggers(self_.info.username):t('glist', false).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('glist', false).table,
command = 'glist',
privilege = 5,
@ -1307,7 +1310,7 @@ function administration.init_command(self_, config)
},
{ -- /broadcast
triggers = utilities.triggers(self_.info.username):t('broadcast', true).table,
triggers = utilities.triggers(self_.info.username, config.cmd_pat):t('broadcast', true).table,
command = 'broadcast <message>',
privilege = 5,
@ -1389,6 +1392,5 @@ function administration:cron()
end
administration.command = 'groups'
administration.doc = '`Returns a list of administrated groups.\nUse '..utilities.CMD_PAT..'ahelp for more administrative commands.`'
return administration

View File

@ -8,20 +8,20 @@ local URL = require('socket.url')
local utilities = require('utilities')
apod.command = 'apod [date]'
apod.doc = [[```
]]..utilities.CMD_PAT..[[apod [query]
function apod:init(config)
apod.triggers = utilities.triggers(self.info.username, config.cmd_pat)
:t('apod', true):t('apodhd', true):t('apodtext', true).table
apod.doc = [[```
]]..config.cmd_pat..[[apod [query]
Returns the Astronomy Picture of the Day.
If the query is a date, in the format YYYY-MM-DD, the APOD of that day is returned.
]]..utilities.CMD_PAT..[[apodhd [query]
]]..config.cmd_pat..[[apodhd [query]
Returns the image in HD, if available.
]]..utilities.CMD_PAT..[[apodtext [query]
]]..config.cmd_pat..[[apodtext [query]
Returns the explanation of the APOD.
Source: nasa.gov
```]]
function apod:init()
apod.triggers = utilities.triggers(self.info.username)
:t('apod', true):t('apodhd', true):t('apodtext', true).table
end
function apod:action(msg, config)
@ -65,13 +65,13 @@ function apod:action(msg, config)
local img_url = jdat.url
if string.match(msg.text, '^'..utilities.CMD_PAT..'apodhd*') then
if string.match(msg.text, '^'..config.cmd_pat..'apodhd*') then
img_url = jdat.hdurl or jdat.url
end
local output = date .. '[' .. jdat.title .. '](' .. img_url .. ')'
if string.match(msg.text, '^'..utilities.CMD_PAT..'apodtext*') then
if string.match(msg.text, '^'..config.cmd_pat..'apodtext*') then
output = output .. '\n' .. jdat.explanation
disable_page_preview = true
end

View File

@ -3,13 +3,13 @@ local bandersnatch = {}
local utilities = require('utilities')
bandersnatch.command = 'bandersnatch'
bandersnatch.doc = [[```
Shun the frumious Bandersnatch.
Alias: ]]..utilities.CMD_PAT..[[bc
```]]
function bandersnatch:init()
bandersnatch.triggers = utilities.triggers(self.info.username):t('bandersnatch'):t('bc').table
function bandersnatch:init(config)
bandersnatch.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('bandersnatch'):t('bc').table
bandersnatch.doc = [[```
Shun the frumious Bandersnatch.
Alias: ]]..config.cmd_pat..[[bc
```]]
end
local fullnames = { "Wimbledon Tennismatch", "Rinkydink Curdlesnoot", "Butawhiteboy Cantbekhan", "Benadryl Claritin", "Bombadil Rivendell", "Wanda's Crotchfruit", "Biblical Concubine", "Syphilis Cankersore", "Buckminster Fullerene", "Bourgeoisie Capitalist" }

View File

@ -11,15 +11,15 @@ function bible:init(config)
return
end
bible.triggers = utilities.triggers(self.info.username):t('bible', true):t('b', true).table
bible.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('bible', true):t('b', true).table
bible.doc = [[```
]]..config.cmd_pat..[[bible <reference>
Returns a verse from the American Standard Version of the Bible, or an apocryphal verse from the King James Version. Results from biblia.com.
Alias: ]]..config.cmd_pat..[[b
```]]
end
bible.command = 'bible <reference>'
bible.doc = [[```
]]..utilities.CMD_PAT..[[bible <reference>
Returns a verse from the American Standard Version of the Bible, or an apocryphal verse from the King James Version. Results from biblia.com.
Alias: ]]..utilities.CMD_PAT..[[b
```]]
function bible:action(msg, config)

View File

@ -19,7 +19,7 @@ function blacklist:action(msg, config)
if self.database.blacklist[msg.from.id_str] then return end
if self.database.blacklist[msg.chat.id_str] then return end
if not msg.text:match('^'..utilities.CMD_PAT..'blacklist') then return true end
if not msg.text:match('^'..config.cmd_pat..'blacklist') then return true end
if msg.from.id ~= config.admin then return end
local target = utilities.user_from_message(self, msg)

View File

@ -5,13 +5,13 @@ local HTTPS = require('ssl.https')
local utilities = require('utilities')
calc.command = 'calc <expression>'
calc.doc = [[```
]]..utilities.CMD_PAT..[[calc <expression>
function calc:init(config)
calc.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('calc', true).table
calc.doc = [[```
]]..config.cmd_pat..[[calc <expression>
Returns solutions to mathematical expressions and conversions between common units. Results provided by mathjs.org.
```]]
function calc:init()
calc.triggers = utilities.triggers(self.info.username):t('calc', true).table
end
function calc:action(msg, config)

View File

@ -9,7 +9,7 @@ function cats:init(config)
print('cats.lua will be enabled, but there are more features with a key.')
end
cats.triggers = utilities.triggers(self.info.username):t('cat').table
cats.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('cat').table
end
cats.command = 'cat'

View File

@ -6,7 +6,6 @@ local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')
function chatter:init(config)
if not config.simsimi_key then
@ -34,7 +33,7 @@ function chatter:action(msg, config)
--Uncomment the following line for Al Gore-like conversation.
--or (msg.reply_to_message and msg.reply_to_message.from.id == self.info.id)
)
or msg.text:match('^'..utilities.CMD_PAT)
or msg.text:match('^'..config.CMD_PAT)
or msg.text == ''
) then
return true

View File

@ -7,8 +7,8 @@ local utilities = require('utilities')
commit.command = 'commit'
commit.doc = '`Returns a commit message from whatthecommit.com.`'
function commit:init()
commit.triggers = utilities.triggers(self.info.username):t('commit').table
function commit:init(config)
commit.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('commit').table
end
local commits = {

View File

@ -3,9 +3,12 @@ local control = {}
local bot = require('bot')
local utilities = require('utilities')
function control:init()
control.triggers = utilities.triggers(self.info.username):t('reload'):t('halt').table
table.insert(control.triggers, '^'..utilities.CMD_PAT..'script')
local cmd_pat -- Prevents the command from being uncallable.
function control:init(config)
cmd_pat = config.cmd_pat
control.triggers = utilities.triggers(self.info.username, cmd_pat,
{'^'..cmd_pat..'script'}):t('reload', true):t('halt').table
end
function control:action(msg, config)
@ -16,7 +19,7 @@ function control:action(msg, config)
if msg.date < os.time() - 1 then return end
if msg.text:match('^'..utilities.CMD_PAT..'reload') then
if msg.text_lower:match('^'..cmd_pat..'reload') then
for pac, _ in pairs(package.loaded) do
if pac:match('^plugins%.') then
package.loaded[pac] = nil
@ -25,18 +28,18 @@ function control:action(msg, config)
package.loaded['bindings'] = nil
package.loaded['utilities'] = nil
package.loaded['config'] = nil
for k, v in pairs(require('config')) do
if msg.text_lower:match('%+config') then for k, v in pairs(require('config')) do
config[k] = v
end
end end
bot.init(self, config)
utilities.send_reply(self, msg, 'Bot reloaded!')
elseif msg.text:match('^'..utilities.CMD_PAT..'halt') then
elseif msg.text_lower:match('^'..cmd_pat..'halt') then
self.is_started = false
utilities.send_reply(self, msg, 'Stopping bot!')
elseif msg.text:match('^'..utilities.CMD_PAT..'script') then
local input = msg.text:match('^'..utilities.CMD_PAT..'script\n(.+)')
elseif msg.text_lower:match('^'..cmd_pat..'script') then
local input = msg.text_lower:match('^'..cmd_pat..'script\n(.+)')
if not input then
utilities.send_reply(self, msg, 'usage: ```\n'..utilities.CMD_PAT..'script\n'..utilities.CMD_PAT..'command <arg>\n...\n```', true)
utilities.send_reply(self, msg, 'usage: ```\n'..cmd_pat..'script\n'..cmd_pat..'command <arg>\n...\n```', true)
return
end
input = input .. '\n'

View File

@ -4,15 +4,15 @@ local HTTPS = require('ssl.https')
local utilities = require('utilities')
currency.command = 'cash [amount] <from> to <to>'
currency.doc = [[```
]]..utilities.CMD_PAT..[[cash [amount] <from> to <to>
Example: ]]..utilities.CMD_PAT..[[cash 5 USD to EUR
function currency:init(config)
currency.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('cash', true).table
currency.doc = [[```
]]..config.cmd_pat..[[cash [amount] <from> to <to>
Example: ]]..config.cmd_pat..[[cash 5 USD to EUR
Returns exchange rates for various currencies.
Source: Google Finance.
```]]
function currency:init()
currency.triggers = utilities.triggers(self.info.username):t('cash', true).table
end
function currency:action(msg, config)

View File

@ -3,13 +3,13 @@ local dice = {}
local utilities = require('utilities')
dice.command = 'roll <nDr>'
dice.doc = [[```
]]..utilities.CMD_PAT..[[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.
```]]
function dice:init()
dice.triggers = utilities.triggers(self.info.username):t('roll', true).table
end
function dice:action(msg)

View File

@ -6,15 +6,15 @@ local bindings = require('bindings')
local utilities = require('utilities')
dilbert.command = 'dilbert [date]'
dilbert.doc = [[```
]]..utilities.CMD_PAT..[[dilbert [YYYY-MM-DD]
function dilbert:init(config)
dilbert.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('dilbert', true).table
dilbert.doc = [[```
]]..config.cmd_pat..[[dilbert [YYYY-MM-DD]
Returns the latest Dilbert strip or that of the provided date.
Dates before the first strip will return the first strip. Dates after the last trip will return the last strip.
Source: dilbert.com
```]]
function dilbert:init()
dilbert.triggers = utilities.triggers(self.info.username):t('dilbert', true).table
end
function dilbert:action(msg, config)

View File

@ -3,13 +3,13 @@ local echo = {}
local utilities = require('utilities')
echo.command = 'echo <text>'
echo.doc = [[```
]]..utilities.CMD_PAT..[[echo <text>
function echo:init(config)
echo.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('echo', true).table
echo.doc = [[```
]]..config.cmd_pat..[[echo <text>
Repeats a string of text.
```]]
function echo:init()
echo.triggers = utilities.triggers(self.info.username):t('echo', true).table
end
function echo:action(msg)

View File

@ -5,8 +5,9 @@ local utilities = require('utilities')
eightball.command = '8ball'
eightball.doc = '`Returns an answer from a magic 8-ball!`'
function eightball:init()
eightball.triggers = utilities.triggers(self.info.username, {'[Yy]/[Nn]%p*$'}):t('8ball', true).table
function eightball:init(config)
eightball.triggers = utilities.triggers(self.info.username, config.cmd_pat,
{'[Yy]/[Nn]%p*$'}):t('8ball', true).table
end
local ball_answers = {

View File

@ -4,7 +4,7 @@ local fortune = {}
local utilities = require('utilities')
function fortune:init()
function fortune:init(config)
local s = io.popen('fortune'):read('*all')
if s:match('not found$') then
print('fortune is not installed on this computer.')
@ -12,7 +12,7 @@ function fortune:init()
return
end
fortune.triggers = utilities.triggers(self.info.username):t('fortune').table
fortune.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('fortune').table
end
fortune.command = 'fortune'

View File

@ -19,15 +19,15 @@ function gImages:init(config)
return
end
gImages.triggers = utilities.triggers(self.info.username):t('image', true):t('i', true):t('insfw', true).table
gImages.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('image', true):t('i', true):t('insfw', true).table
gImages.doc = [[```
]]..config.cmd_pat..[[image <query>
Returns a randomized top result from Google Images. Safe search is enabled by default; use "]]..config.cmd_pat..[[insfw" to disable it. NSFW results will not display an image preview.
Alias: ]]..config.cmd_pat..[[i
```]]
end
gImages.command = 'image <query>'
gImages.doc = [[```
]]..utilities.CMD_PAT..[[image <query>
Returns a randomized top result from Google Images. Safe search is enabled by default; use "]]..utilities.CMD_PAT..[[insfw" to disable it. NSFW results will not display an image preview.
Alias: ]]..utilities.CMD_PAT..[[i
```]]
function gImages:action(msg, config)
@ -43,7 +43,7 @@ function gImages:action(msg, config)
local url = 'https://www.googleapis.com/customsearch/v1?&searchType=image&imgSize=xlarge&alt=json&num=8&start=1&key=' .. config.google_api_key .. '&cx=' .. config.google_cse_key
if not string.match(msg.text, '^'..utilities.CMD_PAT..'i[mage]*nsfw') then
if not string.match(msg.text, '^'..config.cmd_pat..'i[mage]*nsfw') then
url = url .. '&safe=high'
end

View File

@ -4,14 +4,14 @@ local bindings = require('bindings')
local utilities = require('utilities')
gMaps.command = 'location <query>'
gMaps.doc = [[```
]]..utilities.CMD_PAT..[[location <query>
Returns a location from Google Maps.
Alias: ]]..utilities.CMD_PAT..[[loc
```]]
function gMaps:init()
gMaps.triggers = utilities.triggers(self.info.username):t('location', true):t('loc', true).table
function gMaps:init(config)
gMaps.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('location', true):t('loc', true).table
gMaps.doc = [[```
]]..config.cmd_pat..[[location <query>
Returns a location from Google Maps.
Alias: ]]..config.cmd_pat..[[loc
```]]
end
function gMaps:action(msg, config)

View File

@ -6,14 +6,14 @@ local JSON = require('dkjson')
local utilities = require('utilities')
gSearch.command = 'google <query>'
gSearch.doc = [[```
]]..utilities.CMD_PAT..[[google <query>
Returns four (if group) or eight (if private message) results from Google. Safe search is enabled by default, use "]]..utilities.CMD_PAT..[[gnsfw" to disable it.
Alias: ]]..utilities.CMD_PAT..[[g
```]]
function gSearch:init()
gSearch.triggers = utilities.triggers(self.info.username):t('g', true):t('google', true):t('gnsfw', true).table
function gSearch:init(config)
gSearch.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('g', true):t('google', true):t('gnsfw', true).table
gSearch.doc = [[```
]]..config.cmd_pat..[[google <query>
Returns four (if group) or eight (if private message) results from Google. Safe search is enabled by default, use "]]..config.cmd_pat..[[gnsfw" to disable it.
Alias: ]]..config.cmd_pat..[[g
```]]
end
function gSearch:action(msg, config)
@ -36,7 +36,7 @@ function gSearch:action(msg, config)
url = url .. '&rsz=4'
end
if not string.match(msg.text, '^'..utilities.CMD_PAT..'g[oogle]*nsfw') then
if not string.match(msg.text, '^'..config.cmd_pat..'g[oogle]*nsfw') then
url = url .. '&safe=active'
end

View File

@ -6,13 +6,13 @@ local bindings = require('bindings')
local utilities = require('utilities')
hackernews.command = 'hackernews'
hackernews.doc = [[```
Returns four (if group) or eight (if private message) top stories from Hacker News.
Alias: ]]..utilities.CMD_PAT..[[hn
```]]
function hackernews:init()
hackernews.triggers = utilities.triggers(self.info.username):t('hackernews', true):t('hn', true).table
function hackernews:init(config)
hackernews.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hackernews', true):t('hn', true).table
hackernews.doc = [[```
Returns four (if group) or eight (if private message) top stories from Hacker News.
Alias: ]]..config.cmd_pat..[[hn
```]]
end
function hackernews:action(msg, config)

View File

@ -6,7 +6,7 @@ local hearthstone = {}
local JSON = require('dkjson')
local utilities = require('utilities')
function hearthstone:init()
function hearthstone:init(config)
if not self.database.hearthstone or os.time() > self.database.hearthstone.expiration then
print('Downloading Hearthstone database...')
@ -36,15 +36,15 @@ function hearthstone:init()
end
hearthstone.triggers = utilities.triggers(self.info.username):t('hearthstone', true):t('hs').table
hearthstone.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('hearthstone', true):t('hs').table
hearthstone.doc = [[```
]]..config.cmd_pat..[[hearthstone <query>
Returns Hearthstone card info.
Alias: ]]..config.cmd_pat..[[hs
```]]
end
hearthstone.command = 'hearthstone <query>'
hearthstone.doc = [[```
]]..utilities.CMD_PAT..[[hearthstone <query>
Returns Hearthstone card info.
Alias: ]]..utilities.CMD_PAT..[[hs
```]]
local function format_card(card)

View File

@ -7,26 +7,26 @@ local utilities = require('utilities')
local help_text
function help:init()
function help:init(config)
local commandlist = {}
help_text = '*Available commands:*\n'..utilities.CMD_PAT
help_text = '*Available commands:*\n'..config.cmd_pat
for _,plugin in ipairs(self.plugins) do
if plugin.command then
table.insert(commandlist, plugin.command)
--help_text = help_text .. '\n• '..utilities.CMD_PAT .. plugin.command:gsub('%[', '\\[')
--help_text = help_text .. '\n• '..config.cmd_pat .. plugin.command:gsub('%[', '\\[')
end
end
table.insert(commandlist, 'help [command]')
table.sort(commandlist)
help_text = help_text .. table.concat(commandlist, '\n'..utilities.CMD_PAT) .. '\nArguments: <required> [optional]'
help_text = help_text .. table.concat(commandlist, '\n'..config.cmd_pat) .. '\nArguments: <required> [optional]'
help_text = help_text:gsub('%[', '\\[')
help.triggers = utilities.triggers(self.info.username):t('help', true):t('h', true).table
help.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('help', true):t('h', true).table
end

View File

@ -6,13 +6,13 @@ local JSON = require('dkjson')
local utilities = require('utilities')
imdb.command = 'imdb <query>'
imdb.doc = [[```
]]..utilities.CMD_PAT..[[imdb <query>
function imdb:init(config)
imdb.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('imdb', true).table
imdb.doc = [[```
]]..config.cmd_pat..[[imdb <query>
Returns an IMDb entry.
```]]
function imdb:init()
imdb.triggers = utilities.triggers(self.info.username):t('imdb', true).table
end
function imdb:action(msg, config)

View File

@ -15,26 +15,26 @@ function lastfm:init(config)
return
end
lastfm.triggers = utilities.triggers(self.info.username):t('lastfm', true):t('np', true):t('fmset', true).table
lastfm.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lastfm', true):t('np', true):t('fmset', true).table
lastfm.doc = [[```
]]..config.cmd_pat..[[np [username]
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
]]..config.cmd_pat..[[fmset <username>
Sets your last.fm username. Otherwise, ]]..config.cmd_pat..[[np will use your Telegram username. Use "]]..config.cmd_pat..[[fmset --" to delete it.
```]]
end
lastfm.command = 'lastfm'
lastfm.doc = [[```
]]..utilities.CMD_PAT..[[np [username]
Returns what you are or were last listening to. If you specify a username, info will be returned for that username.
]]..utilities.CMD_PAT..[[fmset <username>
Sets your last.fm username. Otherwise, ]]..utilities.CMD_PAT..[[np will use your Telegram username. Use "]]..utilities.CMD_PAT..[[fmset --" to delete it.
```]]
function lastfm:action(msg, config)
local input = utilities.input(msg.text)
if string.match(msg.text, '^'..utilities.CMD_PAT..'lastfm') then
if string.match(msg.text, '^'..config.cmd_pat..'lastfm') then
utilities.send_message(self, msg.chat.id, lastfm.doc, true, msg.message_id, true)
return
elseif string.match(msg.text, '^'..utilities.CMD_PAT..'fmset') then
elseif string.match(msg.text, '^'..config.cmd_pat..'fmset') then
if not input then
utilities.send_message(self, msg.chat.id, lastfm.doc, true, msg.message_id, true)
elseif input == '--' or input == utilities.char.em_dash then
@ -57,10 +57,10 @@ function lastfm:action(msg, config)
username = self.database.users[msg.from.id_str].lastfm
elseif msg.from.username then
username = msg.from.username
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use '..utilities.CMD_PAT..'fmset <username>.'
alert = '\n\nYour username has been set to ' .. username .. '.\nTo change it, use '..config.cmd_pat..'fmset <username>.'
self.database.users[msg.from.id_str].lastfm = username
else
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..utilities.CMD_PAT..'fmset.')
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
return
end
@ -78,7 +78,7 @@ function lastfm:action(msg, config)
local jdat = JSON.decode(jstr)
if jdat.error then
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..utilities.CMD_PAT..'fmset.')
utilities.send_reply(self, msg, 'Please specify your last.fm username or set it with '..config.cmd_pat..'fmset.')
return
end

View File

@ -4,8 +4,8 @@ local utilities = require('utilities')
local URL = require('socket.url')
local JSON = require('dkjson')
function luarun:init()
luarun.triggers = utilities.triggers(self.info.username):t('lua', true):t('return', true).table
function luarun:init(config)
luarun.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lua', true):t('return', true).table
end
function luarun:action(msg, config)
@ -20,7 +20,7 @@ function luarun:action(msg, config)
return
end
if msg.text_lower:match('^'..utilities.CMD_PAT..'return') then
if msg.text_lower:match('^'..config.cmd_pat..'return') then
input = 'return ' .. input
end
@ -32,8 +32,8 @@ function luarun:action(msg, config)
local URL = require('socket.url')
local HTTP = require('socket.http')
local HTTPS = require('ssl.https')
return function (self, msg) ]] .. input .. [[ end
]] )()(self, msg)
return function (self, msg, config) ]] .. input .. [[ end
]] )()(self, msg, config)
if output == nil then
output = 'Done!'
else

View File

@ -2,8 +2,8 @@ local me = {}
local utilities = require('utilities')
function me:init()
me.triggers = utilities.triggers(self.info.username):t('me', true).table
function me:init(config)
me.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('me', true).table
end
function me:action(msg, config)

View File

@ -3,13 +3,13 @@ local nick = {}
local utilities = require('utilities')
nick.command = 'nick <nickname>'
nick.doc = [[```
]]..utilities.CMD_PAT..[[nick <nickname>
Set your nickname. Use "]]..utilities.CMD_PAT..[[nick --" to delete it.
```]]
function nick:init()
nick.triggers = utilities.triggers(self.info.username):t('nick', true).table
function nick:init(config)
nick.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('nick', true).table
nick.doc = [[```
]]..config.cmd_pat..[[nick <nickname>
Set your nickname. Use "]]..config.cmd_pat..[[nick --" to delete it.
```]]
end
function nick:action(msg, config)

View File

@ -4,12 +4,12 @@ local ping = {}
local utilities = require('utilities')
function ping:init()
ping.triggers = utilities.triggers(self.info.username):t('ping'):t('annyong').table
function ping:init(config)
ping.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('ping'):t('annyong').table
end
function ping:action(msg)
local output = msg.text_lower:match('^'..utilities.CMD_PAT..'ping') and 'Pong!' or 'Annyong.'
function ping:action(msg, config)
local output = msg.text_lower:match('^'..config.cmd_pat..'ping') and 'Pong!' or 'Annyong.'
utilities.send_message(self, msg.chat.id, output)
end

View File

@ -6,14 +6,14 @@ local bindings = require('bindings')
local utilities = require('utilities')
pokedex.command = 'pokedex <query>'
pokedex.doc = [[```
]]..utilities.CMD_PAT..[[pokedex <query>
Returns a Pokedex entry from pokeapi.co.
Alias: ]]..utilities.CMD_PAT..[[dex
```]]
function pokedex:init()
pokedex.triggers = utilities.triggers(self.info.username):t('pokedex', true):t('dex', true).table
function pokedex:init(config)
pokedex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pokedex', true):t('dex', true).table
pokedex.doc = [[```
]]..config.cmd_pat..[[pokedex <query>
Returns a Pokedex entry from pokeapi.co.
Alias: ]]..config.cmd_pat..[[dex
```]]
end
function pokedex:action(msg, config)

View File

@ -4,13 +4,13 @@ local HTTP = require('socket.http')
local utilities = require('utilities')
preview.command = 'preview <link>'
preview.doc = [[```
]]..utilities.CMD_PAT..[[preview <link>
function preview:init(config)
preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table
preview.doc = [[```
]]..config.cmd_pat..[[preview <link>
Returns a full-message, "unlinked" preview.
```]]
function preview:init()
preview.triggers = utilities.triggers(self.info.username):t('preview', true).table
end
function preview:action(msg)

View File

@ -5,8 +5,8 @@ local utilities = require('utilities')
pun.command = 'pun'
pun.doc = '`Returns a pun.`'
function pun:init()
pun.triggers = utilities.triggers(self.info.username):t('pun').table
function pun:init(config)
pun.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pun').table
end
local puns = {

View File

@ -25,24 +25,24 @@ local mapping = {
local help
function reactions:init()
function reactions:init(config)
-- Generate a "help" message triggered by "/reactions".
help = 'Reactions:\n'
reactions.triggers = utilities.triggers(self.info.username):t('reactions').table
reactions.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('reactions').table
for trigger,reaction in pairs(mapping) do
help = help .. '' .. utilities.CMD_PAT .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, utilities.CMD_PAT..trigger)
table.insert(reactions.triggers, utilities.CMD_PAT..trigger..'@'..self.info.username:lower())
help = help .. '' .. config.cmd_pat .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, config.cmd_pat..trigger)
table.insert(reactions.triggers, config.cmd_pat..trigger..'@'..self.info.username:lower())
end
end
function reactions:action(msg)
if string.match(msg.text_lower, utilities.CMD_PAT..'reactions') then
function reactions:action(msg, config)
if string.match(msg.text_lower, config.cmd_pat..'reactions') then
utilities.send_message(self, msg.chat.id, help)
return
end
for trigger,reaction in pairs(mapping) do
if string.match(msg.text_lower, utilities.CMD_PAT..trigger) then
if string.match(msg.text_lower, config.cmd_pat..trigger) then
utilities.send_message(self, msg.chat.id, reaction)
return
end

View File

@ -6,14 +6,14 @@ local JSON = require('dkjson')
local utilities = require('utilities')
reddit.command = 'reddit [r/subreddit | query]'
reddit.doc = [[```
]]..utilities.CMD_PAT..[[reddit [r/subreddit | query]
Returns the top posts or results for a given subreddit or query. If no argument is given, returns the top posts from r/all. Querying specific subreddits is not supported.
Aliases: ]]..utilities.CMD_PAT..[[r, /r/subreddit
```]]
function reddit:init()
reddit.triggers = utilities.triggers(self.info.username, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table
function reddit:init(config)
reddit.triggers = utilities.triggers(self.info.username, config.cmd_pat, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table
reddit.doc = [[```
]]..config.cmd_pat..[[reddit [r/subreddit | query]
Returns the top posts or results for a given subreddit or query. If no argument is given, returns the top posts from r/all. Querying specific subreddits is not supported.
Aliases: ]]..config.cmd_pat..[[r, /r/subreddit
```]]
end
local format_results = function(posts)
@ -48,7 +48,7 @@ function reddit:action(msg, config)
local text = msg.text_lower
if text:match('^/r/.') then
-- Normalize input so this hack works easily.
text = msg.text_lower:gsub('^/r/', utilities.CMD_PAT..'r r/')
text = msg.text_lower:gsub('^/r/', config.cmd_pat..'r r/')
end
local input = utilities.input(text)
local source, url

View File

@ -3,15 +3,15 @@ local remind = {}
local utilities = require('utilities')
remind.command = 'remind <duration> <message>'
remind.doc = [[```
]]..utilities.CMD_PAT..[[remind <duration> <message>
Repeats a message after a duration of time, in minutes.
```]]
function remind:init()
function remind:init(config)
self.database.reminders = self.database.reminders or {}
remind.triggers = utilities.triggers(self.info.username):t('remind', true).table
remind.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('remind', true).table
remind.doc = [[```
]]..config.cmd_pat..[[remind <duration> <message>
Repeats a message after a duration of time, in minutes.
```]]
end
function remind:action(msg)

View File

@ -2,26 +2,25 @@ local setandget = {}
local utilities = require('utilities')
function setandget:init()
function setandget:init(config)
self.database.setandget = self.database.setandget or {}
setandget.triggers = utilities.triggers(self.info.username):t('set', true):t('get', true).table
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]
Returns the stored value or a list of stored values.
```]]
end
setandget.command = 'set <name> <value>'
setandget.doc = [[```
]]..utilities.CMD_PAT..[[set <name> <value>
Stores a value with the given name. Use "]]..utilities.CMD_PAT..[[set <name> --" to delete the stored value.
]]..utilities.CMD_PAT..[[get [name]
Returns the stored value or a list of stored values.
```]]
function setandget:action(msg)
function setandget:action(msg, config)
local input = utilities.input(msg.text)
self.database.setandget[msg.chat.id_str] = self.database.setandget[msg.chat.id_str] or {}
if msg.text_lower:match('^'..utilities.CMD_PAT..'set') then
if msg.text_lower:match('^'..config.cmd_pat..'set') then
if not input then
utilities.send_message(self, msg.chat.id, setandget.doc, true, nil, true)
@ -41,7 +40,7 @@ function setandget:action(msg)
utilities.send_message(self, msg.chat.id, '"' .. name .. '" has been set to "' .. value .. '".', true)
end
elseif msg.text_lower:match('^'..utilities.CMD_PAT..'get') then
elseif msg.text_lower:match('^'..config.cmd_pat..'get') then
if not input then
local output

View File

@ -2,8 +2,8 @@ local shell = {}
local utilities = require('utilities')
function shell:init()
shell.triggers = utilities.triggers(self.info.username):t('run', true).table
function shell:init(config)
shell.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('run', true).table
end
function shell:action(msg, config)

View File

@ -3,13 +3,13 @@ local shout = {}
local utilities = require('utilities')
shout.command = 'shout <text>'
shout.doc = [[```
]]..utilities.CMD_PAT..[[shout <text>
function shout:init(config)
shout.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('shout', true).table
shout.doc = [[```
]]..config.CMD_PAT..[[shout <text>
Shouts something. Input may be the replied-to message.
```]]
function shout:init()
shout.triggers = utilities.triggers(self.info.username):t('shout', true).table
end
function shout:action(msg)

View File

@ -3,13 +3,13 @@ local slap = {}
local utilities = require('utilities')
slap.command = 'slap [target]'
slap.doc = [[```
]]..utilities.CMD_PAT..[[slap [target]
function slap:init(config)
slap.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('slap', true).table
slap.doc = [[```
]]..config.cmd_pat..[[slap [target]
Slap somebody.
```]]
function slap:init()
slap.triggers = utilities.triggers(self.info.username):t('slap', true).table
end
local slaps = {

View File

@ -5,13 +5,13 @@ local JSON = require('dkjson')
local utilities = require('utilities')
time.command = 'time <location>'
time.doc = [[```
]]..utilities.CMD_PAT..[[time <location>
function time:init(config)
time.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('time', true).table
time.doc = [[```
]]..config.cmd_pat..[[time <location>
Returns the time, date, and timezone for the given location.
```]]
function time:init()
time.triggers = utilities.triggers(self.info.username):t('time', true).table
end
function time:action(msg, config)

View File

@ -6,13 +6,13 @@ local JSON = require('dkjson')
local utilities = require('utilities')
translate.command = 'translate [text]'
translate.doc = [[```
]]..utilities.CMD_PAT..[[translate [text]
function translate:init(config)
translate.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('translate', true):t('tl', true).table
translate.doc = [[```
]]..config.cmd_pat..[[translate [text]
Translates input or the replied-to message into the bot's language.
```]]
function translate:init()
translate.triggers = utilities.triggers(self.info.username):t('translate', true):t('tl', true).table
end
function translate:action(msg, config)

View File

@ -6,14 +6,15 @@ local JSON = require('dkjson')
local utilities = require('utilities')
urbandictionary.command = 'urbandictionary <query>'
urbandictionary.doc = [[```
]]..utilities.CMD_PAT..[[urbandictionary <query>
Returns a definition from Urban Dictionary.
Aliases: ]]..utilities.CMD_PAT..[[ud, ]]..utilities.CMD_PAT..[[urban
```]]
function urbandictionary:init()
urbandictionary.triggers = utilities.triggers(self.info.username):t('urbandictionary', true):t('ud', true):t('urban', true).table
function urbandictionary:init(config)
urbandictionary.triggers = utilities.triggers(self.info.username, config.cmd_pat)
:t('urbandictionary', true):t('ud', true):t('urban', true).table
urbandictionary.doc = [[```
]]..config.cmd_pat..[[urbandictionary <query>
Returns a definition from Urban Dictionary.
Aliases: ]]..config.cmd_pat..[[ud, ]]..config.cmd_pat..[[urban
```]]
end
function urbandictionary:action(msg, config)

View File

@ -11,14 +11,14 @@ function weather:init(config)
return
end
weather.triggers = utilities.triggers(self.info.username):t('weather', true).table
weather.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('weather', true).table
weather.doc = [[```
]]..config.cmd_pat..[[weather <location>
Returns the current weather conditions for a given location.
```]]
end
weather.command = 'weather <location>'
weather.doc = [[```
]]..utilities.CMD_PAT..[[weather <location>
Returns the current weather conditions for a given location.
```]]
function weather:action(msg, config)

View File

@ -3,13 +3,13 @@ local whoami = {}
local utilities = require('utilities')
whoami.command = 'whoami'
whoami.doc = [[```
Returns user and chat info for you or the replied-to message.
Alias: ]]..utilities.CMD_PAT..[[who
```]]
function whoami:init()
whoami.triggers = utilities.triggers(self.info.username):t('who', true):t('whoami').table
function whoami:init(config)
whoami.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('who', true):t('whoami').table
whoami.doc = [[```
Returns user and chat info for you or the replied-to message.
Alias: ]]..config.cmd_pat..[[who
```]]
end
function whoami:action(msg)

View File

@ -6,14 +6,14 @@ local JSON = require('dkjson')
local utilities = require('utilities')
wikipedia.command = 'wikipedia <query>'
wikipedia.doc = [[```
]]..utilities.CMD_PAT..[[wikipedia <query>
Returns an article from Wikipedia.
Aliases: ]]..utilities.CMD_PAT..[[w, ]]..utilities.CMD_PAT..[[wiki
```]]
function wikipedia:init()
wikipedia.triggers = utilities.triggers(self.info.username):t('wikipedia', true):t('wiki', true):t('w', true).table
function wikipedia:init(config)
wikipedia.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('wikipedia', true):t('wiki', true):t('w', true).table
wikipedia.doc = [[```
]]..config.cmd_pat..[[wikipedia <query>
Returns an article from Wikipedia.
Aliases: ]]..config.cmd_pat..[[w, ]]..config.cmd_pat..[[wiki
```]]
end
local get_title = function(search)

View File

@ -5,13 +5,13 @@ local JSON = require('dkjson')
local utilities = require('utilities')
xkcd.command = 'xkcd [i]'
xkcd.doc = [[```
]]..utilities.CMD_PAT..[[xkcd [i]
function xkcd:init(config)
xkcd.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('xkcd', true).table
xkcd.doc = [[```
]]..config.cmd_pat..[[xkcd [i]
Returns the latest xkcd strip and its alt text. If a number is given, returns that number strip. If "r" is passed in place of a number, returns a random strip.
```]]
function xkcd:init()
xkcd.triggers = utilities.triggers(self.info.username):t('xkcd', true).table
end
function xkcd:action(msg, config)

View File

@ -14,15 +14,15 @@ function youtube:init(config)
return
end
youtube.triggers = utilities.triggers(self.info.username):t('youtube', true):t('yt', true).table
youtube.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('youtube', true):t('yt', true).table
youtube.doc = [[```
]]..config.cmd_pat..[[youtube <query>
Returns the top result from YouTube.
Alias: ]]..config.cmd_pat..[[yt
```]]
end
youtube.command = 'youtube <query>'
youtube.doc = [[```
]]..utilities.CMD_PAT..[[youtube <query>
Returns the top result from YouTube.
Alias: ]]..utilities.CMD_PAT..[[yt
```]]
function youtube:action(msg, config)

View File

@ -281,24 +281,23 @@ end
utilities.md_escape = utilities.markdown_escape
utilities.CMD_PAT = '/'
utilities.triggers_meta = {}
utilities.triggers_meta.__index = utilities.triggers_meta
function utilities.triggers_meta:t(pattern, has_args)
local username = self.username:lower()
table.insert(self.table, '^'..utilities.CMD_PAT..pattern..'$')
table.insert(self.table, '^'..utilities.CMD_PAT..pattern..'@'..username..'$')
table.insert(self.table, '^'..self.cmd_pat..pattern..'$')
table.insert(self.table, '^'..self.cmd_pat..pattern..'@'..username..'$')
if has_args then
table.insert(self.table, '^'..utilities.CMD_PAT..pattern..'%s+[^%s]*')
table.insert(self.table, '^'..utilities.CMD_PAT..pattern..'@'..username..'%s+[^%s]*')
table.insert(self.table, '^'..self.cmd_pat..pattern..'%s+[^%s]*')
table.insert(self.table, '^'..self.cmd_pat..pattern..'@'..username..'%s+[^%s]*')
end
return self
end
function utilities.triggers(username, trigger_table)
function utilities.triggers(username, cmd_pat, trigger_table)
local self = setmetatable({}, utilities.triggers_meta)
self.username = username
self.cmd_pat = cmd_pat
self.table = trigger_table or {}
return self
end