Skip to content

Instantly share code, notes, and snippets.

@daisyUniverse
Created May 10, 2024 14:45
Show Gist options
  • Save daisyUniverse/4acd722f1c19a5105e42b8b5a4a1c480 to your computer and use it in GitHub Desktop.
Save daisyUniverse/4acd722f1c19a5105e42b8b5a4a1c480 to your computer and use it in GitHub Desktop.
Powershell script that automatically uploads fresh minecraft screenshots to discord using webhooks
# PowerWatch
# Monitor a directory for changes and upload the file to a filehost, then include a link to that file in a webhook.
# Daisy Universe [D]
# 05 . 08 . 24
$filehost_url = "https://basedbin.fly.dev"
# You should point this to a trusted filehost. this script is set up to use https://github.com/wantguns/bin
$watch_path = "C:\Users\robins\Documents\PowerWatch"
# Pont this to your screenshots folder
$webhook_url = "WEBHOOK URL"
# You will need to get a discord Webhook URL to paste here
$watch_path_files = Get-ChildItem -Path $watch_path
# Upload the changed file to a filehost
function Upload-File{
param (
[string]$file
)
$filepath = "$watch_path\$file"
try {
$response = Invoke-RestMethod -Uri $filehost_url -Method Post -InFile $filepath
}
catch {
Write-Host "Failed to upload file... " -ForegroundColor Red
Write-Host ("`t"+$_) -ForegroundColor Yellow
return
}
return "$filehost_url/p$response"
}
function Generate-JsonPayload{
param (
[string]$link
)
$jsonPayload = '{"embeds": [{"image": {"url": "'+$link+'"}}]}'
return $jsonPayload
}
function Fire-Webhook{
param (
[string]$JsonPayload
)
try {
Invoke-RestMethod -Uri $webhook_url -Method Post -Body $jsonPayload -ContentType "application/json"
}
catch {
Write-Host "Failed to send webhook... " -ForegroundColor Red
Write-Host ("`t"+$_) -ForegroundColor Yellow
return
}
}
function Watch-Files{
while($true){
$new_files = Get-ChildItem -Path $watch_path
if ($null -eq $new_files) {
Write-Host "Folder empty... " -ForegroundColor Yellow
}
elseif ($new_files -ne $watch_path_files){
$new_file = Compare-Object -ReferenceObject $watch_path_files -DifferenceObject $new_files | Where-Object { $_.SideIndicator -eq "=>" }
if ($null -ne $new_file){
Start-Sleep -Seconds 3
Write-Host "`nNew file found! Uploading and sending $($new_file.InputObject)" -ForegroundColor Green
$link = Upload-File -file $new_file.InputObject
$json = Generate-JsonPayload -link $link
Fire-Webhook $json
}
}
$watch_path_files = $new_files
$i=0
while ($i -lt 4){
$elipse = "." * $i
Write-Host "`rWatching for new files$elipse $($new_file.InputObject) " -NoNewline
Start-Sleep -Seconds 1
$i++
}
}
}
Watch-Files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment