🧩
Plugin

AutoIdleOnAFK

von HeadX

Automatically updates your discord status to 'idle' when you haven't opened your discord client for more than 5 minutes.

🏷️ v0.5.2 📦 19.7 KB 🗓️ 2.7.2026 📄 576 Zeilen
⬇️ Download 📄 Raw ansehen 🌐 Source
Installation: Datei herunterladen und in deinen BetterDiscord-Ordner legen — %appdata%\\BetterDiscord\\plugins (Windows) bzw. über Discord: Einstellungen → Plugins → Plugin-Ordner öffnen.
AutoIdleOnAFK.plugin.js · 576 Zeilen · 19.7 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
/*
  FILE META START
  name: AutoIdleOnAFK.plugin.js
  version: 0.0.10
  description: Multi-surface admin, cloud, bot host, login, and secure chat server.
  lastModified: 2026-05-04T20:22:59.631Z
  checksum: 7307067ddf36183df253a2b324cafe7c6f991d904f41ccc1419a116cee83c5f7
  FILE META END
*/

/**
 * @name AutoIdleOnAFK
 * @author HeadX
 * @authorId 808385710700494919
 * @description Automatically updates your discord status to 'idle' when you haven't opened your discord client for more than 5 minutes.
 * @version 0.5.2
 * @invite J6wTJJ5fp
 * @website https://headcraft.cloud/better-discord
 * @source https://headcraft.cloud/better-discord/AutoIdleOnAFK.plugin.js
 * @updateUrl https://headcraft.cloud/better-discord/AutoIdleOnAFK.plugin.js
 */


/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ "./src/AutoIdleOnAFK/settings.js":
/*!***************************************!*\
  !*** ./src/AutoIdleOnAFK/settings.js ***!
  \***************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ Settings)
/* harmony export */ });
const defaults = {
    changelog: [
        {
            type: "fixed",
            title: "(v0.5.1) Updated Deprecated Methods",
            items: [
                "just a small update which updates the functions that have been deprecated in BD Api to newer ones",
                "Credits to **MsNecromancer** (GitHub & Discord) for her time and efforts spent on this `:)`",
            ],
        },
        {
            type: "fixed",
            title: "(v0.5.0) Fixed Settings Page & Plugin Refactor!",
            items: [
                "It's been a _longgg_ while since this plugin has received any update\nI've been busy with life. :)",
                "This update fixes the settings panel, which broke a month or so back",
                "In doing so, plugin was refactored to remove the  dependency on `ZeresPluginLibrary` keeping up with the new way to write BD plugins.",
                "**A \\*HUGE\\*** credit goes to **opdude** (GitHub & Discord) for their time and effort in this refactor and the fixes!",
            ],
        },
    ],
    settings: [
        {
            type: "radio",
            name: "Change Status To:",
            note: "the status selected will be switched to when AFK. default: Idle",
            id: "afkStatus",
            value: "idle",
            options: [
                {
                    name: "Idle",
                    value: "idle",
                },
                {
                    name: "Invisible",
                    value: "invisible",
                },
                {
                    name: "Do Not Disturb",
                    value: "dnd",
                },
            ],
        },
        {
            type: "slider",
            name: "AFK Timeout (minutes)",
            note: "minutes to wait before changing your status to Idle/Invisible",
            id: "afkTimeout",
            defaultValue: 5,
            min: 5,
            max: 30,
            units: "min",
            markers: [5, 10, 15, 20, 25, 30],
        },
        {
            type: "slider",
            name: "Back To Online Delay (Grace/Cooldown Period)",
            note: "seconds to wait before changing your status back to Online. if you close or unfocus discord window, status will not be changed",
            id: "backToOnlineDelay",
            defaultValue: 10,
            value: 10,
            min: 5,
            max: 120,
            units: "s",
            markers: [5, 10, 30, 60, 90, 120],
        },
        {
            type: "switch",
            name: "Ignore VC state when going AFK",
            note: "Allows going to Idle mode even when you are in a VC. default: off",
            id: "ignoreVCState",
            value: false,
            defaultValue: false,
        },
        {
            type: "switch",
            name: "Always Revert To Online",
            note: "Come back online even if this plugin didn't change your status. Useful for multiple devices.",
            id: "alwaysOnline",
            value: false,
            defaultValue: false,
        },
        {
            type: "switch",
            name: "Show toast messages?",
            note: 'toggles the visibility of "Changing status back to online" toast message',
            id: "showToasts",
            defaultValue: true,
            value: true,
        },
        {
            type: "switch",
            name: "Enable debug mode?",
            note: "toggles the visibility of debug messages and decreases AFK timeout for testing purposes",
            id: "showDebug",
            defaultValue: false,
            value: false,
        },
    ],
};

class Settings {
    constructor() {
        const settings = defaults;
        const storedData = BdApi.Data.load("AutoIdleOnAFK", "settings") || {};
        settings.settings.forEach((setting) => {
            const storedSetting = (storedData.settings || []).find(
                (s) => s.id === setting.id,
            );
            if (storedSetting && storedSetting.value !== undefined) {
                setting.value = storedSetting.value;
            }
        });

        this.settings = settings;
    }

    getValue(id) {
        const setting = this.settings.settings.find((s) => s.id === id);
        if (setting) {
            return setting.value;
        }
        return null;
    }

    getSettingsPanel() {
        return BdApi.UI.buildSettingsPanel({
            settings: this.settings.settings,
            onChange: (_, id, value) => {
                const setting = this.settings.settings.find((s) => s.id === id);
                if (setting) {
                    setting.value = value;
                    BdApi.Data.save("AutoIdleOnAFK", "settings", {
                        settings: this.settings.settings,
                    });
                }
            },
        });
    }
}


/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
(() => {
/*!************************************!*\
  !*** ./src/AutoIdleOnAFK/index.js ***!
  \************************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ AutoIdleOnAFK)
/* harmony export */ });
/* harmony import */ var _settings__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./settings */ "./src/AutoIdleOnAFK/settings.js");
/*
MIT License

Copyright (c) 2022-2025 RoguedBear

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


class AutoIdleOnAFK {
    UserSettingsProtoStore = BdApi.Webpack.getModule(
        (m) =>
            m &&
            typeof m.getName == "function" &&
            m.getName() == "UserSettingsProtoStore" &&
            m,
        {
            first: true,
            searchExports: true,
        },
    );

    UserSettingsProtoUtils = BdApi.Webpack.getModule(
        (m) =>
            m.ProtoClass &&
            m.ProtoClass.typeName.endsWith(".PreloadedUserSettings"),
        {
            first: true,
            searchExports: true,
        },
    );

    SelectedChannelStore = BdApi.Webpack.getByKeys("getLastSelectedChannelId");

    constructor(meta) {
        this._config = {
            info: {
                name: "AutoIdleOnAFK",
            },
        };
        this.meta = meta;

        this.settings = new _settings__WEBPACK_IMPORTED_MODULE_0__["default"]();

        // Some instance variables
        this.afkTimeoutID = undefined; // timeout set by onBlur
        this.backFromAFKTimeoutID = undefined; // timeout set by onFocus
        // key used as a flag to detect if afk was set by the plugin or manually by user
        this.keyIdleSetByPlugin = "IdleSetByPlugin";

        this.OnBlurBounded = this.onBlurFunction.bind(this);
        this.boundOnFocusBounded = this.onFocusFunction.bind(this);
    }

    getSettingsPanel() {
        return this.settings.getSettingsPanel();
    }

    start() {
        if (this.settings.getValue("showDebug") === true) {
            this.log_debug(this);
            this.log_debug("Current status: " + this.currentStatus());
            this.log_debug("In Voice Channel: " + this.inVoiceChannel());
            this.log_debug(
                "onlineStatusAndNotInVC: " + this.onlineStatusAndNotInVC(),
            );
        }

        window.addEventListener("blur", this.OnBlurBounded);
        window.addEventListener("focus", this.boundOnFocusBounded);
        this.showChangelog();
    }

    stop() {
        clearTimeout(this.afkTimeoutID);
        clearTimeout(this.backFromAFKTimeoutID);
        window.removeEventListener("blur", this.OnBlurBounded);
        window.removeEventListener("focus", this.boundOnFocusBounded);
    }

    onBlurFunction() {
        this.log_debug("Focus lost from discord window");

        // if user closes discord before the backToOnlineTimeout, clear the
        // pending timeout (if it even exists)
        this.backFromAFKTimeoutID = this.cancelTimeout(
            this.backFromAFKTimeoutID,
        );

        var __afkSetByPlugin = BdApi.Data.load(
            this._config.info.name,
            this.keyIdleSetByPlugin,
        );
        var inVoiceChannelAndIdleSetByPlugin =
            this.inVoiceChannel() && __afkSetByPlugin;

        var inVoiceChannelAndAlwaysOnlineEnabled =
            this.inVoiceChannel() && this.settings.getValue("alwaysOnline");

        if (this.onlineStatusAndNotInVC()) {
            var _timeout_ms =
                this.settings.getValue("afkTimeout") *
                (this.settings.getValue("showDebug") ? 2 : 60) *
                1000;
            this.log_debug("setting timeout of " + _timeout_ms + "ms");
            this.afkTimeoutID = setTimeout(() => {
                if (this.onlineStatusAndNotInVC()) {
                    this.updateStatus(this.settings.getValue("afkStatus"));
                    BdApi.Data.save(
                        this._config.info.name,
                        this.keyIdleSetByPlugin,
                        true,
                    );
                }
                // If DEBUG is enabled then keep a shorter duration than 60s
            }, _timeout_ms); // converting min to ms
        } else if (
            // if the user is in a VC, idle was set by plugin &
            // their current status == afkStatus
            (inVoiceChannelAndIdleSetByPlugin ||
                // OR, is user in VC and always online is enabled
                inVoiceChannelAndAlwaysOnlineEnabled) &&
            this.currentStatus() == this.settings.getValue("afkStatus")
        ) {
            this.updateStatus("online");
            this.showToast("Changing status back to online, You are in VC");
            BdApi.Data.save(
                this._config.info.name,
                this.keyIdleSetByPlugin,
                false,
            );
        }
    }

    onFocusFunction() {
        this.log_debug("Discord window in focus now");

        // if user opens discord before the afkTimeout, clear the pending
        // timeout (if it even exists)
        this.afkTimeoutID = this.cancelTimeout(this.afkTimeoutID);

        this.log_debug(
            "Setting timeout of " +
                this.settings.getValue("backToOnlineDelay") * 1000 +
                " ms",
        );
        this.backFromAFKTimeoutID = setTimeout(() => {
            // TODO: Refactor/comment out/test more this part
            var __afkSetByPlugin = BdApi.Data.load(
                this._config.info.name,
                this.keyIdleSetByPlugin,
            );
            var statusIsAFKAndWasSetByPlugin =
                this.currentStatus() === this.settings.getValue("afkStatus") &&
                __afkSetByPlugin === true;

            var statusIsAFKAndAlwaysOnlineIsTrue =
                this.currentStatus() === this.settings.getValue("afkStatus") &&
                this.settings.getValue("alwaysOnline") === true;

            if (statusIsAFKAndWasSetByPlugin) {
                this.showToast("Changing status back to online");
                this.updateStatus("online");
                BdApi.Data.save(
                    this._config.info.name,
                    this.keyIdleSetByPlugin,
                    false,
                );
            } else if (statusIsAFKAndAlwaysOnlineIsTrue) {
                /**
                     * Note: Using BdApi.UI.showToast() directly instead of
                     * this.showToast()
                     *
                     * The showToast() method only displays messages if the user has
                     * enabled the "Show toast messages" setting. However, when the
                     * plugin changes the user's status from idle back to online, I
                     * believe we should notify the user regardless of their toast
                     * preference

                    */
                BdApi.UI.showToast("Changing status back to online");
                this.updateStatus("online");
                BdApi.Data.save(
                    this._config.info.name,
                    this.keyIdleSetByPlugin,
                    false,
                );
            } else if (__afkSetByPlugin == undefined) {
                return;
            } else {
                BdApi.Data.delete(
                    this._config.info.name,
                    this.keyIdleSetByPlugin,
                );
                this.log_debug(
                    "User overrode the status. leaving status unchanged...",
                );
            }
        }, this.settings.getValue("backToOnlineDelay") * 1000);
    }

    /**
     * Cancles the given timeout id
     * @param {number} timeoutId
     * @returns {undefined}
     */
    cancelTimeout(timeoutId) {
        if (timeoutId != undefined) {
            this.log_debug("Cancelling timeout " + timeoutId);
            clearTimeout(timeoutId);
            return undefined;
        }
    }

    /**
     * @returns {string} the current user status
     */
    currentStatus() {
        return this.UserSettingsProtoStore.settings.status.status.value;
    }
    /**
     * @returns {boolean} if user is in a VC
     */
    inVoiceChannel() {
        return (
            this.settings.getValue("ignoreVCState") === false &&
            this.SelectedChannelStore.getVoiceChannelId() !== null
        );
    }

    /**
     * Returns if the current status is 'online' and the user is not in a
     * voice channel
     * @returns
     */
    onlineStatusAndNotInVC() {
        return (
            this.currentStatus() === "online" && this.inVoiceChannel() === false
        );
    }

    /**
     * Updates the remote status to the param `toStatus`
     * @param {('online'|'idle'|'invisible'|'dnd')} toStatus
     */
    updateStatus(toStatus) {
        if (
            this._config.DEBUG === true &&
            this._config.DEBUG_ActuallyChangeStatus === false
        ) {
            this.log_debug(
                "Changing (but not changing) status to: " + toStatus,
            );
            return;
        }
        this.log_debug("Actually changing status to: " + toStatus);
        this.UserSettingsProtoUtils.updateAsync(
            "status",
            (statusSetting) => {
                this.log_debug(statusSetting);
                statusSetting.status.value = toStatus;
            },
            0,
        );
    }

    /**
     * shows toast message based on showToast settings
     * @param {string} msg
     */
    showToast(msg) {
        if (this.settings.getValue("showToasts")) {
            BdApi.UI.showToast(msg);
        }
    }

    log_debug(module, ...message) {
        if (this.settings.getValue("showDebug") !== true) {
            return;
        } else {
            console.log(module, ...message);
        }
    }

    showChangelog() {
        const currentVersionInfo = BdApi.Data.load(
            this._config.info.name,
            "currentVersionInfo",
        );
        if (
            currentVersionInfo &&
            currentVersionInfo.version &&
            currentVersionInfo.version === this.meta.version
        ) {
            return;
        }
        BdApi.UI.showChangelogModal({
            title: this.meta.name,
            subtitle: "version " + this.meta.version,
            changes: this.settings.settings.changelog,
        });
        BdApi.Data.save(this._config.info.name, "currentVersionInfo", {
            version: this.meta.version,
        });
    }
}

})();

module.exports = __webpack_exports__["default"];
/******/ })()
;
← Alle Plugins & Themes index.json headcraft.cloud