Snailcat Auto DM Bot

Automatically sends messages to a Snailcat on web browser

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Snailcat Auto DM Bot
// @namespace    http://tampermonkey.net/
// @version      3.4
// @description  Automatically sends messages to a Snailcat on web browser
// @match        https://discord.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    console.log("Script started");

    function sendMessage(content) {
        const messageInput = document.querySelector('div[contenteditable="true"][data-slate-editor="true"]');
        if (!messageInput) {
            console.error('Message input field not found. Retrying...');
            setTimeout(() => sendMessage(content), 2000);
            return;
        }

        messageInput.focus();

        // Clear existing content by selecting all and deleting
        const selection = window.getSelection();
        const range = document.createRange();
        range.selectNodeContents(messageInput);
        selection.removeAllRanges();
        selection.addRange(range);

        const deleteEvent = new InputEvent('beforeinput', {
            bubbles: true,
            cancelable: true,
            inputType: 'deleteContentBackward',
        });
        messageInput.dispatchEvent(deleteEvent);

        // Insert new content using 'insertText' inputType
        for (let i = 0; i < content.length; i++) {
            const char = content[i];
            const inputEvent = new InputEvent('beforeinput', {
                data: char,
                bubbles: true,
                cancelable: true,
                inputType: 'insertText',
            });
            messageInput.dispatchEvent(inputEvent);
        }

        // Send the message by simulating 'Enter' keydown event
        const enterEvent = new KeyboardEvent('keydown', {
            key: 'Enter',
            code: 'Enter',
            keyCode: 13,
            which: 13,
            bubbles: true,
            cancelable: true,
        });
        messageInput.dispatchEvent(enterEvent);

        console.log("Message sent:", content);
    }

    // Send both messages immediately, with retry logic if input is not available
    sendMessage('snailcat beg');
    sendMessage('snailcat daily');

    // Send "snailcat beg" every 10 minutes with an additional 10-second buffer (610,000 milliseconds)
    setInterval(() => sendMessage('snailcat beg'), 610000);

    // Send "snailcat daily" once every hour with an additional 1-minute buffer (3,600,000 milliseconds)
    setTimeout(function dailyMessage() {
        sendMessage('snailcat daily');
        setTimeout(dailyMessage, 3600000); // Schedule next daily message
    }, 3600000); // Initial delay for the first daily message after immediate send
})();