fix main file. adapt dockerfile.

This commit is contained in:
Enzo Nunes
2025-12-02 23:15:40 +00:00
parent 06d3098940
commit 1cac1d2e66
2 changed files with 104 additions and 94 deletions

View File

@@ -2,16 +2,20 @@ FROM python:3.14-slim
RUN set -eux; \ RUN set -eux; \
apt-get update; \ apt-get update; \
apt-get install -y --no-install-recommends cloc; \ apt-get install -y --no-install-recommends cloc git; \
apt-get dist-clean apt-get clean; \
rm -rf /var/lib/apt/lists/*
WORKDIR /app WORKDIR /app
RUN pip install --no-cache-dir discord asyncio requests RUN pip install --no-cache-dir discord.py requests
COPY main.py . COPY main.py .
COPY PICable.py . COPY PICable.py .
RUN useradd -m appuser RUN useradd -m appuser && \
mkdir -p /temp && \
chown appuser:appuser /temp
USER appuser USER appuser
CMD ["python", "main.py"] CMD ["python", "main.py"]

42
main.py
View File

@@ -11,23 +11,22 @@ MAX_STORAGE_KB = 50 * 1024 * 1024 # 50GB
DISCORD_TOKEN = os.getenv("PICABLE_DISCORD_TOKEN") DISCORD_TOKEN = os.getenv("PICABLE_DISCORD_TOKEN")
GITHUB_TOKEN = os.getenv("PICABLE_GITHUB_TOKEN") GITHUB_TOKEN = os.getenv("PICABLE_GITHUB_TOKEN")
if __name__ == "__main__": current_storage_kb = 0
current_storage_kb = 0 storage_condition = Condition()
storage_condition = Condition() current_repositories = set()
current_repositories = set() current_repositories_lock = Lock()
current_repositories_lock = Lock()
if not DISCORD_TOKEN or not GITHUB_TOKEN: if not DISCORD_TOKEN or not GITHUB_TOKEN:
print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN") print("Discord or Github tokens not set in env. Use variables PICABLE_DISCORD_TOKEN and PICABLE_GITHUB_TOKEN")
exit(1) exit(1)
intents = Intents.default() intents = Intents.default()
intents.message_content = True intents.message_content = True
client = commands.Bot(command_prefix="/", intents=intents) client = commands.Bot(command_prefix="/", intents=intents)
client.run(DISCORD_TOKEN)
@client.event
async def on_ready(): @client.event
async def on_ready():
print(f"We have logged in as {client.user}") print(f"We have logged in as {client.user}")
await client.change_presence(activity=Game("with PIC ideas")) await client.change_presence(activity=Game("with PIC ideas"))
try: try:
@@ -36,12 +35,14 @@ if __name__ == "__main__":
except Exception as e: except Exception as e:
print(f"Failed to sync commands: {e}") print(f"Failed to sync commands: {e}")
@client.tree.command(name="ping", description="Check the bot's latency.")
async def ping(interaction: Interaction): @client.tree.command(name="ping", description="Check the bot's latency.")
async def ping(interaction: Interaction):
latency = client.latency * 1000 latency = client.latency * 1000
await interaction.response.send_message(f"Pong! Latency: {latency:.2f}ms") await interaction.response.send_message(f"Pong! Latency: {latency:.2f}ms")
async def handle_picable(owner: str, repository: str, repository_size_kb: int, github_token: str):
async def handle_picable(owner: str, repository: str, repository_size_kb: int, github_token: str):
global current_storage_kb global current_storage_kb
async with storage_condition: async with storage_condition:
await storage_condition.wait_for(lambda: current_storage_kb + repository_size_kb <= MAX_STORAGE_KB) await storage_condition.wait_for(lambda: current_storage_kb + repository_size_kb <= MAX_STORAGE_KB)
@@ -50,9 +51,10 @@ if __name__ == "__main__":
print(f"Storage is {current_storage_kb / MAX_STORAGE_KB * 100:.2f}% full.") print(f"Storage is {current_storage_kb / MAX_STORAGE_KB * 100:.2f}% full.")
return await to_thread(PICable, owner, repository, github_token) return await to_thread(PICable, owner, repository, github_token)
@client.tree.command(name="picable", description="Check if a Github repository is eligible for PIC.")
@app_commands.describe(owner="The owner of the repository", repository="The name of the repository") @client.tree.command(name="picable", description="Check if a Github repository is eligible for PIC.")
async def picable(interaction: Interaction, owner: str, repository: str): @app_commands.describe(owner="The owner of the repository", repository="The name of the repository")
async def picable(interaction: Interaction, owner: str, repository: str):
await interaction.response.defer(thinking=True) await interaction.response.defer(thinking=True)
repository_full_name = f"{owner}/{repository}" repository_full_name = f"{owner}/{repository}"
@@ -108,3 +110,7 @@ if __name__ == "__main__":
async with storage_condition: async with storage_condition:
current_storage_kb -= repository_size_kb current_storage_kb -= repository_size_kb
storage_condition.notify_all() storage_condition.notify_all()
if __name__ == "__main__":
client.run(DISCORD_TOKEN)