- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBot.py
More file actions
Latest commit
178 lines (158 loc) · 4.56 KB
/
Copy pathGameBot.py
File metadata and controls
178 lines (158 loc) · 4.56 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
#coding=utf-8
importos
importdiscord, platform, asyncio
importrandom, time
importsubprocess
importpyscreenshotasImageGrab
importio
frompynput.keyboardimportKey, Controller
client=discord.Client()
token="[ADD YOUR TOKEN HERE]"
loadedrom="PokemonRed.gb"
pathtorom=os.getcwd() +"/"+loadedrom
msg=None
ch=None
UpdateLimit=3
CurrentUpdate=0
keyboard=Controller()
movtime=0.25
# Emotes
emotes= {"LeftArrow": "\u2B05", "DownArrow": "\u2B07", "UpArrow": "\u2B06", "RightArrow": "\u27A1", "AButton": "\U0001F170", "BButton": "\U0001F171", "Start": "\u25B6", "Select": "\U0001F502"}
defIsValidReaction(react):
globalemotes
foremoteinemotes:
if (react==emotes[emote]):
returnTrue
returnFalse
defGetWindowCoords():
outs=str(subprocess.check_output(["wmctrl", "-lG"]))[2:-1].split("\\n")
forfinouts:
if (f.find("Gambatte SDL") !=-1): #Found Window
f2=f.split(" ")
f= []
foreleminf2:
if (elem!=""):
f.append(elem)
# Brut
X1=int(f[2])
Y1=int(f[3]) -26
X2=X1+int(f[4]) -1
Y2=Y1+int(f[5])
return (X1, Y1, X2, Y2)
returnNone
asyncdefUpdateFrame():
globalmsg
globalCurrentUpdate
globalUpdateLimit
whileTrue:
if (ch!=None):
CurrentUpdate+=1
if (CurrentUpdate<UpdateLimit):
print ("Updating Frame...")
awaitSendImage()
elif (CurrentUpdate==UpdateLimit):
print ("Updating Frame+Emojis...")
awaitSendImage(True) # React
print ("All good.")
else:
awaitasyncio.sleep(0.5)
else:
awaitasyncio.sleep(0.5)
asyncdefSendImage(react=False):
globalmsg
globalch
globalemotes
coords=GetWindowCoords()
os.system("wmctrl -a 'Gambatte SDL'") # Focus Window
im=ImageGrab.grab(bbox=(coords[0], coords[1], coords[2], coords[3])) # X1,Y1,X2,Y2
im.save("frame.jpg")
msgold=msg
msg=awaitch.send(file=discord.File("frame.jpg"))
if (msgold!=None):
try:
awaitmsgold.delete()
except:
pass
if (react):
awaitmsg.add_reaction(emotes["AButton"])
awaitmsg.add_reaction(emotes["BButton"])
awaitmsg.add_reaction(emotes["LeftArrow"])
awaitmsg.add_reaction(emotes["DownArrow"])
awaitmsg.add_reaction(emotes["UpArrow"])
awaitmsg.add_reaction(emotes["RightArrow"])
awaitmsg.add_reaction(emotes["Start"])
awaitmsg.add_reaction(emotes["Select"])
@client.event
asyncdefon_ready():
print('All good! Name: '+client.user.name)
asyncio.ensure_future(UpdateFrame()) # Fire and Forget
awaitclient.change_presence(activity=discord.Game(name='%activate'))
asyncdefSendKey(k, movkey=False):
globalkeyboard
globalmovtime
keyboard.press(k)
if (movkey==False):
awaitasyncio.sleep(0.25)
else:
awaitasyncio.sleep(movtime)
keyboard.release(k)
@client.event
asyncdefon_reaction_add(reaction, user):
globalmsg
globalemotes
globalCurrentUpdate
if (reaction.message.author==client.userandreaction.count!=1): # We got a react
reaction=str(reaction) # Simplify
if (IsValidReaction(reaction)):
print ("Input Received: "+reaction)
os.system("wmctrl -a 'Gambatte SDL'") # Focus Window
if (reaction==emotes["LeftArrow"]):
awaitSendKey(Key.left, True)
elif (reaction==emotes["DownArrow"]):
awaitSendKey(Key.down, True)
elif (reaction==emotes["UpArrow"]):
awaitSendKey(Key.up, True)
elif (reaction==emotes["RightArrow"]):
awaitSendKey(Key.right, True)
elif (reaction==emotes["AButton"]):
awaitSendKey("d")
elif (reaction==emotes["BButton"]):
awaitSendKey("c")
elif (reaction==emotes["Start"]):
awaitSendKey(Key.enter)
elif (reaction==emotes["Select"]):
awaitSendKey(Key.shift_r)
CurrentUpdate=0
@client.event
asyncdefon_message(message):
globalmsg
globalch
globalCurrentUpdate
globalmovtime
ifmessage.author==client.user:
return
if (message.content=="%activate"):
if (GetWindowCoords() ==None): # No emulator running
os.system("gambatte_sdl "+pathtorom+" --scale 2 &")
while (GetWindowCoords() ==None): # Avoid any issues
time.sleep(0.1)
if (ch!=message.channel): # Active on a different channel
ch=message.channel
msg=None
CurrentUpdate=0
elif (message.content=="%stop"):
awaitmsg.delete()
ch=None
elif (message.content=="%loadrom"):
pass
elif (message.content[:8] =="%movtime"):
inp=message.content[9:]
movtime=float(inp)
if (ch!=Noneandmessage.channel==ch):
awaitch.send("Setting movement speed as: **"+str(inp) +"** (Default: 0.25)")
whileTrue:
try:
client.run(token)
client.close()
exceptExceptionase:
print (e)