-
Notifications
You must be signed in to change notification settings - Fork 5
/
autotyper.py
175 lines (137 loc) · 4.17 KB
/
autotyper.py
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
"""discord-autotyper-python
Delano Lourenco (https://delano-lourenco.web.app)
Repo: https://github.com/3ddelano/discord-autotyper-python
"""
import json
from math import floor
from threading import Timer
from pynput.keyboard import Key, Controller, Listener
from random import randint
try:
open("./settings.json")
except FileNotFoundError:
print("Error: settings.json file not found in directory.")
quit()
settings = json.load(open("./settings.json"))
queue = []
onetime_queue = []
timers = []
cmd_count = 0
started = False
exited = False
cooldown = False
onetime_cooldown = False
onetime_delay = int(settings["onetime"]["delay"])
randomTime = int(settings["randomTime"])
command_delay = float(settings["commandDelay"])
stop_after = float(settings["stopAfter"])
controller = Controller()
def stop_typer():
global started
if started:
started = False
print("Auto stopped.")
def start_stop_after():
global stop_after
if stop_after > 0:
Timer(stop_after*60, stop_typer).start()
def send_command(text):
global controller
controller.type(text)
controller.press(Key.enter)
controller.release(Key.enter)
def add_command(text, waittime, isRandom):
global started
global queue
global timers
global cmd_count
global randomTime
if started:
random_delay = 0
if isRandom:
random_delay = floor(randint(0, randomTime))
if randint(0, 100) / 100 <= settings["randomSkip"]:
print(
f"skipped command: {text} | next in {waittime + random_delay}s | total commands: {cmd_count}")
else:
cmd_count += 1
queue.append({"text": text, "waittime": waittime})
print(
f"sending cmd: {text} | next in {waittime + random_delay}s | total commands: {cmd_count}")
t = Timer(waittime + random_delay, add_command, args=(
text, waittime, isRandom))
t.start()
timers.append(t)
def init_typer():
global queue
global timers
commands = settings["commands"]
queue = []
for timer in timers:
timer.cancel()
timers = []
for cmd in commands:
add_command(cmd["text"], int(cmd["waittime"]),
cmd["randomtime"] or False)
def remove_cooldown():
global cooldown
cooldown = False
def remove_onetime_cooldown():
global onetime_cooldown
onetime_cooldown = not onetime_cooldown
def press_backspace():
global controller
controller.press(Key.backspace)
controller.release(Key.backspace)
def on_key_press(key):
if hasattr(key, "char"):
key = str(key.char)
else:
key = str(key)
if settings["showKeyCode"]:
print("Key pressed: ", key)
global started
global exited
if key == settings["hotkey"]:
press_backspace()
started = not started
if started:
print("Started.")
init_typer()
start_stop_after()
else:
print("Stopped.")
if key == settings["exitkey"]:
press_backspace()
print("Exited.")
started = False
exited = True
return False
global onetime_cooldown
global onetime_queue
if key == settings["onetime"]["hotkey"]:
press_backspace()
print("Started onetime cmds.")
cmds = settings["onetime"]["commands"]
for i in cmds:
onetime_queue.append(i)
print("Discord AutoTyper\nWaiting for hotkey to be pressed...")
listener = Listener(on_press=on_key_press)
listener.start()
while not exited:
if started:
if len(queue) > 0:
if not cooldown:
cooldown = True
send_command(queue[0]["text"])
queue.pop(0)
Timer(command_delay, remove_cooldown).start()
if len(onetime_queue) > 0:
if not onetime_cooldown:
onetime_cooldown = True
send_command(onetime_queue[0])
print(f"sending onetime cmd: {onetime_queue[0]}")
onetime_queue.pop(0)
if len(onetime_queue) == 0:
print("finished sending onetime cmds.")
Timer(onetime_delay, remove_onetime_cooldown).start()