TwitchText
in package
The parts of turning a message into text that are Twitch's rules rather than anybody's.
Everything shared — resolving Discord's mention markup, budgeting attachment links, truncating on a character boundary — lives in MessageText and is used from here. What is left is what IRC and Twitch specifically demand, and it is not much, but every line of it matters: a message that keeps a newline becomes an IRC command, and one that starts with a slash becomes a chat command.
Tags
Table of Contents
Constants
- LIMIT : mixed = 500
- What Twitch accepts in one chat message.
Methods
- compose() : string|null
- Renders a Discord message for Twitch chat, or `null` when there is nothing worth relaying.
- normalizeLogin() : string|null
- Normalises a Twitch channel reference (`#Foo`, `https://twitch.tv/Foo`) to a bare login.
- safeLine() : string
- Makes a line safe to hand to `Irc::say()`.
- sanitizeIrc() : string
- speaker() : string
- A name that is safe to *start* a chat line with.
Constants
LIMIT
What Twitch accepts in one chat message.
public
mixed
LIMIT
= 500
Methods
compose()
Renders a Discord message for Twitch chat, or `null` when there is nothing worth relaying.
public
static compose(Outgoing $message[, int $limit = self::LIMIT ]) : string|null
The author: prefix is not only cosmetic: it guarantees the line never
begins with / or ., which Twitch parses as a chat command. A bare
relayed /ban someone would otherwise execute if the bot holds moderator
on the target channel.
Attachments are relayed as their CDN links, so someone in chat can actually open the picture rather than being told one exists. They are budgeted before the message text and appended after truncation, not before it: a link that has had its tail cut off is not a link, whereas a shortened sentence still reads.
Parameters
- $message : Outgoing
- $limit : int = self::LIMIT
Return values
string|nullnormalizeLogin()
Normalises a Twitch channel reference (`#Foo`, `https://twitch.tv/Foo`) to a bare login.
public
static normalizeLogin(string $input) : string|null
What this returns is what gets stored, so it has to be stable: normalising one way today and another tomorrow orphans every bridge made before the change.
Parameters
- $input : string
Return values
string|nullsafeLine()
Makes a line safe to hand to `Irc::say()`.
public
static safeLine(string $text[, int $limit = self::LIMIT ]) : string
Two separate hazards. CR and LF would terminate the PRIVMSG line and
let the remainder be parsed as a fresh IRC command — so they are stripped
rather than escaped. And Twitch reads a leading / or . as a chat
command, so a reply that happens to begin with one (an API error quoting
a path, say) would be executed instead of said; a leading zero-width
space would be invisible but is also silently dropped by some clients, so
the character is prefixed with a space instead.
Parameters
- $text : string
- $limit : int = self::LIMIT
Return values
stringsanitizeIrc()
public
static sanitizeIrc(string $text) : string
Parameters
- $text : string
Return values
stringspeaker()
A name that is safe to *start* a chat line with.
public
static speaker(string $name) : string
The name comes first on every relayed line, and it is chosen by whoever
is speaking — a Discord nickname, a Telegram name. The other bots in a
Twitch chat (Nightbot, StreamElements, Fossabot) read a line starting
with ! as a command, and the bridge's account is usually a moderator
there, so a nickname like !addcom !x would be run with a moderator's
authority by somebody who is not one. Leading symbols are dropped; what
is left starts with a letter or a digit.
Parameters
- $name : string