Closed nonfreegithub closed 11 months ago
- What function can I use to send a message to a matterbridge gate from an external mod?
beerchat.send_on_channel("Sam", "channel", "message")
This will go through certain callbacks and checks like before_send_on_channel and on_send_on_channel plus (for minetest chat) any callbacks registered by other mods or plugins. It will send message, as if it was sent in game to channel #channel by player named Sam, to both bridge and minetest chat.
beerchat.on_channel_message(channel, name, message)
Will not send to minetest chat and skips some (possibly all) registered callbacks, restrictions, cleanups, whatever. This function was supposed to be chainable/overridable custom handler but isn't really used as such and there's now better ways to do message editing through beerchat.register_callback(triggername, function [, priority])
.
So bad naming for actual real current use which is to send message toward bridge. Probably wont disappear any time soon but might receive deprecation warning if we're going to update naming for it some day, likely wont be removed immediately to keep backwards compatibility so I'd say this is safe for now.
- How can I modify the format of the main channel and not the others to prevent the main channel from showing [#main]?
I was actually going to add a way to actually somehow mark message as formatted for multiple custom formatters but for now you can register higher priority message formatter and cancel any formatters that would normally get applied after it.
beerchat.register_callback(triggername, function [, priority])
, here we use predefined high priority which is meant to be used if you know for sure that your callback will handle required paths for message routing and it really has to override default flow in some special cases.
Generally, do not use highest priority with callback cancellation unless you absolutely have to and know that your callback will handle every use case because it might be used for access checks like user permission validations.
Example for custom formatter:
beerchat.register_callback("before_send", function(target, message, data)
if data and data.channel == "main" then
-- Apply custom formatting (edit message) only for channel "main" messages
data.message = beerchat.format_message(
"<${from_player}> ${message}", {
channel_name = data.channel,
to_player = target,
from_player = data.name,
message = message
}
)
-- Cancel any registered callbacks and prevent double formatting but allow sending message
return true;
end
end, "high")
There's many other ways to send, restrict, reroute and/or modify messages through several callbacks that can be registered with beerchat.register_callback(triggername, function [, priority])
but no really clean way for custom formatting.
But while function is exposed for direct access and overrides this isn't exactly considered to be part of the public API but wont probably see too big changes any time soon.
Override beerchat.format_message(format_string, message_data)
https://github.com/mt-mods/beerchat/blob/2a7a1eafaaf17f14f0fc280e46e37640197ece83/format_message.lua#L16-L49
Like this:
local beerchat_format_message = beerchat.format_message
beerchat.format_message = function(format_string, message_data)
if (message_data.channel == "main") then
format_string = "<${from_player}> ${message}"
end
return beerchat_format_message(format_string, message_data)
end
Override beerchat.format_message(format_string, message_data)
message_data.channel
must be message_data.channel_name
"main"
can be beerchat.main_channel_name
local beerchat_format_message = beerchat.format_message
beerchat.format_message = function(format_string, message_data)
if (message_data.channel_name == beerchat.main_channel_name) then
format_string = "<${from_player}> ${message}"
end
return beerchat_format_message(format_string, message_data)
end
it works! :tada:
What function can I use to send a message to a matterbridge gate from an external mod?
How can I modify the format of the main channel and not the others to prevent the main channel from showing [#main]?