This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWayAFK.plugin.js
More file actions
105 lines (84 loc) · 3.34 KB
/
WayAFK.plugin.js
File metadata and controls
105 lines (84 loc) · 3.34 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
/**
* @name WayAFK
* @author SomewhereOutInSpace
* @license MIT
* @description A fix for AFK detection under Wayland display servers.
* @version 1.0.0
*/
const Dispatcher = BdApi.Webpack.getModule(m => m.dispatch && m.subscribe);
const Patcher = BdApi.Patcher;
var WayAFKConfig = {
timeout: 600
};
module.exports = class WayAFK {
constructor(meta) {
this.afk = false;
this.interval = null;
this.lastInteraction = new Date();
this.controller = new AbortController();
}
start() {
// Load configuration from disk.
WayAFKConfig = Object.assign({}, WayAFKConfig, BdApi.loadData("WayAFK", "settings"));
// Specify options (for abort controller) and interaction listener.
let options = { signal: this.controller.signal };
let listener = () => this.handleInteraction();
// Subscribe to all keydown/mousedown events with the listener.
document.addEventListener('keydown', listener, options);
document.addEventListener('mousedown', listener, options);
// Start an interval that checks if we should go AFK ever 1000ms.
this.interval = window.setInterval(() => this.checkElapsed(), 1000);
// Patches the dispatcher - if we are AFK and an AFK event is fired,
// this will unconditionally rewrite it to be true.
// (The Wayland bug seems to regularly generate false AFK events, so
// this handles those without having to spam true AFK events.)
BdApi.Patcher.before("WayAFK", Dispatcher, "dispatch", (that, args) => {
const [ event ] = args;
if (event.type === "AFK" && this.afk) {
event.afk = true;
}
});
}
stop() {
window.clearInterval(this.interval);
Patcher.unpatchAll("WayAFK");
this.controller.abort();
}
handleInteraction() {
if (this.afk) {
console.log("[WayAFK] Exiting AFK...");
this.afk = false;
}
this.lastInteraction = new Date();
}
checkElapsed() {
let elapsedTime = new Date() - this.lastInteraction;
elapsedTime /= 1000;
elapsedTime = Math.round(elapsedTime);
if (elapsedTime >= WayAFKConfig.timeout && !this.afk) {
console.log("[WayAFK] Spoofing AFK...");
Dispatcher.dispatch({type: 'AFK', afk: true});
this.afk = true;
}
}
getSettingsPanel() {
const panel = document.createElement("div");
panel.id = "WayAFK-settings";
const timeoutSetting = document.createElement("div");
timeoutSetting.classList.add("setting");
const timeoutLabel = document.createElement("span")
timeoutLabel.textContent = "Timeout (seconds) ";
timeoutLabel.style = "color:white";
const timeoutInput = document.createElement("input");
timeoutInput.type = "number";
timeoutInput.name = "buttonText";
timeoutSetting.append(timeoutLabel, timeoutInput);
timeoutInput.value = WayAFKConfig.timeout;
timeoutInput.addEventListener("change", () => {
WayAFKConfig.timeout = timeoutInput.value;
BdApi.saveData("WayAFK", "settings", WayAFKConfig);
});
panel.append(timeoutSetting);
return panel;
}
};