-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiscordSaper.py
375 lines (284 loc) · 12.1 KB
/
DiscordSaper.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import discord
from Game import game, time, MAX_SIZE, MIN_SIZE
import asyncio
import Resources
COMMANDS_PREFIX = '/'
ALLOWED_CHANNELS_IDS = [756959527919943680]
ACTIVE_GAMES = {} #'channel.id':game
'''
!start_game private 10X10 10
!end_game
!open x y
'''
lock = asyncio.Lock()
async def send_pole_rendered(channel,rendered):
lines = rendered.split('\n')
max_len = 0
for line in lines:
if len(line) > max_len:
max_len = len(line)
to_return = []
await lock.acquire()
banch = 2000//(max_len*2 + 2)
if banch >= len(lines):
banch = len(lines)
for i in range(0,len(lines)-1,banch):
message = ''
for n in range(i,i+banch):
try:
message += lines[n]+'\n'
except:
break
if message != '':
msg = await channel.send(message)
to_return.append(msg.id)
#lock.release()
#ids = ACTIVE_GAMES[str(channel.id)].last_messages.copy()
#for id in ACTIVE_GAMES[str(channel.id)].last_messages:
# msg = await channel.fetch_message(id)
# await msg.delete()
ACTIVE_GAMES[str(channel.id)].new_message(to_return)
lock.release()
#return to_return
async def delete_prev_messages(channel,ids):
for id in ids:
msg = await channel.fetch_message(id)
try:
await msg.delete()
except:
pass
#for id in ACTIVE_GAMES[str(channel.id)].last_messages:
# msg = await channel.fetch_message(id)
# await msg.delete()
async def process_message(message):
channel_id = str(message.channel.id)
if message.content[0] == COMMANDS_PREFIX: #is command
args = message.content.split(' ')
if args[0] == f'{COMMANDS_PREFIX}start_game':
game_exists = False
res = ACTIVE_GAMES.get(channel_id,False)
if res == False:
game_exists = False
else:
game_exists = True
if game_exists:
afk = not ACTIVE_GAMES[channel_id].is_alive()
else:
afk = True
if not afk:
await lock.acquire()
await message.channel.send(Resources.GAME_ALREADY_RUNNING)
lock.release()
return
if len(args) == 1:
ACTIVE_GAMES[channel_id] = game(message.author.id)
ACTIVE_GAMES[channel_id].new_message([message.id])
ACTIVE_GAMES[channel_id].generate_map()
rendered = ACTIVE_GAMES[channel_id].render_to_emodji()
task = asyncio.create_task(send_pole_rendered(message.channel,rendered))
await task
return
private = False
if 'private' in args:
private = True
size = False
n_bombs = False
for i in range(1,len(args)):
if 'X' in args[i]:
pr = args[i].split('X')
if len(pr) != 2:
await lock.acquire()
await message.channel.send(Resources.HELP_START)
lock.release()
return
try:
size = (int(pr[0]),int(pr[1]))
except:
await lock.acquire()
await message.channel.send(Resources.HELP_START)
lock.release()
return
else:
try:
n_bombs = int(args[i])
except:
pass
if size == False:
if n_bombs == False:
gm = game(message.author.id,private = private)
else:
gm = game(message.author.id,number_of_bombs=n_bombs,private = private)
elif n_bombs == False:
try:
gm = game(message.author.id,size,private = private)
except:
await lock.acquire()
await message.channel.send(f'Min size:{MIN_SIZE}, Max size:{MAX_SIZE}')
lock.release()
return
else:
try:
gm = game(message.author.id,size,n_bombs,private = private)
except:
await lock.acquire()
await message.channel.send(f'Min size:{MIN_SIZE}, Max size:{MAX_SIZE}')
lock.release()
return
gm.new_message([message.id])
ACTIVE_GAMES[channel_id] = gm
ACTIVE_GAMES[channel_id].generate_map()
rendered = ACTIVE_GAMES[channel_id].render_to_emodji()
task = asyncio.create_task(send_pole_rendered(message.channel,rendered))
await task
return
elif args[0] == f'{COMMANDS_PREFIX}end_game':
try:
ACTIVE_GAMES[channel_id]
except:
await lock.acquire()
await message.channel.send(Resources.NO_ACTIVE_GAMES)
lock.release()
return
await lock.acquire()
if ACTIVE_GAMES[channel_id].user_id != message.author.id:
return
ACTIVE_GAMES[channel_id].new_message([message.id])
await message.channel.send(Resources.GAME_ENDED)
ids = ACTIVE_GAMES[channel_id].last_messages.copy()
del ACTIVE_GAMES[channel_id]
lock.release()
task = asyncio.create_task(delete_prev_messages(message.channel,ids))
await task
elif args[0] == f'{COMMANDS_PREFIX}open' or args[0] == f'{COMMANDS_PREFIX}o':
try:
ACTIVE_GAMES[channel_id]
except:
return
if ACTIVE_GAMES[channel_id].private and ACTIVE_GAMES[channel_id].user_id != message.author.id:
return
ACTIVE_GAMES[channel_id].new_message([message.id])
if len(args) != 3:
await lock.acquire()
await message.channel.send(Resources.XY)
lock.release()
return
position = False
try:
position = (int(args[1])-1,int(args[2])-1)
except:
await lock.acquire()
await message.channel.send("Ok...Fucking values must be inputted")
lock.release()
return
res = ACTIVE_GAMES[channel_id].open_tile(position)
rendered = ACTIVE_GAMES[channel_id].render_to_emodji()
await send_pole_rendered(message.channel,rendered)
if not res:
return
if ACTIVE_GAMES[channel_id].is_alive():
if ACTIVE_GAMES[channel_id].check_win():
await lock.acquire()
await message.channel.send(Resources.WIN)
ids = ACTIVE_GAMES[channel_id].last_messages.copy()
del ACTIVE_GAMES[channel_id]
lock.release()
task = asyncio.create_task(delete_prev_messages(message.channel,ids))
await task
else:
await lock.acquire()
await message.channel.send(Resources.GAMEOVER)
try:
ids = ACTIVE_GAMES[channel_id].last_messages.copy()
del ACTIVE_GAMES[channel_id]
except:
return
lock.release()
task = asyncio.create_task(delete_prev_messages(message.channel,ids))
await task
elif args[0] == f'{COMMANDS_PREFIX}put_flag' or args[0] == f'{COMMANDS_PREFIX}pf':
try:
ACTIVE_GAMES[channel_id]
except:
return
if ACTIVE_GAMES[channel_id].private and ACTIVE_GAMES[channel_id].user_id != message.author.id:
return
ACTIVE_GAMES[channel_id].new_message([message.id])
if len(args) != 3:
await lock.acquire()
await message.channel.send(Resources.XY)
lock.release()
return
position = False
try:
position = (int(args[1])-1,int(args[2])-1)
except:
await lock.acquire()
await message.channel.send("Ok...Fucking values must be inputted")
lock.release()
return
res = ACTIVE_GAMES[channel_id].put_flag(position)
rendered = ACTIVE_GAMES[channel_id].render_to_emodji()
await send_pole_rendered(message.channel,rendered)
if not res:
return
if ACTIVE_GAMES[channel_id].is_alive():
if ACTIVE_GAMES[channel_id].check_win():
await lock.acquire()
await message.channel.send(Resources.WIN)
ids = ACTIVE_GAMES[channel_id].last_messages.copy()
del ACTIVE_GAMES[channel_id]
lock.release()
task = asyncio.create_task(delete_prev_messages(message.channel,ids))
await task
else:
await lock.acquire()
await message.channel.send(Resources.GAMEOVER)
ids = ACTIVE_GAMES[channel_id].last_messages.copy()
del ACTIVE_GAMES[channel_id]
lock.release()
task = asyncio.create_task(delete_prev_messages(message.channel,ids))
await task
elif args[0] == f'{COMMANDS_PREFIX}remove_flag' or args[0] == f'{COMMANDS_PREFIX}rf':
try:
ACTIVE_GAMES[channel_id]
except:
return
if ACTIVE_GAMES[channel_id].private and ACTIVE_GAMES[channel_id].user_id != message.author.id:
return
ACTIVE_GAMES[channel_id].new_message([message.id])
if len(args) != 3:
await lock.acquire()
await message.channel.send(Resources.XY)
lock.release()
return
position = False
try:
position = (int(args[1])-1,int(args[2])-1)
except:
await lock.acquire()
await message.channel.send("Ok...Fucking values must be inputted")
lock.release()
return
res = ACTIVE_GAMES[channel_id].remove_flag(position)
rendered = ACTIVE_GAMES[channel_id].render_to_emodji()
await send_pole_rendered(message.channel,rendered)
if not res:
return
elif args[0] == f'{COMMANDS_PREFIX}help':
await lock.acquire()
await message.channel.send(Resources.HELP+'\n'+Resources.HELP_START)
lock.release()
return
class MainClient(discord.Client):
async def on_ready(self):
print('Logged in')
async def on_message(self,message):
if len(ALLOWED_CHANNELS_IDS) != 0 and message.channel.id not in ALLOWED_CHANNELS_IDS:
return None
if message.author.id == Resources.BOT_ID:
return None
task = asyncio.create_task(process_message(message))
await task
if __name__ == '__main__':
client = MainClient()
client.run(Resources.API_KEY)