解决Twitch频道点数兑换触发时Discord推送日志内容异常问题
解决Twitch频道点数兑换消息推送到Discord的问题
问题现状
通过Chatty将Twitch频道点数兑换消息记录到本地Windows日志文件后,现有PowerShell脚本仅推送文件变更提示,无法推送日志中的实际兑换内容。
修改后的PowerShell脚本
# 初始化文件监视器 $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = "C:\Users\AYYA\.chatty\logs" # 若日志每日生成新文件,可改为*_highlighted.log匹配所有日期的高亮日志 $watcher.Filter = "2023-12-09_highlighted.log" $watcher.IncludeSubdirectories = $false $watcher.EnableRaisingEvents = $true # 存储上次推送的内容,避免重复推送 $lastContent = $null $action = { $path = $Event.SourceEventArgs.FullPath # 读取日志文件最后一行内容 $latestLine = Get-Content $path | Select-Object -Last 1 # 过滤空行,且仅当内容与上次不同时推送 if ($latestLine -and $latestLine -ne $lastContent) { $lastContent = $latestLine $webhookurl = "https://discord.com/api/webhooks/1182854675947593799/DHgRfZWP547WkDUrwG9LuGtwNrWLs51o6Z0CmgqLa0Gh_jtUmeDj9qxmhvPNOV8_dOkx" $body = @{ content = $latestLine } | ConvertTo-Json Invoke-RestMethod -Uri $webhookurl -Method Post -Body $body -ContentType 'application/json' } } # 注册文件创建和变更事件 Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action
关键修改说明
- 获取实际日志内容:使用
Get-Content $path | Select-Object -Last 1读取日志文件的最后一行,也就是最新的兑换消息 - 避免重复推送:新增
$lastContent变量记录上次推送的内容,仅当新内容与上次不同时才推送,解决FileSystemWatcher事件重复触发的问题 - 适配每日日志文件:如果Chatty每天生成新的日期命名日志文件,可将
$watcher.Filter改为*_highlighted.log,匹配所有日期的高亮日志文件
内容的提问来源于stack exchange,提问作者Mohamed Shuau
相关产品推荐
相关产品推荐

