q6 commited on
Commit
08bd593
·
1 Parent(s): 8e76c77

Run Discord interaction periodically

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py CHANGED
@@ -11,6 +11,7 @@ import sys
11
  import tempfile
12
  import threading
13
  import time
 
14
  import urllib.request
15
  import uuid
16
  import warnings
@@ -294,6 +295,40 @@ GPU_RETRY_ERRORS = (
294
  "gpu task aborted",
295
  "uncorrectable ecc error",
296
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
  jobs = {}
299
  images = {}
@@ -315,6 +350,25 @@ def log(message):
315
  print(message, flush=True)
316
 
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  def retry_gpu(call):
319
  for attempt in range(GPU_ATTEMPTS):
320
  try:
@@ -4026,4 +4080,5 @@ if __name__ == "__main__":
4026
  prevent_thread_lock=True,
4027
  )
4028
  threading.Thread(target=preload_startup_assets, daemon=True).start()
 
4029
  demo.block_thread()
 
11
  import tempfile
12
  import threading
13
  import time
14
+ import urllib.error
15
  import urllib.request
16
  import uuid
17
  import warnings
 
295
  "gpu task aborted",
296
  "uncorrectable ecc error",
297
  )
298
+ DISCORD_INTERVAL = 15
299
+ DISCORD_URL = "https://discord.com/api/v9/interactions"
300
+ DISCORD_HEADERS = {
301
+ "Accept": "*/*",
302
+ "Content-Type": "application/json",
303
+ "Origin": "https://discord.com",
304
+ "Referer": (
305
+ "https://discord.com/channels/1037784194476163223/"
306
+ "1195860393206231090"
307
+ ),
308
+ "User-Agent": (
309
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
310
+ "AppleWebKit/537.36 Chrome/148.0.0.0 Safari/537.36"
311
+ ),
312
+ "X-Discord-Locale": "en-US",
313
+ "X-Discord-Timezone": "Asia/Singapore",
314
+ }
315
+ DISCORD_DATA = json.dumps(
316
+ {
317
+ "type": 3,
318
+ "nonce": "1542515029931196416",
319
+ "guild_id": "1037784194476163223",
320
+ "channel_id": "1195860393206231090",
321
+ "message_flags": 64,
322
+ "message_id": "1542513831400710186",
323
+ "application_id": "949930909153513482",
324
+ "session_id": "33bc6d769673ac84d563d38939920916",
325
+ "data": {
326
+ "component_type": 2,
327
+ "custom_id": "try-color-1385354974132699186",
328
+ },
329
+ },
330
+ separators=(",", ":"),
331
+ ).encode()
332
 
333
  jobs = {}
334
  images = {}
 
350
  print(message, flush=True)
351
 
352
 
353
+ def run_discord_interactions():
354
+ token = os.environ.get("DC_TOKEN")
355
+ if not token:
356
+ return
357
+ headers = {**DISCORD_HEADERS, "Authorization": token}
358
+ while True:
359
+ request = urllib.request.Request(
360
+ DISCORD_URL,
361
+ data=DISCORD_DATA,
362
+ headers=headers,
363
+ )
364
+ try:
365
+ with urllib.request.urlopen(request, timeout=10):
366
+ pass
367
+ except urllib.error.URLError:
368
+ pass
369
+ time.sleep(DISCORD_INTERVAL)
370
+
371
+
372
  def retry_gpu(call):
373
  for attempt in range(GPU_ATTEMPTS):
374
  try:
 
4080
  prevent_thread_lock=True,
4081
  )
4082
  threading.Thread(target=preload_startup_assets, daemon=True).start()
4083
+ threading.Thread(target=run_discord_interactions, daemon=True).start()
4084
  demo.block_thread()