-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathntfy-updates.plugin.js
More file actions
108 lines (89 loc) · 3.79 KB
/
ntfy-updates.plugin.js
File metadata and controls
108 lines (89 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { log, LogLevel } = require("@peacockproject/core/loggingInterop")
const { PEACOCKVERSTRING } = require("@peacockproject/core/utils")
const { readFileSync, existsSync } = require("fs")
const { join } = require("path")
let lastCheckTime = 0
const CHECK_COOLDOWN = 14400
function loadConfig() {
const configPath = join(process.cwd(), "plugins", "ntfy-config.json")
if (!existsSync(configPath)) {
log(LogLevel.ERROR, "ntfy-config.json not found! Please create it in plugins/ directory", "ntfy-plugin")
throw new Error("Configuration file required: plugins/ntfy-config.json")
}
try {
const configData = readFileSync(configPath, "utf-8")
const config = JSON.parse(configData)
if (!config.server || !config.topic) {
throw new Error("Config must include 'server' and 'topic' fields")
}
return config
} catch (error) {
log(LogLevel.ERROR, `Failed to load ntfy config: ${error}`, "ntfy-plugin")
throw error
}
}
async function checkUpdatesWithNotification(config) {
try {
const res = await fetch("https://backend.rdil.rocks/peacock/latest-version/data")
const data = await res.json()
const { compare } = require("@peacockproject/core/utils")
const current = compare(PEACOCKVERSTRING, data.id)
const isOutdated = current === -1
if (isOutdated) {
await sendNtfyNotification(
config,
"Peacock Update Available!",
`Current: ${PEACOCKVERSTRING}\nLatest: ${data.id}\n\nCheck the Discord for the latest release.`
)
log(LogLevel.INFO, `Update notification sent: ${data.id} available`, "ntfy-plugin")
} else {
log(LogLevel.DEBUG, "No update available", "ntfy-plugin")
}
} catch (error) {
log(LogLevel.ERROR, `Failed to check updates: ${error}`, "ntfy-plugin")
}
}
async function sendNtfyNotification(config, title, message) {
try {
const response = await fetch(`${config.server}/${config.topic}`, {
method: "POST",
headers: {
"Title": title,
"Priority": "high",
"Tags": "rocket",
},
body: message,
})
if (!response.ok) {
throw new Error(`ntfy.sh responded with ${response.status}`)
}
log(LogLevel.INFO, "ntfy notification sent successfully", "ntfy-plugin")
} catch (error) {
log(LogLevel.ERROR, `Failed to send ntfy notification: ${error}`, "ntfy-plugin")
}
}
module.exports = async function (controller) {
let config
try {
config = loadConfig()
} catch (error) {
log(LogLevel.WARN, "ntfy plugin failed to load - skipping", "ntfy-plugin")
return
}
if (!config.enabled) {
log(LogLevel.INFO, "ntfy update notifications disabled", "ntfy-plugin")
return
}
log(LogLevel.INFO, "Starting ntfy update notifications (triggered on game start)", "ntfy-plugin")
controller.hooks.onUserLogin.tap("ntfy-updates", async (gameVersion, userId) => {
const now = Date.now()
if (now - lastCheckTime > CHECK_COOLDOWN) {
lastCheckTime = now
log(LogLevel.DEBUG, `User ${userId} logged in - checking for updates`, "ntfy-plugin")
await checkUpdatesWithNotification(config)
} else {
log(LogLevel.DEBUG, `User ${userId} logged in - skipping update check (cooldown)`, "ntfy-plugin")
}
})
await checkUpdatesWithNotification(config)
}