85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
)
|
|
|
|
func pressDiscordMuteHotkey() error {
|
|
script := `
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
function Get-DiscordWindows {
|
|
$windows = foreach ($p in Get-Process) {
|
|
$title = ($p.MainWindowTitle | Out-String).Trim()
|
|
if (-not $title) { continue }
|
|
if (-not $title.ToLower().Contains('discord')) { continue }
|
|
$p
|
|
}
|
|
if (-not $windows) { return @() }
|
|
return $windows
|
|
}
|
|
|
|
function Get-BestDiscordWindow {
|
|
$windows = Get-DiscordWindows
|
|
if (-not $windows -or $windows.Count -eq 0) { return $null }
|
|
|
|
$browserKeywords = @('google chrome', 'microsoft edge', 'mozilla firefox', 'brave', 'opera')
|
|
$ranked = $windows | Sort-Object {
|
|
$title = ($_.MainWindowTitle | Out-String).Trim().ToLower()
|
|
$score = 0
|
|
foreach ($keyword in $browserKeywords) {
|
|
if ($title.Contains($keyword)) { $score += 10 }
|
|
}
|
|
if ($title -match '^\(\d+\)\s*discord') { $score += 20 }
|
|
if ($title.Contains('discord')) { $score += 5 }
|
|
$score
|
|
} -Descending
|
|
|
|
return $ranked | Select-Object -First 1
|
|
}
|
|
|
|
function Get-VRChatWindow {
|
|
$windows = Get-Process | Where-Object {
|
|
$_.MainWindowTitle -and (
|
|
$_.MainWindowTitle -eq 'VRChat' -or $_.MainWindowTitle.ToLower().StartsWith('vrchat ')
|
|
)
|
|
}
|
|
if (-not $windows) { return $null }
|
|
return $windows | Sort-Object { $_.MainWindowHandle } -Descending | Select-Object -First 1
|
|
}
|
|
|
|
function Activate-Window($p) {
|
|
if ($null -eq $p) { return }
|
|
$wshell = New-Object -ComObject WScript.Shell
|
|
try { $null = $wshell.AppActivate($p.Id) } catch {}
|
|
Start-Sleep -Milliseconds 200
|
|
}
|
|
|
|
$discord = Get-BestDiscordWindow
|
|
if ($null -eq $discord) { exit 2 }
|
|
|
|
Activate-Window $discord
|
|
$wshell = New-Object -ComObject WScript.Shell
|
|
Start-Sleep -Milliseconds 200
|
|
$wshell.SendKeys('^+m')
|
|
Start-Sleep -Milliseconds 200
|
|
|
|
$vrchat = Get-VRChatWindow
|
|
if ($null -ne $vrchat) {
|
|
Activate-Window $vrchat
|
|
}
|
|
`
|
|
|
|
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", script)
|
|
out, err := cmd.CombinedOutput()
|
|
if len(out) > 0 {
|
|
log.Printf("discord mute output: %s", string(out))
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("discord mute failed: %w", err)
|
|
}
|
|
return nil
|
|
}
|