zorael / kameloso

IRC bot with Twitch support
Boost Software License 1.0
9 stars 3 forks source link

IRC message formatting tags #118

Closed zorael closed 2 years ago

zorael commented 2 years ago

Like kameloso.common.expandTags, this adds kameloso.irccolours.expandIRCTags that translates <tags> in outgoing IRC messages and does some string-replacing to translate them to IRCControlCharacters in their appropriate syntaxes.

// Old
enum pattern = "Quote %s #%s saved.";
immutable message = plugin.state.settings.colouredOutgoing ?
    pattern.format(id.ircColourByHash, index.ircBold) :
    pattern.format(id, index);
privmsg(plugin.state, event.channel, event.sender.nickname. message);

// New
enum newPattern = "Quote <h>%s<h> #<b>%d<b> saved.";
immutable newMessage = newPattern.format(id, index);
privmsg(plugin.state, event.channel, event.sender.nickname, newMessage);

It doesn't look like much and there are other ways around it, but compared to the previous solution it simplifies supporting both coloured and uncoloured output (to the server). As illustrated above, previously we branched; "if coloured send this, else send that". Now we just have one tagged message string and let expandIRCTags either expand them to colouring/formatting or strip them from the output instead. Messaging functions in kameloso.messaging expand the tags.

One drawback is that it accesses the global kameloso.common.settings to tell if coloured output should be sent, but there is already precedence in this in kameloso.printing, which does the same to tell if there should be coloured (and/or bright) terminal output. Not ideal, but the alternative is to pass the setting as an argument to each and every call, which is even less ideal.

Sort of an abuse of the notion of release candidate to include this now, but I'd very much like to see it in v3.0.0.