Locking an ahk script to a discord window.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hopit
Posts: 13
Joined: 11 May 2018, 19:59

Locking an ahk script to a discord window.

20 May 2018, 12:58

[note for people about to start typeing a thing about how selfbots are evil and make jesus cry: #1 its not for a spam bot its actually a fast delete bot, #2 This may be against the discord TOS but I don't see how it hurts anyone and anyway.]

Sorry for the first part I tried asking on the discord reddit page and got my post deleted and an angry DM from a moderator :D

Code: Select all

^Enter::
loop
{
send, {up}
send, ^a
send, {BS}
send, {Enter}
sleep, 100
}
^`::ExitApp
This works and does delete stuff very fast... however it seems to "escape" the discord window sometimes and mess up stuff. In order to prevent this I would like to lock the ahk script to the discord window ONLY. Note: before anyone says the obvious I did try ControlSend.
johntnev
Posts: 2
Joined: 19 Dec 2021, 16:35

Re: Locking an ahk script to a discord window.

19 Dec 2021, 18:22

12/19/2021
First, your script seems to be missing a second ENTER command after the BS key sequence. The first ENTER pops up the "are you sure" delete box and then you need a second ENTER key that selects the default for removing the selected line. When I run your key sequence by hand, it seems to actually take two passes to delete a single message?? am surprised the the Discord editor can keep up without sleep commands between each SEND command. The response of the editor is limited by your internet connection, server loads etc. etc. If you are running too fast the entire screen will be randomly highlighted.

Code: Select all

^Enter::
loop
{
send, {up}
send, ^a
send, {BS}
send, {Enter}
sleep, 100
;  Missing send,  {Enter}  ??
}
^`::ExitApp
[Mod edit: [code][/code] tags added.]
Allowing the delete script to run outside of the discord edit window can have unpleasant effects.

The following AHK code looks for the title "DISCORD" in the window where the script is started. If there is NOT a match, the
script displays a warning and the user can hit the ENTER Key to continue anyway or the script will exit after 5 seconds. The
code stores the window title in Window_name to allow the full script to exit if the window focus is changed by the user.

Note: The discord Web browser client does not display the word DISCORD in a DM session, but the standalone Discord client does. The Web browser client will display the DM user that you were chatting with and the Browser Name, (thus a way to continue running the delete script was needed). Everything is based on built in AHK calls and they are explained in the AHK documentation.

Code: Select all

WinGetActiveTitle, Window_name
Win_Discord := "DISCORD"
Found_Discord := InStr( Window_name, Win_Discord, false )	;AHK builtin function
If  !( Found_Discord >= 1 )
 {
 MsgBox, 1,!!WARNING!! ,The Client Window "%Win_Discord%" NOT Found.  Found "%Window_name%"%lf_char%%lf_char%Press ENTER Key to Ignore Warning and Continue., 5  ; -seconds before timeout.
 IfMsgBox, Cancel
    exit  ; User pressed the "Cancel" button.

;Tell the user what is happening if the script quits
 IfMsgBox, Timeout  
    {
    SplashTextOn,,, Script EXITING Now
    Sleep 2000
    SplashTextOff
    exit ; i.e. Assume "Cancel" if it timed out.
	}
 }
 WinActivate, Window_name 			;Refocus in case user moved mouse to click
 ;

To keep the script running in the discord DM edit window, you need to add a check for the active window name determined above and EXIT if the focus is changed by the user. The code is needed at the beginning of the "LOOP" to try and prevent any unintended deletions in another window.

Code: Select all

if ( !( WinActive(Window_name) ) ) {
		ExitApp						;user changed focus. Exit
		}
Your complete script:

Code: Select all

;Save as    DiscordDM_Delete1.ahk
;
;Date 12/19/21  Version 0.01 Direct Message(DM) delete script for Win10 Stand Alone Discord Client, Brave, FireFox64
;
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^Enter::

WinGetActiveTitle, Window_name
Win_Discord := "DISCORD"
Found_Discord := InStr( Window_name, Win_Discord, false )	;AHK builtin function
If  !( Found_Discord >= 1 )
 {
 MsgBox, 1,!!WARNING!! ,The Client Window "%Win_Discord%" NOT Found.  Found "%Window_name%"%lf_char%%lf_char%Press ENTER Key to Ignore Warning and Continue., 5  ; -seconds before timeout.
 IfMsgBox, Cancel
    exit  ; User pressed the "Cancel" button.

;Tell the user what is happening if the script quits
 IfMsgBox, Timeout  
    {
    SplashTextOn,,, Script EXITING Now
    Sleep 2000
    SplashTextOff
    exit ; i.e. Assume "Cancel" if it timed out.
	}
 }
 WinActivate, Window_name 			;Refocus in case user moved mouse to click

loop
{
	if ( !( WinActive(Window_name) ) ) {
		ExitApp						;user changed focus. Exit
		}
send, {up}
sleep, 100
send, ^a
sleep, 200
send, {BS}
sleep, 100
send, {Enter}
sleep, 100
send, {Enter}
sleep, 300
}
^`::ExitApp

A script that I have been using that stays in memory. Starts with ^j and can gracefully exit by HOLDING SHIFT Key down :

Code: Select all

;Save as    DiscordDM_Delete4.ahk

;Date 12/16/21  Version 0.14 Direct Message(DM) delete script for Win10 Stand Alone Discord Client, Brave, FireFox64

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^j::


global lf_char := CHR(10)  ;AKA ( newline char ) needed for posting to YouTube
; Check for Discord Message Window.
WinGetActiveTitle, Window_name
Win_Discord := "DISCORD"
Found_Discord := InStr( Window_name, Win_Discord, false )	;AHK builtin function
If  !( Found_Discord >= 1 )
 {
 MsgBox, 1,!!WARNING!! ,The Client Window "%Win_Discord%" NOT Found.  Found "%Window_name%"%lf_char%%lf_char%Press ENTER Key to Ignore Warning and Continue., 5  ; -seconds before timeout.
 IfMsgBox, Cancel
    exit  ; User pressed the "Cancel" button.
 IfMsgBox, Timeout  
    {
    SplashTextOn,,, Script EXITING Now
    Sleep 2000
    SplashTextOff
    exit ; i.e. Assume "Cancel" if it timed out.
	}
 }
 WinActivate, Window_name 			;Refocus in case user moved mouse to click
 ;
;     Set sleep wait times to allow Discord server to keep up.
waitTime := 100						;Increase by 25 if delete is intermittent 
2_waitTime := ( waitTime * 2 )
3_waitTime := ( waitTime * 3 )
4_waitTime := ( waitTime * 4 )
;
Loop_max := 300
;
ToolTip, Delete DM Script Running in %Window_name%. HOLD SHIFT Key to Stop.
send, {Escape}
;
Loop, %Loop_max%
{
	GetKeyState, state, Shift		;Check if either SHIFT key is held DOWN to stop script
	if ( !( WinActive(Window_name) ) or (state = "D") ) {
		Break						;user changed focus or shift key down. Exit loop
		}
    send, {Up}
    send, ^a
	sleep, waitTime       			;Needed to allow the server time to highlight text
    send, {BS}
    sleep, waitTime
    send, {Enter}
    sleep, 2_waitTime
    send, {Enter}
    sleep, 4_waitTime				;increase waitTime if intermittent highlighting
  	send, {Escape}   				;exits edit mode
	sleep, waitTime
	send, {Escape}					;sends focus to chat input line
	sleep, waitTime
}
ToolTip
send, {Escape}   					;sends focus to chat input line
    SplashTextOn,,, Script EXITING Now
    Sleep 2000
    SplashTextOff
exit
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 19 Dec 2021, 18:31, edited 1 time in total.
Reason: Please use code tags. Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Descolada, Nerafius and 210 guests