discord for windows now supports both color and title. dynv6 updates on one line per ip.

This commit is contained in:
2023-11-14 17:01:25 -05:00
parent b24e445b5a
commit aeac5fb04b
3 changed files with 45 additions and 10 deletions
+20 -3
View File
@@ -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
+4 -4
View File
@@ -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
+21 -3
View File
@@ -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