2019-08-29 21:54:08 -04:00
|
|
|
local http = minetest.request_http_api()
|
|
|
|
local settings = minetest.settings
|
|
|
|
|
|
|
|
local port = settings:get('discord.port') or 8080
|
|
|
|
local timeout = 10
|
|
|
|
|
|
|
|
discord = {}
|
|
|
|
|
|
|
|
discord.text_colorization = settings:get('discord.text_color') or '#ffffff'
|
|
|
|
|
|
|
|
discord.registered_on_messages = {}
|
|
|
|
|
2019-10-18 15:08:12 -04:00
|
|
|
local irc_enabled = minetest.get_modpath("irc")
|
|
|
|
|
2022-10-20 16:02:00 +13:00
|
|
|
function discord.register_on_message(func)
|
2019-08-29 21:54:08 -04:00
|
|
|
table.insert(discord.registered_on_messages, func)
|
2022-10-20 16:02:00 +13:00
|
|
|
end
|
2019-08-29 21:54:08 -04:00
|
|
|
|
2019-10-18 15:08:12 -04:00
|
|
|
discord.chat_send_all = minetest.chat_send_all
|
2019-08-29 21:54:08 -04:00
|
|
|
|
2022-10-20 16:02:00 +13:00
|
|
|
-- Allow the chat message format to be customised by other mods
|
|
|
|
function discord.format_chat_message(name, msg)
|
|
|
|
return ('<%s@Discord> %s'):format(name, msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
function discord.handle_response(response)
|
2019-08-29 21:54:08 -04:00
|
|
|
local data = response.data
|
|
|
|
if data == '' or data == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local data = minetest.parse_json(response.data)
|
|
|
|
if not data then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if data.messages then
|
|
|
|
for _, message in pairs(data.messages) do
|
|
|
|
for _, func in pairs(discord.registered_on_messages) do
|
|
|
|
func(message.author, message.content)
|
|
|
|
end
|
2022-10-20 16:02:00 +13:00
|
|
|
local msg = discord.format_chat_message(message.author, message.content)
|
2019-10-18 15:08:12 -04:00
|
|
|
discord.chat_send_all(minetest.colorize(discord.text_colorization, msg))
|
|
|
|
if irc_enabled then
|
|
|
|
irc.say(msg)
|
|
|
|
end
|
2021-05-31 19:02:13 +00:00
|
|
|
minetest.log('action', '[Discord] Message: '..msg)
|
2019-08-29 21:54:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if data.commands then
|
|
|
|
local commands = minetest.registered_chatcommands
|
|
|
|
for _, v in pairs(data.commands) do
|
|
|
|
if commands[v.command] then
|
|
|
|
if minetest.get_ban_description(v.name) ~= '' then
|
|
|
|
discord.send('You cannot run commands because you are banned.', v.context or nil)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Check player privileges
|
|
|
|
local required_privs = commands[v.command].privs or {}
|
|
|
|
local player_privs = minetest.get_player_privs(v.name)
|
|
|
|
for priv, value in pairs(required_privs) do
|
|
|
|
if player_privs[priv] ~= value then
|
|
|
|
discord.send('Insufficient privileges.', v.context or nil)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local old_chat_send_player = minetest.chat_send_player
|
|
|
|
minetest.chat_send_player = function(name, message)
|
|
|
|
old_chat_send_player(name, message)
|
|
|
|
if name == v.name then
|
|
|
|
discord.send(message, v.context or nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
success, ret_val = commands[v.command].func(v.name, v.params or '')
|
|
|
|
if ret_val then
|
|
|
|
discord.send(ret_val, v.context or nil)
|
|
|
|
end
|
|
|
|
minetest.chat_send_player = old_chat_send_player
|
|
|
|
else
|
|
|
|
discord.send(('Command not found: `%s`'):format(v.command), v.context or nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if data.logins then
|
|
|
|
local auth = minetest.get_auth_handler()
|
|
|
|
for _, v in pairs(data.logins) do
|
|
|
|
local authdata = auth.get_auth(v.username)
|
|
|
|
local result = false
|
|
|
|
if authdata then
|
|
|
|
result = minetest.check_password_entry(v.username, authdata.password, v.password)
|
|
|
|
end
|
|
|
|
local request = {
|
|
|
|
type = 'DISCORD_LOGIN_RESULT',
|
|
|
|
user_id = v.user_id,
|
|
|
|
username = v.username,
|
|
|
|
success = result
|
|
|
|
}
|
|
|
|
http.fetch({
|
|
|
|
url = 'localhost:'..tostring(port),
|
|
|
|
timeout = timeout,
|
|
|
|
post_data = minetest.write_json(request)
|
|
|
|
}, discord.handle_response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:02:00 +13:00
|
|
|
function discord.send(message, id)
|
2019-08-29 21:54:08 -04:00
|
|
|
local data = {
|
|
|
|
type = 'DISCORD-RELAY-MESSAGE',
|
|
|
|
content = minetest.strip_colors(message)
|
|
|
|
}
|
|
|
|
if id then
|
|
|
|
data['context'] = id
|
|
|
|
end
|
|
|
|
http.fetch({
|
|
|
|
url = 'localhost:'..tostring(port),
|
|
|
|
timeout = timeout,
|
|
|
|
post_data = minetest.write_json(data)
|
|
|
|
}, function(_) end)
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:02:00 +13:00
|
|
|
function minetest.chat_send_all(message)
|
2019-10-18 15:08:12 -04:00
|
|
|
discord.chat_send_all(message)
|
2019-08-29 21:54:08 -04:00
|
|
|
discord.send(message)
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:02:00 +13:00
|
|
|
-- Register the chat message callback after other mods load so that anything
|
|
|
|
-- that overrides chat will work correctly
|
|
|
|
minetest.after(0, minetest.register_on_chat_message, function(name, message)
|
|
|
|
discord.send(minetest.format_chat_message(name, message))
|
2019-08-29 21:54:08 -04:00
|
|
|
end)
|
|
|
|
|
|
|
|
local timer = 0
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
if dtime then
|
|
|
|
timer = timer + dtime
|
|
|
|
if timer > 0.2 then
|
|
|
|
http.fetch({
|
|
|
|
url = 'localhost:'..tostring(port),
|
|
|
|
timeout = timeout,
|
|
|
|
post_data = minetest.write_json({
|
|
|
|
type = 'DISCORD-REQUEST-DATA'
|
|
|
|
})
|
|
|
|
}, discord.handle_response)
|
|
|
|
timer = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_shutdown(function()
|
|
|
|
discord.send('*** Server shutting down...')
|
|
|
|
end)
|
|
|
|
|
2019-10-18 15:08:12 -04:00
|
|
|
if irc_enabled then
|
|
|
|
discord.old_irc_sendLocal = irc.sendLocal
|
2022-10-20 16:02:00 +13:00
|
|
|
function irc.sendLocal(msg)
|
2019-10-18 15:08:12 -04:00
|
|
|
discord.old_irc_sendLocal(msg)
|
|
|
|
discord.send(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-29 21:54:08 -04:00
|
|
|
discord.send('*** Server started!')
|