Skip to content

fix: Plugin API: passing a MessageElement to Message:append_element - #7176

Merged
pajlada merged 3 commits into
Chatterino:masterfrom
brilliantdrink:fix/plugin-message-append-element
Aug 15, 2026
Merged

pajlada merged 3 commits into
Chatterino:masterfrom
brilliantdrink:fix/plugin-message-append-element

Conversation

@brilliantdrink

Copy link
Copy Markdown
Contributor

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.

Reordering the sol::overload so that the Message:append_element(MessageElement) one comes fixes this. I'm not 100% sure on this but it seems that the passed message element somehow matches as a sol::table under the hood so the MessageElementInit overload matches before MessageElement gets checked. With the order from this pr both passing a MessageElement and a MessageElementInit works as should.

Small example to test:

---@param msg c2.Message
local function change_message(msg)
    -- not :clone()ing here to get an empty elements array
    local newMessage = c2.Message.new(msg)
    newMessage:append_element({
        type = 'text'
        text = 'foo'
    })
    for index, element in ipairs(msg:elements()) do
        newMessage:append_element(element)
    end
end

c2.Channel.by_name('myChannel'):on_message_appended(change_message)

@Nerixyz

Nerixyz commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Thank you, good catch! Can you add a test in https://github.com/Chatterino/chatterino2/blob/bf87302eb4837397a498d7a669860e859949b588/tests/lua/message/clone.lua (or some other file)? You can add a new function to the tests entry. Then we can make sure to not miss this again.

@brilliantdrink

Copy link
Copy Markdown
Contributor Author

@Nerixyz thanks for responding so quickly. I've added a test as a new element.lua file, its asserting append_element is executing without error for text inits and text element, and that the element was actually added.

@pajlada
pajlada enabled auto-merge (squash) August 15, 2026 22:41
@pajlada
pajlada merged commit 58f9ff3 into Chatterino:master Aug 15, 2026
18 checks passed
babicekmarek pushed a commit to babicekmarek/streamally-chatterino that referenced this pull request Sep 14, 2026
…tterino#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants