Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
import logging
|
6 |
+
from fastapi import FastAPI, BackgroundTasks
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Configuraci贸n de logging
|
11 |
+
logging.basicConfig(level=logging.INFO,
|
12 |
+
format="%(asctime)s - %(levelname)s - %(message)s")
|
13 |
+
logger = logging.getLogger("DiscordLogger")
|
14 |
+
|
15 |
+
# Datos para la API de Discord
|
16 |
+
TOKEN = os.getenv("TOKEN") # Aseg煤rate de que est茅 configurado en Replit
|
17 |
+
URL = "https://discord.com/api/v9/channels/1330367547249524788/messages"
|
18 |
+
|
19 |
+
HEADERS = {
|
20 |
+
"User-Agent": "Mozilla/5.0",
|
21 |
+
"Accept": "*/*",
|
22 |
+
"Content-Type": "application/json",
|
23 |
+
"Authorization": TOKEN
|
24 |
+
}
|
25 |
+
|
26 |
+
STICKER_IDS = [
|
27 |
+
"1004733061377839155", "1069462756597714954", "1069464246276083783",
|
28 |
+
"1071961877590069329", "1135522478924496968", "1143089446959456266",
|
29 |
+
"1151968132378931341", "1167322137225342986", "1203740621668483112",
|
30 |
+
"1238703199091953725", "1240964158388961331", "1243564218741952663",
|
31 |
+
"1257957401236344915", "1260907231549980734", "1263109894807097404",
|
32 |
+
"1338763640630804490", "1338945320498888745"
|
33 |
+
]
|
34 |
+
GIF_URLS = [
|
35 |
+
"https://tenor.com/view/cute-impatient-dog-bulldog-happy-gif-25124319",
|
36 |
+
"https://media.discordapp.net/attachments/978373982011150369/981008116990771270/9C48BE6C-EA86-49CF-90DB-C07F58AAE464.gif?ex=67b7d881&is=67b68701&hm=5b3a233dafc4de52414bae4280cb644fda85e12430a30470d7b2b59c78605d5a&",
|
37 |
+
"https://cdn.discordapp.com/attachments/1330367547249524788/1335016420110630992/SPOILER_Kiss-ezgif.com-speed.gif?ex=67b7aeed&is=67b65d6d&hm=fc5b5f4fed5e4af289dd4b257640d6891b3722b967aed56689057e1ce5ca0de9&",
|
38 |
+
"https://media.discordapp.net/attachments/1304500459565744161/1306263414607450142/media-iv-text.gif?ex=67b7e3db&is=67b6925b&hm=88be547a870fa4dc2ced25b470c2324a87b7ed7d0bfe8158a426f289c7b7d312&",
|
39 |
+
"https://media.discordapp.net/attachments/1086563383924494386/1154871691827236884/rf.gif?ex=67b78b93&is=67b63a13&hm=7a780800f3bb3a70695c1bcf4df6ecb291c4f8fdeadd1192e086f9749e315183&",
|
40 |
+
"https://media.discordapp.net/attachments/753808274062573639/809190519594025050/arana.gif?ex=67b7ade5&is=67b65c65&hm=422e139b8d883a76476022acfdddf8cb9dd0492bbc775540b228f1479828590f&",
|
41 |
+
"https://cdn.discordapp.com/attachments/1334611383442083901/1334611429583618138/cd16102f-4f11-45a3-b34a-0ccf58e3f982.gif?ex=67b78740&is=67b635c0&hm=6b129cbfe287257f6f476c3bb696b2b5d15614e93bb722e42051e639c0011be0&",
|
42 |
+
"https://media.discordapp.net/attachments/1330367547249524788/1334973793940144138/image0.gif?ex=67b7873b&is=67b635bb&hm=7429b7df7a90562babde0111e9bbcbfee1974ebbee12c857e8bbc59a2623a0af&=",
|
43 |
+
"https://media.discordapp.net/attachments/1037060119881601195/1176932252559622204/voicemessage.gif?ex=67b76193&is=67b61013&hm=394a35f837da28653683cb229f2209deeed4a9d6aaba2441de88968a34103342&=",
|
44 |
+
"https://tenor.com/view/funny-gif-11026644638307910936",
|
45 |
+
"https://tenor.com/view/bee-gif-2221550446572997929",
|
46 |
+
"https://cdn.discordapp.com/attachments/1330367547249524788/1341925949138010173/PERDONAME_POR_TODO_ALISON.gif?ex=67b7c56e&is=67b673ee&hm=0f974f8d5888ef5732f9b0ebee9276466e4dce4c99b7cc959327b0758a577357&",
|
47 |
+
"https://tenor.com/view/cats-cat-cute-run-kitten-gif-14804766"
|
48 |
+
]
|
49 |
+
|
50 |
+
|
51 |
+
def send_message(data):
|
52 |
+
"""Env铆a un mensaje a Discord y maneja errores con reintentos controlados."""
|
53 |
+
while True:
|
54 |
+
try:
|
55 |
+
response = requests.post(URL, headers=HEADERS, json=data)
|
56 |
+
|
57 |
+
if response.status_code == 429:
|
58 |
+
retry_after = response.json().get("retry_after", 5)
|
59 |
+
logger.warning(
|
60 |
+
f"L铆mite alcanzado. Reintentando en {retry_after}s...")
|
61 |
+
time.sleep(retry_after)
|
62 |
+
continue
|
63 |
+
|
64 |
+
if response.ok:
|
65 |
+
logger.info(f"Mensaje enviado correctamente: {data}")
|
66 |
+
else:
|
67 |
+
logger.error(f"Error {response.status_code}: {response.text}")
|
68 |
+
|
69 |
+
response.raise_for_status()
|
70 |
+
break
|
71 |
+
except requests.exceptions.RequestException as e:
|
72 |
+
logger.error(f"Error de conexi贸n: {e}")
|
73 |
+
time.sleep(5)
|
74 |
+
|
75 |
+
|
76 |
+
def send_random():
|
77 |
+
"""Env铆a un sticker o un GIF aleatorio en intervalos de tiempo."""
|
78 |
+
while True:
|
79 |
+
action = random.choice([{
|
80 |
+
"sticker_ids": [random.choice(STICKER_IDS)]
|
81 |
+
}, {
|
82 |
+
"content": random.choice(GIF_URLS)
|
83 |
+
}])
|
84 |
+
send_message(action)
|
85 |
+
sleep_time = random.randint(65, 80)
|
86 |
+
logger.info(
|
87 |
+
f"Esperando {sleep_time} segundos antes del pr贸ximo env铆o.")
|
88 |
+
time.sleep(sleep_time)
|
89 |
+
|
90 |
+
|
91 |
+
@app.get("/ping")
|
92 |
+
def health_check():
|
93 |
+
return {"status": "OK"}
|
94 |
+
|
95 |
+
|
96 |
+
@app.on_event("startup")
|
97 |
+
def start_background_tasks():
|
98 |
+
"""Inicia la tarea en segundo plano al arrancar FastAPI."""
|
99 |
+
import threading
|
100 |
+
thread = threading.Thread(target=send_random, daemon=True)
|
101 |
+
thread.start()
|