From d6df8fcf4f44076c4d79e0e89ca14d7100fba63b Mon Sep 17 00:00:00 2001 From: diamante0018 Date: Mon, 3 Mar 2025 18:47:17 +0100 Subject: [PATCH] feat: detect sales! --- .github/workflows/docker-publish.yml | 6 +-- bot/tasks.py | 76 +++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index dfc604f..f864ad0 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -17,7 +17,7 @@ jobs: uses: actions/checkout@main - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3.6.1 + uses: docker/setup-buildx-action@v3.10.0 - name: Log in to DockerHub uses: docker/login-action@v3.3.0 @@ -26,7 +26,7 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - id: meta - uses: docker/metadata-action@v5.5.1 + uses: docker/metadata-action@v5.7.0 with: images: | alterware/aw-bot @@ -36,7 +36,7 @@ jobs: - name: Build and push Docker image id: build-and-push - uses: docker/build-push-action@v6.7.0 + uses: docker/build-push-action@v6.15.0 with: context: . platforms: linux/amd64,linux/arm64 diff --git a/bot/tasks.py b/bot/tasks.py index b3d04ca..9b1524d 100644 --- a/bot/tasks.py +++ b/bot/tasks.py @@ -1,13 +1,85 @@ from datetime import datetime, timezone +import requests import discord -from discord.ext import tasks +from discord.ext import tasks, commands from bot.utils import aware_utcnow, fetch_api_data TARGET_DATE = datetime(2036, 8, 12, tzinfo=timezone.utc) OFFTOPIC_CHANNEL = 1112048063448617142 +COD_GAMES = { + 10180: {"name": "Modern Warfare 2 (2009)", "channel": 1145458108190163014}, + 42680: {"name": "Modern Warfare 3 (2011)", "channel": 1145459504436220014}, + 209160: {"name": "Call of Duty: Ghosts", "channel": 1145469106133401682}, + 209650: {"name": "Call of Duty: Advanced Warfare", "channel": 1145469136919613551}, + 311210: {"name": "Call of Duty: Black Ops 3", "channel": 1180796251529293844}, +} + + +class SteamSaleChecker(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.check_steam_sale.start() # Start the task when the cog is loaded + + def cog_unload(self): + self.check_steam_sale.cancel() # Stop the task when the cog is unloaded + + @tasks.loop(hours=24) + async def check_steam_sale(self): + + for app_id, game_data in COD_GAMES.items(): + game_name = game_data["name"] + channel_id = game_data["channel"] + + channel = self.bot.get_channel(channel_id) + if channel is None: + print(f"Error: Channel ID {channel_id} for {game_name} not found.") + return + + steam_api_url = ( + f"https://store.steampowered.com/api/appdetails?appids={app_id}" + ) + + try: + response = requests.get(steam_api_url) + data = response.json().get(str(app_id), {}).get("data", {}) + + if not data: + print(f"Warning: No data returned for {game_name}. Skipping...") + return + + price_info = data.get("price_overview", {}) + + if not price_info: + await channel.send( + f"{game_name} is currently unavailable for purchase." + ) + return + + original_price = price_info.get("initial", 0) / 100 + discounted_price = price_info.get("final", 0) / 100 + discount_percent = price_info.get("discount_percent", 0) + store_url = f"https://store.steampowered.com/app/{app_id}/" + + if discount_percent > 0: + message = ( + f"**{game_name} is on sale!**\n" + f"Original Price: **${original_price:.2f}**\n" + f"Discounted Price: **${discounted_price:.2f}** (**-{discount_percent}%**)\n" + f"[View on Steam]({store_url})\n" + ) + + await channel.send(message) + + except requests.RequestException as e: + print(f"Error fetching Steam sale data for {game_name}: {e}") + + @check_steam_sale.before_loop + async def before_check_steam_sale(self): + await self.bot.wait_until_ready() + async def setup(bot): @tasks.loop(minutes=10) @@ -42,4 +114,6 @@ async def setup(bot): update_status.start() heat_death.start() + await bot.add_cog(SteamSaleChecker(bot)) + print("Tasks extension loaded!")