fix(plugins): Message:append_element(MessageElement) unreachable (#7176)

When passing a `MessageElement` instead of an init table to
`Message:append_element` in a lua plugin, it would error with "Invalid
message type" suggesting that it calls `elementFromTable`, i.e. acts as
if a `MessageElementInit` table was passed.

```

Reviewed-by: Nerixyz <nerixdev@outlook.de>
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
b_drink
2026-08-15 23:06:33 +00:00
committed by GitHub
parent bf87302eb4
commit 58f9ff32a2
2 changed files with 54 additions and 6 deletions
+6 -6
View File
@@ -767,6 +767,12 @@ void createUserType(sol::table &c2)
},
"append_element",
sol::overload(
// Message:append_element(MessageElement)
// Create a clone of the given element and add it to the message
[](Message *msg, ElementRef &element) {
checkWritable(msg);
msg->elements.emplace_back(element.constElement()->clone());
},
// Message:append_element(MessageElementInit)
// Create a new element
[](Message *msg, const sol::table &tbl) {
@@ -776,12 +782,6 @@ void createUserType(sol::table &c2)
{
msg->elements.emplace_back(std::move(el));
}
},
// Message:append_element(MessageElement)
// Create a clone of the given element and add it to the message
[](Message *msg, ElementRef &element) {
checkWritable(msg);
msg->elements.emplace_back(element.constElement()->clone());
}),
"clone",
[](const Message &message) {
+48
View File
@@ -0,0 +1,48 @@
-- SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com>
--
-- SPDX-License-Identifier: CC0-1.0
local chan = c2.Channel.by_name("mm2pl")
assert(chan)
local tests = {
append_element = function()
local msg = c2.Message.new({
elements = {
{
type = "text",
text = "abcde",
},
{ type = "twitch-moderation" },
},
})
chan:add_message(msg)
local new_msg = c2.Message.new(msg)
local append_init_ok, append_init_err = pcall(function()
new_msg:append_element({
type = 'text',
text = 'foo'
})
end)
assert(append_init_ok)
assert(new_msg:elements()[1])
assert(new_msg:elements()[1].type == 'text' and new_msg:elements()[1].words[1] == 'foo')
local append_element_ok, append_element_err = pcall(function()
for index, element in ipairs(msg:elements()) do
new_msg:append_element(element)
end
end)
assert(append_element_ok)
assert(new_msg:elements()[2])
assert(new_msg:elements()[2].type == 'text' and new_msg:elements()[2].words[1] == 'abcde')
end,
}
for name, fn in pairs(tests) do
chan:clear_messages() -- start off without any messages
local ok, res = pcall(fn)
if not ok then
error(name .. " failed: " .. res)
end
end