PoshBot.Joker.psm1

function Get-Advice {
    $Header = @{'Accept' = 'application/json'}

    (Invoke-RestMethod -Method Get -Uri 'https://api.adviceslip.com/advice' -Headers $Header -UseBasicParsing).slip.advice
}
function Get-AQuoteAboutNothing {
    $quoteUri = "https://seinfeld-quotes.herokuapp.com/random"

    try{
        # Are you master of your domain?
        $m = Invoke-RestMethod -Method Get -Uri $quoteUri

        Write-Output "$($m.quote) - $($m.author) [Season $($m.season), Episode $($m.episode)]"
    }
    catch{
        # [Slams money on the counter] I'm out!!
        Write-Output "No quote for you! Come back one year!!"
    }
}
function Get-CNorrisJoke {
    $Header = @{'Accept' = 'application/json'}

    (Invoke-RestMethod -Method Get -Uri 'https://api.chucknorris.io/jokes/random' -Headers $Header -UseBasicParsing).value
}
function Get-CorpBuzzword {
    $Header = @{'Accept' = 'application/json'}

    (Invoke-RestMethod -Method Get -Uri 'https://corporatebs-generator.sameerkumar.website/' -Headers $Header -UseBasicParsing).phrase
}
function Get-DadJoke {
    $Header = @{'Accept' = 'application/json'}

    (Invoke-RestMethod -Method Get -Uri 'https://icanhazdadjoke.com/' -Headers $Header -UseBasicParsing).joke
}
function Get-Fortune {
    (Invoke-RestMethod -Uri 'http://yerkee.com/api/fortune' -UseBasicParsing).fortune
}
function Get-GeekJoke {
    do {
    $Joke = Invoke-RestMethod -Uri 'https://sv443.net/jokeapi/v2/joke/Programming?blacklistFlags=nsfw,religious,political' -UseBasicParsing
    } while ($Joke.setup -like '*God*' -or $Joke.setup -like '*sex*' -or $Joke.joke -like '*sex*' -or $Joke.setup -like '*Gender*' -or $Joke.joke -like '*snuts*')

    if ($Joke.type -eq 'single') {
        $Joke.joke
    } else {
        $Joke.setup

        Start-Sleep -Seconds (Get-Random -Minimum 3 -Maximum 8)

        $Joke.delivery
    }
}
Function Get-GeneralJoke{
    $Header = @{'Accept' = 'application/json'}

 $joke = (Invoke-RestMethod -Method Get -Uri 'https://official-joke-api.appspot.com/random_joke' -Headers $Header -UseBasicParsing)
 Write-Output "[Question]:: $($joke.Setup)"
 Write-Output "[PunchLine]:: $($joke.punchline)"
}
Function Get-ProgrammingJoke{
    $Header = @{'Accept' = 'application/json'}

 $joke = (Invoke-RestMethod -Method Get -Uri 'https://official-joke-api.appspot.com/jokes/programming/random' -Headers $Header -UseBasicParsing)
 Write-Output "[Question]:: $($joke.Setup)"
 Write-Output "[PunchLine]:: $($joke.punchline)"
}
function Get-RSwansonQuote {
    $Header = @{'Accept' = 'application/json' }

    $quote = Invoke-RestMethod -Method Get -Uri 'https://ron-swanson-quotes.herokuapp.com/v2/quotes' -Headers $Header -UseBasicParsing

    Write-Output $quote
}
function Get-Joke {
    <#
    .SYNOPSIS
        Get a random joke.
    .DESCRIPTION
        Get a random joke, or fortune cookie message from a selection of APIs.
    .PARAMETER Genre
        The type of joke you're interested in. Genre can be chosen without specifying the parameter. Defaults to 'Dad' jokes.
    .PARAMETER Random
        Randomly selects a genre of joke to be retrieved.
    .EXAMPLE
        !joke
 
        Gets a random dad joke via default genre.
    .EXAMPLE
        !joke 'Dad'
 
        Gets a random dad joke by explictly selecting that genre.
    .EXAMPLE
        !joke -Genre 'Geek'
 
        Gets a random geek / programmer joke specifying the optional '-Genre' identifier.
    .EXAMPLE
        !joke -Random
 
        Gets a random joke from a random genre.
    #>

    [PoshBot.BotCommand(CommandName = 'joke')]
    [CmdletBinding(DefaultParameterSetName = 'Specific')]
    param (
        [Parameter(Position = 0,
                   ParameterSetName = 'Specific')]
        [ValidateSet('Dad', 'Geek', 'Fortune', 'ChuckNorris', 'RonSwanson', 'CorpBuzzword', 'Advice','Programming','General', 'Seinfeld')]
        [string] $Genre = 'Dad',

        [Parameter(Mandatory,
                   ParameterSetName = 'Random')]
        [switch] $Random
    )

    if ($Random) {
        $Genre = (Get-Variable "Genre").Attributes.ValidValues | Get-Random
    }

    switch ($Genre) {
        Dad { Get-DadJoke }
        Geek { Get-GeekJoke }
        Fortune { Get-Fortune }
        ChuckNorris {Get-CNorrisJoke }
        RonSwanson { Get-RSwansonQuote }
        CorpBuzzword { Get-CorpBuzzword }
        Advice { Get-Advice }
        Programming {Get-ProgrammingJoke}
        General {Get-GeneralJoke}
        Seinfeld { Get-AQuoteAboutNothing }
    }
}
Export-ModuleMember -Function 'Get-Joke'