From aeac5fb04bb2c67d24e547ccc3498530d6d9c68e Mon Sep 17 00:00:00 2001 From: Andrew Stowell Date: Tue, 14 Nov 2023 17:01:25 -0500 Subject: [PATCH] discord for windows now supports both color and title. dynv6 updates on one line per ip. --- Discord.ps1 | 23 ++++++++++++++++++++--- Dynv6.ps1 | 8 ++++---- SendDiscordChunk.ps1 | 24 +++++++++++++++++++++--- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Discord.ps1 b/Discord.ps1 index 02f2acc..9286505 100644 --- a/Discord.ps1 +++ b/Discord.ps1 @@ -29,7 +29,9 @@ param ( [parameter (ValueFromPipeline = $true)] [string[]] $message, [string] $distinct = "", [string] $webhook = "", - [string] $botName = "" + [string] $botName = "", + [string] $color = "", + [string] $title = "" ) begin { $chunk = "" @@ -68,16 +70,31 @@ end { # if distinct, load previous message to check for duplicate message if ($distinct) { - $distinctFile = "$PSScriptRoot\\distinct\\$distinct.msg" + if ( ! $title) { + $title = $distinct; + } + $clean = $distinct -replace '[\.\|\"\*\/\:\<\>\?\\]', ''; + $distinctFile = "$PSScriptRoot\\distinct\\$clean.msg" if (Test-Path -Path $distinctFile) { [string[]] $oldMessage = Get-Content -Path $distinctFile } } + $colors = @{ + "maroon" = 6619169; "brown" = 6633216; "olive" = 7238926; "teal" = 168838; "navy" = 70974; "black" = 0; + "red" = 15007744; "orange" = 16347910; "yellow" = 16776980; "lime" = 11206450; "green" = 1421338; + "cyan" = 65535; "blue" = 213983; "purple" = 8265372; "magenta" = 12714104; "grey" = 9606545; + "pink" = 16744896; "apricot" = 16757101; "beige" = 15129254; "mint" = 10485424; "lavender" = 13082607; "white" = 16777215; + }; + + if ($colors.ContainsKey($color)) { + $colorNumber = $colors.$color; + } + if (($oldMessage -join "`n") -ne ($message -join "`n")) { # finally send each chunk of the message foreach ($chunk in $chunks) { - SendDiscordMessage -message $chunk -webhook $webhook -botName $botName + SendDiscordMessage -message $chunk -webhook $webhook -botName $botName -title $title -color $colorNumber } # if distinct, store the updated message for future checking diff --git a/Dynv6.ps1 b/Dynv6.ps1 index 0199989..77971d2 100644 --- a/Dynv6.ps1 +++ b/Dynv6.ps1 @@ -103,13 +103,13 @@ if ($old4 -eq $ipv4) { $events += "IPv4 address unchanged" } else { # send IPv4 address to dynv6 - $events += "Updating IPv4" $body = @{ zone = $zone token = $token ipv4 = $ipv4 } - $events += Invoke-RestMethod -Method Get -Uri $update4 -Body $body + $msg = Invoke-RestMethod -Method Get -Uri $update4 -Body $body + $events += "Updating IPv4: " + $msg # save current IPv4 address Out-File -FilePath $file4 -InputObject $ipv4 @@ -120,13 +120,13 @@ if ($old6 -eq $ipv6) { $events += "IPv6 address unchanged" } else { # send IPv6 address to dynv6 - $events += "Updating IPv6" $body = @{ zone = $zone token = $token ipv6 = $ipv6 } - $events += Invoke-RestMethod -Method Get -Uri $update6 -Body $body + $msg = Invoke-RestMethod -Method Get -Uri $update6 -Body $body + $events += "Updating IPv6: " + $msg # save current IPv6 address Out-File -FilePath $file6 -InputObject $ipv6 diff --git a/SendDiscordChunk.ps1 b/SendDiscordChunk.ps1 index 021be3c..d0ec412 100644 --- a/SendDiscordChunk.ps1 +++ b/SendDiscordChunk.ps1 @@ -4,7 +4,9 @@ function SendDiscordMessage param ( [string] $message = $(throw "-message is required"), [string] $webhook = $(throw "-webhook is required"), - [string] $botName = $(throw "-botName is required") + [string] $botName = $(throw "-botName is required"), + [int] $color = -1, + [string] $title = "" ) # force utf-8, because that is what Discord needs @@ -13,6 +15,7 @@ function SendDiscordMessage $header = "Content-Type: application/json" # convert string into json, escaping the given message + $title = ConvertTo-Json -InputObject $title $message = ConvertTo-Json -InputObject $message $botName = ConvertTo-Json -InputObject $botName @@ -20,15 +23,30 @@ function SendDiscordMessage # windows is fine with the missing zero, but Discord needs it $message = $message -Replace "\\u001b\[m", "\u001b[0m" + # only enable colors if needed + $prefix = ''; + if ($message -contains "\u001b") { + $prefix = 'ansi\n'; + } + # remove wrapping quotes around json string, replace with color-enabled block quotes - $message = '"```ansi\n' + $message.SubString(1, $message.length - 2) + '\n```"' + $message = '"```' + $prefix + $message.SubString(1, $message.length - 2) + '\n```"' # build post data: {"username": "$botName", "content": "$message"} $content = "{"; if ($botName) { $content += '"username": ' + $botName + ', '; } - $content += '"content": ' + $message + '}'; + $content += '"embeds": [{"description": ' + $message; + + if ($color -ge 0) { + $content += ', "color": ' + $color; + } + if ($title) { + $content += ', "title": ' + $title; + } + + $content += '}]}'; # post data is complex, so we have to pipe it into curl # display the resulting id/message returned from Discord