Split commands into separate file. Added API to register custom commands (no arguments yet, just a name)

This commit is contained in:
neko259 2019-01-17 10:14:50 +02:00
parent 3ddcc0df3c
commit 04e404f2e5
2 changed files with 27 additions and 14 deletions

16
commands.lua Normal file
View File

@ -0,0 +1,16 @@
telegram.register_command("ping", function(msg)
telegram.send_message(msg.chat.id, "Pong!")
end)
telegram.register_command("groupid", function(msg)
telegram.send_message(msg.chat.id, "Group id: " .. msg.chat.id)
end)
telegram.register_command("players", function(msg)
local players = ""
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
players = players .. player:get_player_name() .. ", "
end
telegram.send_message(msg.chat.id, "Active players: " .. players)
end)

View File

@ -1,7 +1,7 @@
local current_mod_name = minetest.get_current_modname() local current_mod_name = minetest.get_current_modname()
local modpath = minetest.get_modpath(current_mod_name) local modpath = minetest.get_modpath(current_mod_name)
local telegram = {} telegram = {}
local token = minetest.settings:get("telegram.token") local token = minetest.settings:get("telegram.token")
local chat_id = minetest.settings:get("telegram.chatid") local chat_id = minetest.settings:get("telegram.chatid")
@ -23,6 +23,12 @@ if not http_api then
error("HTTP API cannot be enabled. Add the mods to trusted.") error("HTTP API cannot be enabled. Add the mods to trusted.")
end end
local COMMANDS = {}
function telegram.register_command(name, command)
COMMANDS[name] = command
end
local function make_request(method, request_body, callback) local function make_request(method, request_body, callback)
local response = {} local response = {}
@ -81,19 +87,9 @@ function telegram.send_message(chat_id, text)
end end
function telegram.on_text_receive(msg) function telegram.on_text_receive(msg)
if msg.text == "/start" then local command = COMMANDS[msg.text]
telegram.send_message(msg.from.id, "Hello there!\nMy name is " .. bot.first_name) if command then
elseif msg.text == "ping" then command(msg)
telegram.send_message(msg.chat.id, "Pong!")
elseif msg.text == "groupid" then
telegram.send_message(msg.chat.id, "Group id: " .. msg.chat.id)
elseif msg.text == "players" then
local players = ""
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
players = players .. player:get_player_name() .. ", "
end
telegram.send_message(msg.chat.id, "Active players: " .. players)
else else
minetest.chat_send_all("<" .. msg.from.first_name .. "@TG> " .. msg.text) minetest.chat_send_all("<" .. msg.from.first_name .. "@TG> " .. msg.text)
end end
@ -127,3 +123,4 @@ if chat_id then
end) end)
end end
dofile(modpath .. "/commands.lua")