-
Notifications
You must be signed in to change notification settings - Fork 2
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 #88 from Vatsim-Scandinavia/feature/training-roles
Added rate limit check, new roles API + restrict event spam
- Loading branch information
Showing
4 changed files
with
63 additions
and
29 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
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,35 @@ | ||
import asyncio | ||
from discord.errors import HTTPException | ||
|
||
|
||
class Error: | ||
def __init__(self) -> None: | ||
pass | ||
|
||
async def crosspost(message, retries=3): | ||
""" | ||
Safely crossposts a message, with rate limit handling. | ||
:param message: The discord message to crosspost. | ||
:param retries: Number of retries allowed in case of rate limiting. | ||
""" | ||
for attempt in range(retries): | ||
try: | ||
# Try publishing the message | ||
await message.publish() | ||
return # Exit when the message has successfully been published | ||
|
||
except HTTPException as e: | ||
if e.status == 429: | ||
retry_after = e.retry_after if hasattr(e, 'retry_after') else 500 # Default to 500 seconds if not provided | ||
print(f'Rate limit hit! Retrying in {retry_after:.2f} seconds...', flush=True) | ||
await asyncio.sleep(retry_after) # Wait for the rate limit to reset | ||
|
||
else: | ||
#If another HTTP Exception is thrown reraise it | ||
raise | ||
|
||
except Exception as e: | ||
print(f"An Error occurred while trying to publish a message: {e}", flush=True) | ||
break | ||
|
||
print(f"Failed to publish message after {retries} attempts", flush=True) |
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