nu_plugin_twitch 0.1.1

A Nu Shell plugin to interact with Twitch IRC and API, making scripting easier.
Documentation
const SOUNDS_ROOT = [(path self | path dirname) "sounds"] | path join;
let VOICES_DIR = [$env.HOME ".local" "share" "piper" "voices"] | path join;

def sound-path [$sound: string] {
    let $sound_path = [$SOUNDS_ROOT $"($sound).mp3"] | path join;

    if ($sound_path | path dirname) != $SOUNDS_ROOT {
        error make {
            msg: $"trying to escape ($SOUNDS_ROOT)",
            label: {
                text: "path here",
                span: (metadata $sound).span
            }
        }
    } else if not ($sound_path | path exists) {
        error make {
            msg: $"file not found ($sound_path)",
            label: {
                text: "path here",
                span: (metadata $sound).span
            }
        }
    } else {
        return $sound_path
    }
}

def play-sound [$sound: string] {
    sound meta (sound-path $sound) | sound play (sound-path $sound) -d $in.duration;
}

def text-to-speech [$text: string] {
    let $tmp = [$SOUNDS_ROOT "tmp_tts.wav"] | path join;
    piper --data-dir $VOICES_DIR -m en_US-lessac-medium -f $tmp -- $text;
    $tmp
}

def main [$channel: string] {
    twitch chat $channel | where message =~ "^bruh~" |
    where {|it| $it.badges | any {|b| $b.id in ["moderator","vip","broadcaster"]} } |
    each {|msg|
        let $tmp = text-to-speech ($msg.message | str substring 5..)
        play-sound "bruh" | sound play $tmp;
        $msg
    }
}