-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3647 from KBVE/dev
Preparing Alpha Branch
- Loading branch information
Showing
11 changed files
with
260 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import os | ||
import json | ||
from ..clients.chrome_client import ChromeClient | ||
import logging | ||
import re | ||
from asyncio import sleep | ||
|
||
logger = logging.getLogger("uvicorn") | ||
|
||
class DiscordClient: | ||
def __init__(self, headless=True, display=":1"): | ||
self.headless = headless | ||
self.display = display | ||
self.chrome_client = ChromeClient(headless=headless, display=display) | ||
|
||
async def login_with_passkey(self, discord_url="https://discord.com/login", passkey=None, retries=3): | ||
""" | ||
Logs into Discord by injecting the passkey into localStorage. | ||
:param discord_url: URL for Discord login | ||
:param passkey: Discord passkey (token). If None, retrieves from environment variable. | ||
:param retries: Number of retry attempts for login in case of failure. | ||
:return: Success or error message. | ||
""" | ||
passkey = passkey or os.getenv("DISCORD_PASSKEY") | ||
if not passkey: | ||
raise ValueError("Passkey not provided. Set it as an argument or in the DISCORD_PASSKEY environment variable.") | ||
|
||
# Validate the token format (alphanumeric with optional underscores, hyphens, or dots) | ||
if not re.match(r"^[A-Za-z0-9_\-\.]+$", passkey): | ||
raise ValueError("Invalid token format. Discord tokens should be alphanumeric with optional underscores, hyphens, or dots.") | ||
|
||
|
||
for attempt in range(retries): | ||
try: | ||
logger.info(f"Attempt {attempt + 1} to log into Discord") | ||
await self.chrome_client.start_chrome_async() | ||
|
||
logger.info(f"Opening Discord URL: {discord_url}") | ||
await self.chrome_client.perform_task_with_chrome(discord_url) | ||
|
||
logger.info("Injecting passkey into localStorage") | ||
self.chrome_client.sb.execute_script(f''' | ||
window.localStorage.setItem('token', '{passkey}'); | ||
''') | ||
|
||
logger.info("Refreshing the page to apply the token") | ||
self.chrome_client.sb.refresh() | ||
|
||
logger.info("Checking for successful login") | ||
if self.chrome_client.sb.is_element_visible('[data-testid="user-settings"]', timeout=10): | ||
logger.info("Discord login verified successfully.") | ||
return "Logged into Discord using passkey successfully." | ||
else: | ||
logger.warning("Failed to verify Discord login.") | ||
return "Failed to verify Discord login." | ||
except Exception as e: | ||
logger.warning(f"Attempt {attempt + 1} failed: {e}") | ||
if attempt < retries - 1: | ||
await sleep(2) # Wait before retrying | ||
else: | ||
raise e | ||
finally: | ||
await self.chrome_client.stop_chrome_async() | ||
|
||
async def verify_login(self): | ||
""" | ||
Verifies if the user is logged into Discord by checking for an authenticated element. | ||
:return: Boolean indicating login success or failure. | ||
""" | ||
try: | ||
# Check if a Discord-specific element is visible after login | ||
if self.chrome_client.sb.is_element_visible('[data-testid="user-settings"]'): | ||
logger.info("Discord login verified successfully.") | ||
return True | ||
else: | ||
logger.warning("Discord login verification failed.") | ||
return False | ||
except Exception as e: | ||
logger.error(f"Failed to verify login: {e}") | ||
return False | ||
|
||
async def fetch_discord_data(self): | ||
""" | ||
Example method to demonstrate fetching data from Discord after login. | ||
You can customize this based on your requirements. | ||
""" | ||
try: | ||
# Navigate to a page and extract data | ||
guild_url = "https://discord.com/channels/@me" | ||
await self.chrome_client.perform_task_with_chrome(guild_url) | ||
|
||
# Extract some example data (like user settings or guild info) | ||
user_data = self.chrome_client.sb.get_text('[data-testid="user-settings"]') | ||
logger.info(f"Fetched user data: {user_data}") | ||
return user_data | ||
except Exception as e: | ||
logger.error(f"Failed to fetch Discord data: {e}") | ||
return f"Failed to fetch Discord data: {e}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: 'Decemeber: 29th' | ||
category: Daily | ||
date: 2024-12-29 12:00:00 | ||
client: Self | ||
unsplash: 1511512578047-dfb367046420 | ||
img: https://images.unsplash.com/photo-1511512578047-dfb367046420?crop=entropy&cs=srgb&fm=jpg&ixid=MnwzNjM5Nzd8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODE3NDg2ODY&ixlib=rb-4.0.3&q=85 | ||
description: Decemeber 29th. | ||
tags: | ||
- daily | ||
--- | ||
|
||
import { Adsense, Tasks } from '@kbve/astropad'; | ||
|
||
## 2024 | ||
|
||
- 01:27PM | ||
|
||
**2024** | ||
|
||
The year is about to wrap over, kinda weird. | ||
There were some changes to account for! | ||
|
||
- 04:01PM | ||
|
||
**Salt** | ||
|
||
I hate to have this entry but it is what it is. | ||
|
||
- 06:06PM | ||
|
||
**Unity** | ||
|
||
After yesterday, I been spending some time to become better at Unity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: 'Decemeber: 30th' | ||
category: Daily | ||
date: 2024-12-30 12:00:00 | ||
client: Self | ||
unsplash: 1511512578047-dfb367046420 | ||
img: https://images.unsplash.com/photo-1511512578047-dfb367046420?crop=entropy&cs=srgb&fm=jpg&ixid=MnwzNjM5Nzd8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODE3NDg2ODY&ixlib=rb-4.0.3&q=85 | ||
description: Decemeber 30th. | ||
tags: | ||
- daily | ||
--- | ||
|
||
import { Adsense, Tasks } from '@kbve/astropad'; | ||
|
||
## 2024 | ||
|
||
- 10:35AM | ||
|
||
**SPY** | ||
|
||
Damn $585? Wild. | ||
|
||
- 05:40PM | ||
|
||
**Time?*** | ||
|
||
xD.... we deal with it as family |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: 'Decemeber: 31st' | ||
category: Daily | ||
date: 2024-12-31 12:00:00 | ||
client: Self | ||
unsplash: 1511512578047-dfb367046420 | ||
img: https://images.unsplash.com/photo-1511512578047-dfb367046420?crop=entropy&cs=srgb&fm=jpg&ixid=MnwzNjM5Nzd8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODE3NDg2ODY&ixlib=rb-4.0.3&q=85 | ||
description: Decemeber 31st. | ||
tags: | ||
- daily | ||
--- | ||
|
||
import { Adsense, Tasks } from '@kbve/astropad'; | ||
|
||
## 2024 | ||
|
||
- 05:19PM | ||
|
||
**New Years** | ||
|
||
I am super excited for the new year, this is the last entry for this year too? | ||
Its going to be more interesting that I can go back to my early entries. | ||
I will have to also get used of updating the 2024 to 2025. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.