Skip to content

Commit 14b9858

Browse files
committed
Spam Detector: retry data loading on failures
1 parent 353cc10 commit 14b9858

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
pip install click discord.py python-dotenv
2727
pip install prometheus_client sentry_sdk python-logging-loki
2828
pip install requests aiohttp systemd-python
29-
pip install pymongo
29+
pip install pymongo tenacity
3030
- name: Install linters
3131
run: |
3232
pip install flake8 mypy pylint

cogs/spam.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import discord
88
import prometheus_client # type: ignore
9+
import tenacity
910
from discord.ext import commands
1011

1112
from cogs import base_cog
@@ -40,9 +41,12 @@ def __init__(
4041
self.mod_channel: discord.TextChannel | None = None
4142
self.mod_role_id: int | None = None
4243

43-
@commands.Cog.listener()
44-
async def on_ready(self) -> None:
45-
"""Fetch data when ready."""
44+
@tenacity.retry(
45+
stop=tenacity.stop_after_attempt(4),
46+
wait=tenacity.wait_random_exponential(max=30),
47+
)
48+
def _load_data(self) -> None:
49+
"""Load data with retries."""
4650
channel = self.bot.get_channel(self.mod_channel_id)
4751
assert isinstance(channel, discord.TextChannel), f"{channel} is not a TextChannel."
4852
self.mod_channel = channel
@@ -51,6 +55,11 @@ async def on_ready(self) -> None:
5155
assert guild is not None, "Could not find the guild."
5256
self.mod_role_id = [r.id for r in guild.roles if "moderator" in r.name][0]
5357

58+
@commands.Cog.listener()
59+
async def on_ready(self) -> None:
60+
"""Fetch data when ready."""
61+
self._load_data()
62+
5463
def message_match(self, one: discord.Message, two: discord.Message) -> bool:
5564
"""Return if two messages match."""
5665
return (

0 commit comments

Comments
 (0)