I'm fairly new to programming, so please forgive me if I overlook any obvious logic or programming concepts.
First, I'll describe what I'm trying to accomplish.
A friend of mine has written a Discord bot for our server that randomly drops a fake currency which can then be used to gamble with the bot. My goal is to use AHK to automatically read the chat for these random drops and enter the command to claim them whenever a drop is detected. Ideally I would like the scanning and text input to happen in the background, but I haven’t been able to figure out if that's possible. In a perfect world, the script would run without affecting normal computer use.
Please chime in with other approaches - I mentioned that I'm new to this so I may not be approaching the problem in the best way. Some of what I'm asking may not be possible in AHK.
Based on my searches here, one good way to do this could be
- Use WinActivate to target Discord Window
- Use the Click command to highlight recent messages in Discord
- Scan the clipboard for the trigger phrase using IfInString
- If trigger phrase detected:
--- Target Discord’s input field
--- Enter the claim command with Send
--- AltTab back to previous task
- Repeat scanning
I’ve been able to Frankenstein a some of these functions together into the following code:
Code: Select all
WinActivate, ahk_exe DiscordCanary.exe ; target discord window
Sleep, 50 ; Wait 50ms
Click 315 75 down ; This clicks at the pixel 315, 75 relative to the active window.
Sleep 50 ; wait 50ms
Click 680 400 up ; drags cursor to pixel 680, 400 relative to the active window, highlights the rows between
Sleep 50 ; wait 50ms
Send ^c ; press ctrl+c to copy highlighted text to clipboard
Sleep 50 ; wait 50ms
IfInString, clipboard, arrived for duty! ; check clipboard for trigger phrase “arrived for duty!”
{
Send .e{Return} ; sends ".e" which is the claim command
MsgBox Success! ; shows visual confirmation that script worked
}
return
A few things I’ve been unable to figure out:
Needs to target Discord's input field - using ControlFocus?
Needs trigger to run at (x) interval - no idea
Needs to run in stealth (background) - also no idea (maybe not possible?)
The WindowSpy information for the text field I'm trying to target is listed below. I'm having a rough time getting ControlFocus to work as I'm unable to find many low-level examples.
#bot-spam - Discord
ahk_class Chrome_WidgetWin_1
ahk_exe DiscordCanary.exe
ahk_pid 17220
Sorry for the wall of text.
Any help would be greatly appreciated!!
edit: This actually works pretty well. Turns out there's no need to re-target the input field after highlighting. Can anybody help with loops or running this in the background?